Rscript and packages: how and when to determine which packages are downloaded?

I want to execute script file.R using Rscript . In file.R I am using the dplyr package.

 # file.R df <- data.frame(ID,x,y,z,...) library(dplyr) filter(df, ID != "null") ...... 

If I do not specify any parameters in the batch file, everything works fine, since file.R includes the line library(dplyr)

 # 1) no specification of packages in the batch file Rscript.exe file.R arg1 arg2 arg3 > outputFile.Rout 2>&1 

However, if I add default-packages=utils to the batch file,

 # 2) specification of packages utils in the batch file Rscript.exe default-packages=utils file.R arg1 arg2 arg3 > outputFile.Rout 2>&1 

the file.R part using dplyr no longer works ( Error in filter(df, ID != 'null') : Object 'ID' could not be found )

So how ?Rscript says

 --default-packages=list where list is a comma-separated list of package names or NULL 

I tried adding --default-packages=utils,dplyr ,

 # 3) specification of packages utils and dplyr in the batch file Rscript.exe default-packages=utils,dplyr file.R arg1 arg2 arg3 > outputFile.Rout 2>&1 

which causes the same error as in 2

Why does batch file 1 only work one? I call the same R script in all 3 alternatives.

+5
source share
3 answers

The --default-packages specifies the packages that you want to download by default. It does not add default packages to the list - it replaces the list. This means that you need to point out all the other base packages that you also rely on. You can see this by running a simple script test that calls sessionInfo()

In the file "env.R":

 sessionInfo() 

Call from terminal: Rscript env.R

 R version 3.1.2 (2014-10-31) Platform: x86_64-w64-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets base 

Now I am changing this call: Rscript --default-packages=utils env.R

 R version 3.1.2 (2014-10-31) Platform: x86_64-w64-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] utils base 

Therefore, you need to specify other missing packages.

 RScript --default-packages=stats,graphics,grDevices,utils,datasets,base,methods env.R 

and I threw methods there too.

With that said, if you had no problems when you just ran it using RScript, I don’t understand why you are trying to combine with the default argument. It seems that you are simply creating problems for yourself, if there are no other problems that you are trying to solve, that you are not telling us.

+6
source

To complete and illustrate my comment, Charles’s example works for me on a single line using littler . I turn it off here only for exposure:

 edd@max :~$ r -ldplyr -e'iris %>% \ group_by(Species) %>% \ summarise(mean(Sepal.Length)) %>% \ print' Source: local data frame [3 x 2] Species mean(Sepal.Length) 1 setosa 5.006 2 versicolor 5.936 3 virginica 6.588 edd@max :~$ 

As I said, this is really one line (and one difference is that r wants to explicitly print ).

But, as you can see, the datasets package also automatically loads r .

+4
source

Can you try the next test? I can not write this in the comments. This works fine on my system.

test.r

 library(dplyr) data(iris) iris %>% group_by(Species) %>% summarise(mean(Sepal.Length)) 

In terminal:

Rscript --default-packages=utils,datasets,dplyr test.R

0
source

All Articles