How to specify (non-R) library path for dynamic library loading in R?

China☆狼群 提交于 2019-11-28 23:17:44

Normally, the iconv method is picked up from glibc, which is linked to during build of the R packages in question. For whatever reason, however, iconv is getting resolved to libiconv in this case, but it is not linked by the R packages during build.

Original Workaround

One can make the linking to libiconv explicit by adding the following line to the haven/src/Makevars source file

PKG_LIBS=-liconv

which then let's you install from source R CMD INSTALL haven. However, editing packages feels hacky, plus this is something that will need to be done every upgrade, which sounds like a hassle.

Cleaner Workaround

Another option is to use withr::with_makevars, which allows one to temporarily control Makevars content. With this technique, one can install directly from the repo:

withr::with_makevars(c(PKG_LIBS="-liconv"), install.packages("haven"), assignment="+=")

Credit: @knb suggested that I inspect the readxl.so with ldd and this turned out to be super useful because it showed that the shared object wasn't even trying to link to libiconv. Knowing that, I realized I could manually add the reference via the -liconv flag. Thanks @knb!

Additional Info

On the package side of things, relevant details about connecting libraries to R packages can be found in the guide for building libraries. On the system configuration side, the R-admin guide has some useful sections.

Are you running the code in RStudio Server? If so, the answer here may be useful.

I used to meet a similar error while loading dynamic library. The library was in a path contained in LD_LIBRARY_PATH. When I ran the code in R console, it could load the dynamic library correctly. But when I ran it in RStudio, the same error in your post raised.

The reason is that RStudio Server has its own library search path environment. You should specify the following configuration in /etc/rstudio/rserver.conf:

rsession-ld-library-path=/usr/lib64/:/usr/local/lib/:OTHER_PATH_OF_YOUR_LIB

Restart RStudio Server and the error should be fixed.

These libraries really should be standard on RH-based system too, and be found.

If you must add them to R, you have to do so before you start R. One way is via LD_LIBRARY_PATH, a better way is to edit a file in /etc/ld.so.conf.d/ (assuming RH/CentOS have that too). Else maybe via /etc/environment.

Edit: If /etc/ is out of reach, you can do everything below $HOME. Standard shell instantiation works, and R has its own .Rprofile and .Renviron. You can have those below $HOME for all your projects, and/or in a per-project directory---see help(Startup).

Did you install R via rpm or compile it yourself?

solution 1

If you have the permission to modify the R executable (shell script), you can try this:

Edit ~/.local/bin/R or /usr/local/bin/R or /usr/bin/R

#!/bin/bash
# Shell wrapper for R executable.

export LD_LIBRARY_PATH="<my_lib_Path>/readxl/libs/"
R_HOME_DIR=...
...
...

Solution 2

Or you may vim ~/.local/bin/R

#!/bin/bash
# Shell wrapper for R executable.

export LD_LIBRARY_PATH="<my_lib_Path>/readxl/libs/"

/usr/bin/R

then add ~/.local/bin to your PATH

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!