我想使用 Rscript
执行一个脚本文件 file.R
。在 file.R
中,我使用了 dplyr
包。
# file.R
df <- data.frame(ID,x,y,z,...)
library(dplyr)
filter(df, ID != "null")
......
如果我在批处理文件中没有指定任何选项,一切都会正常工作,因为file.R
包含了library(dplyr)
这一行。
# 1) no specification of packages in the batch file
Rscript.exe file.R arg1 arg2 arg3 > outputFile.Rout 2>&1
然而,如果我在批处理文件中添加
default-packages=utils
,# 2) specification of packages utils in the batch file
Rscript.exe default-packages=utils file.R arg1 arg2 arg3 > outputFile.Rout 2>&1
使用dplyr
的file.R
部分不再起作用(Error in filter(df, ID != 'null') : Object 'ID' could not be found
)。
由于?Rscript
显示
--default-packages=list
where list is a comma-separated list of package names or NULL
我试图添加
--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
这是因为批处理文件1
是唯一有效的,而在另外两个选项中会出现与2
相同的错误。
为什么只有批处理文件1
有效?我在所有三个选项中都调用了同样的R脚本。
r
命令。我经常使用-lpkg1,pkg2,pkg3
开关来加载多个必需的包。它还默认加载methods
,而且启动速度比Rscript
还要快。 - Dirk Eddelbuettel