[Rd] Rscript -e does not accept newlines under Linux?

2018-09-17 Thread 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
__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2018-09-17 Thread Rainer Krug
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.  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:  rai...@krugs.de
Skype:  RMkrug

PGP: 0x0F52F982





signature.asc
Description: Message signed with OpenPGP
__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2018-09-17 Thread Duncan Murdoch

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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2018-09-17 Thread 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 | e...@debian.org

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] diag(-1) produces weird result

2018-09-17 Thread William Revelle
Dear list

A strange bug in the psych package is due to the behavior of the diag function:

It gives the expected values for 1, a vector (-1,1), but not for -1

Is this a known feature?


> diag(1)
 [,1]
[1,]1
> diag(c(-1,1))
 [,1] [,2]
[1,]   -10
[2,]01
> diag(-1)
Error in diag(-1) : invalid 'nrow' value (< 0)


Bill


William Revellepersonality-project.org/revelle.html
Professor personality-project.org
Department of Psychology www.wcas.northwestern.edu/psych/
Northwestern Universitywww.northwestern.edu/
Use R for psychology personality-project.org/r
It is 2   minutes to midnight   www.thebulletin.org

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] diag(-1) produces weird result

2018-09-17 Thread Duncan Murdoch

On 17/09/2018 12:14 PM, William Revelle wrote:

Dear list

A strange bug in the psych package is due to the behavior of the diag function:

It gives the expected values for 1, a vector (-1,1), but not for -1

Is this a known feature?


It is pretty clearly documented:

"diag has four distinct usages:

...

3.  x is a scalar (length-one vector) and the only argument, it returns 
a square identity matrix of size given by the scalar."


Duncan Murdoch





diag(1)

  [,1]
[1,]1

diag(c(-1,1))

  [,1] [,2]
[1,]   -10
[2,]01

diag(-1)

Error in diag(-1) : invalid 'nrow' value (< 0)


Bill


William Revellepersonality-project.org/revelle.html
Professor personality-project.org
Department of Psychology www.wcas.northwestern.edu/psych/
Northwestern Universitywww.northwestern.edu/
Use R for psychology personality-project.org/r
It is 2   minutes to midnight   www.thebulletin.org

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel



__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] diag(-1) produces weird result

2018-09-17 Thread Gábor Csárdi
I would say it is a mis-feature. If the 'x' argument of diag() is a
vector of length 1, then it creates an identity matrix of that size,
instead of creating a 1x1 matrix with the given value:

❯ diag(3)
 [,1] [,2] [,3]
[1,]100
[2,]010
[3,]001

Of course this makes it cumbersome to use diag() in a package, when
you are not sure if the input vector is longer than 1. This seems to
be a good workaround:

❯ diag(-1, nrow = 1)
 [,1]
[1,]   -1

Or, in general if you have vector v:

❯ v <- -1
❯ diag(v, nrow = length(v))
 [,1]
[1,]   -1

Gabor
On Mon, Sep 17, 2018 at 5:14 PM William Revelle  wrote:
>
> Dear list
>
> A strange bug in the psych package is due to the behavior of the diag 
> function:
>
> It gives the expected values for 1, a vector (-1,1), but not for -1
>
> Is this a known feature?
>
>
> > diag(1)
>  [,1]
> [1,]1
> > diag(c(-1,1))
>  [,1] [,2]
> [1,]   -10
> [2,]01
> > diag(-1)
> Error in diag(-1) : invalid 'nrow' value (< 0)
>
>
> Bill
>
>
> William Revellepersonality-project.org/revelle.html
> Professor personality-project.org
> Department of Psychology www.wcas.northwestern.edu/psych/
> Northwestern Universitywww.northwestern.edu/
> Use R for psychology personality-project.org/r
> It is 2   minutes to midnight   www.thebulletin.org
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] diag(-1) produces weird result

2018-09-17 Thread Barry Rowlingson
On Mon, Sep 17, 2018 at 5:22 PM, Gábor Csárdi 
wrote:

> I would say it is a mis-feature. If the 'x' argument of diag() is a
> vector of length 1, then it creates an identity matrix of that size,
> instead of creating a 1x1 matrix with the given value:
>
> ❯ diag(3)
>  [,1] [,2] [,3]
> [1,]100
> [2,]010
> [3,]001
>
> Of course this makes it cumbersome to use diag() in a package, when
> you are not sure if the input vector is longer than 1. This seems to
> be a good workaround:
>
> ❯ diag(-1, nrow = 1)
>  [,1]
> [1,]   -1
>
> Or, in general if you have vector v:
>
> ❯ v <- -1
> ❯ diag(v, nrow = length(v))
>  [,1]
> [1,]   -1
> >
>

Anyone else getting deja-vu with the `sample` function?

 > sample(5:3)
 [1] 3 5 4

ok...

 > sample(5:4)
 [1] 4 5

fine...

 > sample(5:5)
 [1] 3 1 5 2 4

uh oh. Documented, of course.

B

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] diag(-1) produces weird result

2018-09-17 Thread Ravi Varadhan
It behaves as per documentation.


" Using diag(x) can have unexpected effects if x is a vector that could be of 
length one. Use diag(x, nrow = length(x)) for consistent behavior."


Ravi



From: R-devel  on behalf of Gábor Csárdi 

Sent: Monday, September 17, 2018 12:22:31 PM
To: li...@revelle.net
Cc: r-devel
Subject: Re: [Rd] diag(-1) produces weird result


I would say it is a mis-feature. If the 'x' argument of diag() is a
vector of length 1, then it creates an identity matrix of that size,
instead of creating a 1x1 matrix with the given value:

❯ diag(3)
 [,1] [,2] [,3]
[1,]100
[2,]010
[3,]001

Of course this makes it cumbersome to use diag() in a package, when
you are not sure if the input vector is longer than 1. This seems to
be a good workaround:

❯ diag(-1, nrow = 1)
 [,1]
[1,]   -1

Or, in general if you have vector v:

❯ v <- -1
❯ diag(v, nrow = length(v))
 [,1]
[1,]   -1

Gabor
On Mon, Sep 17, 2018 at 5:14 PM William Revelle  wrote:
>
> Dear list
>
> A strange bug in the psych package is due to the behavior of the diag 
> function:
>
> It gives the expected values for 1, a vector (-1,1), but not for -1
>
> Is this a known feature?
>
>
> > diag(1)
>  [,1]
> [1,]1
> > diag(c(-1,1))
>  [,1] [,2]
> [1,]   -10
> [2,]01
> > diag(-1)
> Error in diag(-1) : invalid 'nrow' value (< 0)
>
>
> Bill
>
>
> William Revellepersonality-project.org/revelle.html
> Professor personality-project.org
> Department of Psychology 
> www.wcas.northwestern.edu/psych/
> Northwestern Universitywww.northwestern.edu/
> Use R for psychology personality-project.org/r
> It is 2   minutes to midnight   
> www.thebulletin.org
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel