Mark Breman
2013-May-28 06:54 UTC
[R] Execution of larger blocks of pasted code often fails?
Hello List, When i paste a large block of R code from an editor to the R command line the execution of the code will often fail at some point because it is not pasted as it was copied. ==============================================================Example: I copied the following block of code (as part of a larger block): .... s$Price <- quote(prices(St, uc=Adjusted, ent=c(Adjusted[-1], NA), xit=c(Adjusted[-1], NA))) s$PnL <- quote(pnl(Price, St)) s$PPnL <- quote(ppnl(Price, St)) s$RoR <- quote(ror(Price, St, delta=1/Price)) t = na.omit(trades(rownames(s), s$Price, s$St)) .... and when i paste this to the R command-line the following happens: ....> s$Price <- quote(prices(St, uc=Adjusted, ent=c(Adjusted[-1], NA),xit=c(Adjusted[-1], NA)))> s$PnL <- quote(pnl(Price,+ + s$RoR <- quote(ror(Pr + t = na.omit(trade Error: unexpected symbol in: "s$RoR <- quote(ror(Pr t" .... ================================================================ As you can see from the example it looks like the code was not pasted correctly. If i execute the same block of code with the source() command it runs just fine! Here is the output of my sessionInfo():> sessionInfo()R version 2.15.3 (2013-03-01) Platform: i486-pc-linux-gnu (32-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=nl_NL.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=nl_NL.UTF-8 [6] LC_MESSAGES=en_US.UTF-8 LC_PAPER=C LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=nl_NL.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] DEoptim_2.2-1 IBrokers_0.9-10 stringr_0.6.2 timeDate_2160.97 PerformanceAnalytics_1.1.0 [6] tradesys_0.1 RUnit_0.4.26 quantmod_0.4-0 TTR_0.22-0 xts_0.9-3 [11] zoo_1.7-9 Defaults_1.1-1 loaded via a namespace (and not attached): [1] grid_2.15.3 lattice_0.20-15 tcltk_2.15.3 tools_2.15.3 Is there anyone who experiences the same problem? Kind regards, -Mark- [[alternative HTML version deleted]]
Pascal Oettli
2013-May-28 07:40 UTC
[R] Execution of larger blocks of pasted code often fails?
Hello, Yes, I do. I just copy and paste smallest blocks, easiest to debug. Regards, Pascal On 28/05/2013 15:54, Mark Breman wrote:> Hello List, > > When i paste a large block of R code from an editor to the R command line > the execution of the code will often fail at some point because it is not > pasted as it was copied. > > ==============================================================> Example: > > I copied the following block of code (as part of a larger block): > > .... > s$Price <- quote(prices(St, uc=Adjusted, ent=c(Adjusted[-1], NA), > xit=c(Adjusted[-1], NA))) > s$PnL <- quote(pnl(Price, St)) > s$PPnL <- quote(ppnl(Price, St)) > s$RoR <- quote(ror(Price, St, delta=1/Price)) > t = na.omit(trades(rownames(s), s$Price, s$St)) > .... > > and when i paste this to the R command-line the following happens: > > .... >> s$Price <- quote(prices(St, uc=Adjusted, ent=c(Adjusted[-1], NA), > xit=c(Adjusted[-1], NA))) >> s$PnL <- quote(pnl(Price, > + > + s$RoR <- quote(ror(Pr > + t = na.omit(trade > Error: unexpected symbol in: > "s$RoR <- quote(ror(Pr > t" > .... > ================================================================> > As you can see from the example it looks like the code was not pasted > correctly. > If i execute the same block of code with the source() command it runs just > fine! > > Here is the output of my sessionInfo(): > >> sessionInfo() > R version 2.15.3 (2013-03-01) > Platform: i486-pc-linux-gnu (32-bit) > > locale: > [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C > LC_TIME=nl_NL.UTF-8 LC_COLLATE=en_US.UTF-8 > LC_MONETARY=nl_NL.UTF-8 > [6] LC_MESSAGES=en_US.UTF-8 LC_PAPER=C LC_NAME=C > LC_ADDRESS=C LC_TELEPHONE=C > [11] LC_MEASUREMENT=nl_NL.UTF-8 LC_IDENTIFICATION=C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > other attached packages: > [1] DEoptim_2.2-1 IBrokers_0.9-10 stringr_0.6.2 > timeDate_2160.97 PerformanceAnalytics_1.1.0 > [6] tradesys_0.1 RUnit_0.4.26 quantmod_0.4-0 > TTR_0.22-0 xts_0.9-3 > [11] zoo_1.7-9 Defaults_1.1-1 > > loaded via a namespace (and not attached): > [1] grid_2.15.3 lattice_0.20-15 tcltk_2.15.3 tools_2.15.3 > > > Is there anyone who experiences the same problem? > > Kind regards, > > -Mark- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Barry Rowlingson
2013-May-28 08:00 UTC
[R] Execution of larger blocks of pasted code often fails?
On Tue, May 28, 2013 at 7:54 AM, Mark Breman <breman.mark at gmail.com> wrote:> Hello List, > > When i paste a large block of R code from an editor to the R command line > the execution of the code will often fail at some point because it is not > pasted as it was copied.This reminds me of the Tommy Cooper joke (I can't do the voice, especially over email): Man: "Doctor, it hurts when I do this" [waves arm about] Doc: "Well don't do that then". Cutting and pasting code - especially large blocks of code - is a poor way to work with R. Make your code re-usable by putting it in functions with sensible arguments and meaningful return values. Debug your functions with little test data sets, or go the whole hog and use package:testthat for writing test suites. Organise your functions into packages and use package:devtools to make packages REALLY REALLY easy. Easier than having a big text file with all the R code you've ever written in, and then pasting chunks of into R every time. Or pasting it back into your big text file and then changing a little bit of it so you now have two very similar chunks of R code with the same bug now in two positions.... Take a day to read up on how to effectively work with R with functions and packages, then revisit your work and see how that can apply to you. You'll get that day back within a couple of weeks. B
Enrico Schumann
2013-May-28 09:33 UTC
[R] Execution of larger blocks of pasted code often fails?
On Tue, 28 May 2013, Mark Breman <breman.mark at gmail.com> writes:> Hello List, > > When i paste a large block of R code from an editor to the R command line > the execution of the code will often fail at some point because it is not > pasted as it was copied. > > ==============================================================> Example: > > I copied the following block of code (as part of a larger block): > > .... > s$Price <- quote(prices(St, uc=Adjusted, ent=c(Adjusted[-1], NA), > xit=c(Adjusted[-1], NA))) > s$PnL <- quote(pnl(Price, St)) > s$PPnL <- quote(ppnl(Price, St)) > s$RoR <- quote(ror(Price, St, delta=1/Price)) > t = na.omit(trades(rownames(s), s$Price, s$St)) > .... > > and when i paste this to the R command-line the following happens: > > .... >> s$Price <- quote(prices(St, uc=Adjusted, ent=c(Adjusted[-1], NA), > xit=c(Adjusted[-1], NA))) >> s$PnL <- quote(pnl(Price, > + > + s$RoR <- quote(ror(Pr > + t = na.omit(trade > Error: unexpected symbol in: > "s$RoR <- quote(ror(Pr > t" > .... > ================================================================> > As you can see from the example it looks like the code was not pasted > correctly. > If i execute the same block of code with the source() command it runs just > fine! >[...] Just for the record, I can copy and paste your code -- and get errors of the type Error in na.omit(trades(rownames(s), s$Price, s$St)) : could not find function "trades" But as others have already pointed out, it is better to '?source' code, or to use an editor that can push code chunks directly to R. -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net
Jim Lemon
2013-May-28 09:44 UTC
[R] Execution of larger blocks of pasted code often fails?
On 05/28/2013 04:54 PM, Mark Breman wrote:> Hello List, > > When i paste a large block of R code from an editor to the R command line > the execution of the code will often fail at some point because it is not > pasted as it was copied. >...Hi Mark, I have encountered this problem, and it may be due to the following: Command lines entered at the console are limited to about 4095 bytes (not characters). From "An Introduction to R", included in the distribution. Jim