Rscript -e does not accept newlines under Linux?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Rscript -e does not accept newlines under Linux?

Voeten, C.C.
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
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Rscript -e does not accept newlines under Linux?

Rainer Krug-3
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. <[hidden email]> 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:      [hidden email]
Skype:      RMkrug

PGP: 0x0F52F982




______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Rscript -e does not accept newlines under Linux?

Duncan Murdoch-2
In reply to this post by Voeten, C.C.
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Rscript -e does not accept newlines under Linux?

Dirk Eddelbuettel

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@rob:~$ (echo "ls()"; echo "ls()")
  ls()
  ls()
  edd@rob:~$ (echo "ls()"; echo "ls()") | r -p
  [1] "argv"
  [1] "argv"
  edd@rob:~$

argv is a global variable to hold the arguments (as in C).

Hence standard things work the way you expect them to:

  edd@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@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 | [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Rscript -e does not accept newlines under Linux?

Tomas Kalibera
In reply to this post by Duncan Murdoch-2
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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel