I want to execute a script file.R
using Rscript
. In file.R
, I use the package dplyr
.
# file.R
df <- data.frame(ID,x,y,z,...)
library(dplyr)
filter(df, ID != "null")
......
If I don't specify any options in the batch-file, everything works fine as 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
in 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 part of file.R
using dplyr
doesn't work anymore (Error in filter(df, ID != 'null') : Object 'ID' could not be found
)
Since ?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 is batch file 1
the only one that works? I am calling the same R script in all 3 alternatives.