【问题标题】:Rscript is not recognising libraries when using renv使用 renv 时,Rscript 无法识别库
【发布时间】:2020-07-26 12:45:47
【问题描述】:

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

Error in library(ggplot2) : there is no package called ‘ggplot2’

根据这个 SO answer,我需要确保的值

Sys.getenv('R_LIBS_USER') in R.exe

的值相同
Rscript.exe -e ".libPaths()"

但是值是一样的,都是指向我项目文件夹中的renv-system-library

那么我该如何解决这个问题?

【问题讨论】:

  • .libPaths() 订单可能是问题所在。你能打印Rscript的第一条路径吗

标签: r renv


【解决方案1】:

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

    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/located' 中,所以我认为依赖包不是从该路径加载的,而是 Rscript 的默认值。在脚本中添加print(.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 中的 .libPaths()。查看实际运行 Rscript 程序的 ruby​​ 程序,我发现它正在使用 --vanilla 选项运行,即

Rscript --vanilla script_name

删除 --vanilla 选项解决了这个问题。我认为带有 --vanilla 选项的脚本停止工作,因为我使用 brew 重新安装了 R 以解决我遇到的另一个问题,并在其中发出了以下命令:

brew link --overwrite r 

【讨论】:

    【解决方案2】:

    library调用中指定lib.loc可能会更好

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

    【讨论】:

    • 我试过了,但收到错误消息no library trees found in 'lib.loc'
    • @Obromios 这很奇怪。你能检查一下.libPaths(),它可能不止一个。通常,首先检查本地的包
    • 打印出 ``` "/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"```,和我输入lib.loc的一样
    • 好的,你能检查一下 ggplot2 安装在其中的哪一个中吗?当您从没有 loc 的脚本调用时,它可能会检查不同的位置
    • @Obromios 这也可能是由于许可。
    猜你喜欢
    • 2021-05-24
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 2022-09-22
    • 2014-01-28
    • 2011-10-25
    • 2015-10-31
    • 2022-01-08
    相关资源
    最近更新 更多