Rscript:没有名为...的软件包吗? [英] Rscript: There is no package called ...?

查看:321
本文介绍了Rscript:没有名为...的软件包吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Rscript在批处理模式下运行R文件,但是它似乎并未加载我需要的库.我得到的具体错误是:

I want to run R files in batch mode using Rscript, however it does not seem to be loading the libraries that I need. The specific error I am getting is:

Error in library(timeSeries) : there is no package called 'timeSeries'
Execution halted

但是我确实有软件包timeSeries,可以从命令行从Rstudio,RGui和R加载它.问题似乎仅是在使用Rscript运行脚本时出现的.

However I do have the package timeSeries and can load it from Rstudio, RGui, and R from the command line no problem. The issue seems to only be when running a script using Rscript.

我的系统/环境变量配置为:

My system/environment variables are configured as:

C:\Program Files\R\R-3.1.0\bin\x64 (Appended to PATH)
R_HOME = C:\Program Files\R\R-3.1.0
R_User = Patrick

我正在RStudio,RGui和R中从命令行运行相同版本的R.我还从这三个来源检查了.Library,并获得了相同的输出.

I am running the same version of R in RStudio, RGui, and R from command line. I've also checked .Library from these three sources and got the same output as well.

如何使用我正在R中使用(并已安装)的软件包从命令行运行Rscript?

How can I run Rscript from command line with the packages that I am using (and have installed) in R?

我正在通过Rscript script.rscript.r所在目录的Windows命令行中使用Rscript.

I am using Rscript via Rscript script.r at the windows command line in the directory where script.r is located.

Rscript -e print(.Library)的输出是[1] "C:/PROGRA~1/R/R-31~1.0/library"

与我提到的其他三个选项一致:[1] "C:/PROGRA~1/R/R-31~1.0/library"

which is consistent with the other three options that I mentioned: [1] "C:/PROGRA~1/R/R-31~1.0/library"

但是,如果我将其放在脚本中:

However, if I put this in my script:

print(.libPaths()) 
library(timeSeries) #This is the package that failed to load

我得到的输出是:

[1] "C:/Program Files/R/R-3.1.0/library"
Error in library(timeSeries) : there is no package called 'timeSeries'
Execution halted

RStudio中的相应调用提供了实际安装软件包的其他路径:

The corresponding call in RStudio gives an additional path to where the package is actually installed:

> print(.libPaths())
[1] "C:/Users/Patrick/Documents/R/win-library/3.1" "C:/Program Files/R/R-3.1.0/library"    

推荐答案

简而言之,在R.exe中调用Sys.getenv('R_LIBS_USER')返回的值必须与在命令行中调用此返回的值相同:

In short, the value returned by calling Sys.getenv('R_LIBS_USER') in R.exe needs to be the same as the value returned by calling this at the command line:

Rscript.exe -e "Sys.getenv('R_LIBS_USER')"

上述值需要包含在此命令行调用中:

and the above value needs to be included in this command line call:

Rscript.exe -e ".libPaths()"

请注意,如果更改了R_USER的值,则R_LIBS_USER的值在R.exe和Rscript.exe之间可能会有所不同,可以在 > ,总的来说,我发现用户库(即.libPaths()[2])根本没有在Rscript.exe中设置

Note that the values of R_LIBS_USER may be differ between R.exe and Rscript.exe if the value of R_USER is changed, either in the .Rprofile or the in target field of user's shortcut to R.exe, and in general, I find that the user library (i.e. .libPaths()[2]) is simply not set in Rscript.exe

由于我喜欢将R_USER设置为USERPROFILE,因此我希望在多种计算机或Rscript.exe的.Rprofile中运行的.R文件顶部包含以下代码块(即Rscript -e "path.expand('~/.Rprofile')"):

Since I'm fond of setting R_USER to my USERPROFILE, I include the following block in at the top of .R files that I wish to run on mulitiple computers or in Rscript.exe's .Rprofile (i.e. Rscript -e "path.expand('~/.Rprofile')"):

# =====================================================================
# For compatibility with Rscript.exe: 
# =====================================================================
if(length(.libPaths()) == 1){
    # We're in Rscript.exe
    possible_lib_paths <- file.path(Sys.getenv(c('USERPROFILE','R_USER')),
                                    "R","win-library",
                                    paste(R.version$major,
                                             substr(R.version$minor,1,1),
                                             sep='.'))
    indx <- which(file.exists(possible_lib_paths))
    if(length(indx)){
       .libPaths(possible_lib_paths[indx[1]])
    }
    # CLEAN UP
    rm(indx,possible_lib_paths)
}
# =====================================================================

这篇关于Rscript:没有名为...的软件包吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆