Hello, I have found what I believe to be a bug in the Linux version of the Rscript binary. Under Windows (official 64-bit 3.5.1 R distribution running on an up-to-date Win10), I can do the following (e.g. under powershell): PS H:\Users\Cesko> Rscript -e 'ls()>> ls()'character(0) character(0) which works as I expect: I am running Rscript with two arguments, namely (1) '-e', and (2) two lines of code to be run, and it indeed executes those two lines of code. This fails when attempted on a Linux build (amd64, compiled from the official 3.5.1 sources, but also reproducible with today's r-devel snapshot): $ Rscript -e 'ls() ls()' ARGUMENT 'ls()' __ignored__ character(0) This behavior is not what I expected. Have I found a bug, or am I simply using it wrong? Regards, Cesko
Same on Mac: $ Rscript -e 'ls()> ls()'ARGUMENT 'ls()' __ignored__ character(0) as well as using ?\n? as a line separator: $ Rscript -e 'ls()\nls()' ARGUMENT 'ls()' __ignored__ character(0)> On 16 Sep 2018, at 10:53, Voeten, C.C. <c.c.voeten at hum.leidenuniv.nl> wrote: > > Rscript -e 'ls()-- Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany) University of Z?rich Cell: +41 (0)78 630 66 57 email: Rainer at krugs.de Skype: RMkrug PGP: 0x0F52F982 -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 488 bytes Desc: Message signed with OpenPGP URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20180917/3247a469/attachment.sig>
Duncan Murdoch
2018-Sep-17 11:09 UTC
[Rd] Rscript -e does not accept newlines under Linux?
On 16/09/2018 4:53 AM, Voeten, C.C. wrote:> Hello, > > I have found what I believe to be a bug in the Linux version of the Rscript binary. > Under Windows (official 64-bit 3.5.1 R distribution running on an up-to-date Win10), I can do the following (e.g. under powershell): > > PS H:\Users\Cesko> Rscript -e 'ls() >>> ls()' > character(0) > character(0) > > which works as I expect: I am running Rscript with two arguments, namely (1) '-e', and (2) two lines of code to be run, and it indeed executes those two lines of code. > > This fails when attempted on a Linux build (amd64, compiled from the official 3.5.1 sources, but also reproducible with today's r-devel snapshot): > > $ Rscript -e 'ls() > ls()' > ARGUMENT 'ls()' __ignored__ > > character(0) > > This behavior is not what I expected. Have I found a bug, or am I simply using it wrong?I would not assume that shell behaviour in Windows and Unix would always be the same. A better comparison would be to list some other command on the same system that behaves differently. For example, on MacOS I see $ echo 'ls() > ls()' ls() ls() which suggests that what you wrote should be legal, but the form of that command is different: there's no equivalent of "-e". Maybe someone else who knows Unix shell behaviour better can comment on whether they'd expect your Rscript command to work. By the way, if you just want multiple commands to execute, you can separate them by semi-colons, and that does work: $ Rscript -e 'ls(); ls()' character(0) character(0) And I see this, which may explain the original problem: $ Rscript -e 'commandArgs(); ls()' [1] "/Library/Frameworks/R.framework/Resources/bin/exec/R" [2] "--slave" [3] "--no-restore" [4] "-e" [5] "commandArgs();~+~ls()" character(0) Notice that argument 5 includes both commands, whereas with the newline they are separated: $ Rscript -e 'commandArgs() > ls()' ARGUMENT 'ls()' __ignored__ [1] "/Library/Frameworks/R.framework/Resources/bin/exec/R" [2] "--slave" [3] "--no-restore" [4] "-e" [5] "commandArgs()" [6] "ls()" And finally, this also works: Rscript -e 'ls() -e ls()' Duncan Murdoch
Dirk Eddelbuettel
2018-Sep-17 11:37 UTC
[Rd] Rscript -e does not accept newlines under Linux?
On 17 September 2018 at 07:09, Duncan Murdoch wrote: | I would not assume that shell behaviour in Windows and Unix would always | be the same. A better comparison would be to list some other command on | the same system that behaves differently. For example, on MacOS I see | | $ echo 'ls() | > ls()' | ls() | ls() | | | which suggests that what you wrote should be legal, but the form of that | command is different: there's no equivalent of "-e". Maybe someone else | who knows Unix shell behaviour better can comment on whether they'd | expect your Rscript command to work. When we wrote littler, ie 'r', just before Rscript was added to R itself, the ability to work from standard input just like any other Unix tool does was in fact a design feature. So with littler it works (and you need -p to print as we are silent by default by another design choice) edd at rob:~$ (echo "ls()"; echo "ls()") ls() ls() edd at rob:~$ (echo "ls()"; echo "ls()") | r -p [1] "argv" [1] "argv" edd at rob:~$ argv is a global variable to hold the arguments (as in C). Hence standard things work the way you expect them to: edd at rob:~$ echo "set.seed(123); print(summary(rnorm(1e6)))" | r Min. 1st Qu. Median Mean 3rd Qu. Max. -4.79919 -0.67439 -0.00026 -0.00052 0.67333 4.85077 edd at rob:~$ just like r -e ... would, or R -e do now. It is harder to do this for R and Rscript due to the way they are invoked, setting shell variables and all that before calling into $R_HOME/exec/bin/R. Dirk -- http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
Tomas Kalibera
2018-Oct-08 15:05 UTC
[Rd] Rscript -e does not accept newlines under Linux?
I've checked in an experimental fix for this (75413). The newline was lost in the shell script wrapper for R, it is now being escaped similarly to space. To pass multiple commands to Rscript, one can also use "-e" multiple times. Tomas On 09/17/2018 01:09 PM, Duncan Murdoch wrote:> On 16/09/2018 4:53 AM, Voeten, C.C. wrote: >> Hello, >> >> I have found what I believe to be a bug in the Linux version of the >> Rscript binary. >> Under Windows (official 64-bit 3.5.1 R distribution running on an >> up-to-date Win10), I can do the following (e.g. under powershell): >> >> PS H:\Users\Cesko> Rscript -e 'ls() >>>> ls()' >> character(0) >> character(0) >> >> which works as I expect: I am running Rscript with two arguments, >> namely (1) '-e', and (2) two lines of code to be run, and it indeed >> executes those two lines of code. >> >> This fails when attempted on a Linux build (amd64, compiled from the >> official 3.5.1 sources, but also reproducible with today's r-devel >> snapshot): >> >> $ Rscript -e 'ls() >> ls()' >> ARGUMENT 'ls()' __ignored__ >> >> character(0) >> >> This behavior is not what I expected. Have I found a bug, or am I >> simply using it wrong? > > I would not assume that shell behaviour in Windows and Unix would > always be the same.? A better comparison would be to list some other > command on the same system that behaves differently.? For example, on > MacOS I see > > $ echo 'ls() > > ls()' > ls() > ls() > > > which suggests that what you wrote should be legal, but the form of > that command is different: there's no equivalent of "-e". Maybe > someone else who knows Unix shell behaviour better can comment on > whether they'd expect your Rscript command to work. > > By the way, if you just want multiple commands to execute, you can > separate them by semi-colons, and that does work: > > $ Rscript -e 'ls(); ls()' > character(0) > character(0) > > And I see this, which may explain the original problem: > > $ Rscript -e 'commandArgs(); ls()' > [1] "/Library/Frameworks/R.framework/Resources/bin/exec/R" > [2] "--slave" > [3] "--no-restore" > [4] "-e" > [5] "commandArgs();~+~ls()" > character(0) > > Notice that argument 5 includes both commands, whereas with the > newline they are separated: > > $ Rscript -e 'commandArgs() > > ls()' > ARGUMENT 'ls()' __ignored__ > > [1] "/Library/Frameworks/R.framework/Resources/bin/exec/R" > [2] "--slave" > [3] "--no-restore" > [4] "-e" > [5] "commandArgs()" > [6] "ls()" > > And finally, this also works: > > Rscript -e 'ls() > -e > ls()' > > > Duncan Murdoch > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel