R/shiny加载多个library目录内的库导致版本错误

问题描述:

最近在做 R/shiny 的项目,electron 配合 R 语言和 shiny 移植打包成一个桌面app;R 使用 4.0.5 版本,其中有个画图相关库 gg 需要使用 0.8.2版本的。在打包时,把R的整个程序目录都复制到electron 的资源目录中,做成在其他 Windows 上直接安装我们的软件包就可以使用,不用用户自己手动安装R环境。

这在多数用户那都没问题,但是,发现某用户本地自己安装了R,然后设置了环境变量R_LIBS_USERR_LIBS_SITE,而他本地的R的 gg 却是 0.7.x 版本的,所以我们的软件就没加载 0.8.2的库导致报错。

官方文档解释:

 

6.2 Managing libraries

R packages are installed into libraries, which are directories in the file system containing a subdirectory for each package installed there.

R comes with a single library, R_HOME/library which is the value of the R object ‘.Library’ containing the standard and recommended packages. Both sites and users can create others and make use of them (or not) in an R session. At the lowest level ‘.libPaths()’ can be used to add paths to the collection of libraries or to report the current collection.

R will automatically make use of a site-specific library R_HOME/site-library if this exists (it does not in a vanilla R installation). This location can be overridden by setting ‘.Library.site’ in R_HOME/etc/Rprofile.site, or (not recommended) by setting the environment variable R_LIBS_SITE.

Users can have one or more libraries, normally specified by the environment variable R_LIBS_USER. This has a default value (to see it, use ‘Sys.getenv("R_LIBS_USER")’ within an R session), but that is only used if the corresponding directory actually exists (which by default it will not).

Both R_LIBS_USER and R_LIBS_SITE can specify multiple library paths, separated by colons (semicolons on Windows).

 用户可以通过添加 R_LIBS_USER 和 R_LIBS_SITE 环境变量,也可以调用 .libPaths 函数,还可以直接修改 .Library.site 对象变量来修改添加 library 库的加载路径。

解决办法:

process.env.R_HOME = path.join(rootPath, "R_portable");// R 程序文件目录
process.env.R_LIBS_SITE = '';
process.env.R_LIBS_USER = '';

在软件启动 R 进程前重置 R_HOME、R_LIBS_USER 和 R_LIBS_SITE 环境变量,前者设置为软件中的 R 路径,后两者可直接设为空。但是 R 似乎还会默认给 R_LIBS_USER 赋值,所以我们在 (R_HOME)/etc/Rprofile.site 文件中再加一句

Sys.setenv(R_LIBS_USER = "")

这样就不会再加载其他地方,以及用户本地自己安装的 R 的 library 库了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值