[R] non-interactive subset, `$`, and quotes

Duncan Murdoch murdoch.duncan at gmail.com
Fri Jun 27 04:25:21 CEST 2014


On 27/06/2014, 4:08 AM, Benjamin Tyner wrote:
> Hi
> 
> I know that subset() is not intended for use in programming. However I
> am still curious to learn why, in non-interactive mode, if I take away
> the quotes around 'bar'
> 
>    Rscript -e "foo <- list(bar = iris); head(subset(foo$'bar',
> Species=='setosa'))"
>      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
>    1          5.1         3.5          1.4         0.2  setosa
>    2          4.9         3.0          1.4         0.2  setosa
>    3          4.7         3.2          1.3         0.2  setosa
>    4          4.6         3.1          1.5         0.2  setosa
>    5          5.0         3.6          1.4         0.2  setosa
>    6          5.4         3.9          1.7         0.4  setosa
> 
> then it results in an error:
> 
>    Rscript -e "foo <- list(bar = iris); head(subset(foo$bar,
> Species=='setosa'))"
>    Error in subset.default(foo, Species == "setosa") :
>      object 'Species' not found
>    Calls: head -> subset -> subset.default
>    Execution halted
> 
> and also why this does not occur in interactive mode? My sessionInfo() is
> 
>    R Under development (unstable) (2014-06-26 r66043)
>    Platform: x86_64-unknown-linux-gnu (64-bit)
> 
>    locale:
>     [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C             
>     [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8   
>     [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8  
>     [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                
>     [9] LC_ADDRESS=C               LC_TELEPHONE=C           
>    [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C      
> 
>    attached base packages:
>    [1] stats     graphics  grDevices utils     datasets  methods   base
> 

Try replacing "Rscript -e" with "echo" to see what your shell is really
sending as a command to execute.   I suspect it is treating $bar as a
shell variable (see the error message, it has disappeared).



More information about the R-help mailing list