使用renv时,Rscript无法识别库

使用renv时,Rscript无法识别库,r,renv,R,Renv,我正在MacOSX上工作,使用R的v3.6.3和renv。在Rstudio和R中,我可以加载已安装软件包的库,例如library(ggplot2)works。但是,当我使用Rscript运行脚本时,我会收到消息 Error in library(ggplot2) : there is no package called ‘ggplot2’ 根据这一点,我需要确保 Sys.getenv('R_LIBS_USER') in R.exe 与的值相同 Rscript.exe -e ".libPath

我正在MacOSX上工作,使用R的v3.6.3和renv。在Rstudio和R中,我可以加载已安装软件包的库,例如
library(ggplot2)
works。但是,当我使用Rscript运行脚本时,我会收到消息

Error in library(ggplot2) : there is no package called ‘ggplot2’
根据这一点,我需要确保

Sys.getenv('R_LIBS_USER') in R.exe
与的值相同

Rscript.exe -e ".libPaths()"
但值相同,都指向我的项目文件夹中的
renv系统库


那么如何解决这个问题呢?

最好在
调用中指定
lib.loc

library(ggplot2, lib.loc = '/path/where/library/is/located')

我设法解决了这个问题。我的答案很有用,它不起作用,但给我指明了错误的方向。答案不起作用,因为使用它时,我收到以下错误:

    Error: package or namespace load failed for ‘ggplot2’:
 .onLoad failed in loadNamespace() for 'pillar', details:
  call: utils::packageVersion("vctrs")
  error: there is no package called ‘vctrs’
现在
vctrs
位于
'/path/where/library/is/location'
中,因此我认为依赖包不是从该路径加载的,而是从Rscript的默认路径加载的。在脚本中放置
打印(.libPaths()
,得到

"/usr/local/Cellar/r/3.6.3_1/lib/R/library"
而不是

[1] "/Users/Chris/Sites/app_name/renv/library/R-3.6/x86_64-apple-darwin18.7.0"
[2] "/private/var/folders/5_/p_yl0439059b7_jdqzrm0hr40000gr/T/RtmptdHcWN/renv-system-library"
对于Rstudio中的
.libpath()
,查看实际运行Rscript程序的ruby程序,我发现它是使用--vanilla选项运行的,即

Rscript --vanilla script_name
删除--vanilla选项修复了该问题。我认为带有--vanilla选项的脚本停止工作,因为我使用brew重新调用了R以修复我遇到的另一个问题,并且作为此命令的一部分,发出了以下命令:

brew link --overwrite r 

可能是
.libPaths()
顺序有问题。您能从RScript打印第一条路径吗?我尝试了,但收到错误消息
在'lib.loc'
@Obromios中找不到库树,这很奇怪。您能检查
.libPaths()吗
,它可能不止一个。通常,首先检查本地的一个文件包是否打印出“```/Users/Chris/Sites/app\u name/renv/library/R-3.6/x86\u 64-apple-darwin18.7.0”[2]“/private/var/folders/5\u/p\u yl0439059b7\u jdqzrm0hr40000gr/T/RtmptdHcWN/renv系统库”```,这与我输入lib.locok的内容相同,您是否可以检查安装了ggplot2中的哪一个。当您从没有loc的脚本调用时,它可能正在检查不同的location@Obromios这也可能是由于许可。