tweag / HaskellR

The full power of R in Haskell.
https://tweag.github.io/HaskellR
Other
584 stars 47 forks source link

Populate R_LIBS environment variable #392

Closed bezirg closed 1 year ago

bezirg commented 1 year ago

This patch allowed the underlying R process see our Nix-installed R libraries. Is this perhaps something worth to upstream?

Note: this patch has only been tested on Linux and Wine (not yet on macosx or native windows). @michaelpj @zeme-iohk

bezirg commented 1 year ago

To explain a bit more on why this is benefical for our Nix usecase:

We are using Nix's rPackages to manage our extra dependencies on R libraries.

Nix is helpful to create a wrapper R executable script to make those R libraries visible by adding them in the library path:

$ nix develop
nix$ cat `which R`

R_LIBS_SITE=${R_LIBS_SITE:+':'$R_LIBS_SITE':'}
R_LIBS_SITE=${R_LIBS_SITE/':''/nix/store/k11dqgj5jmc0h0dnww0a2cgylmi3k8ma-r-class-7.3-20/library'':'/':'}
R_LIBS_SITE='/nix/store/k11dqgj5jmc0h0dnww0a2cgylmi3k8ma-r-class-7.3-20/library'$R_LIBS_SITE
R_LIBS_SITE=${R_LIBS_SITE#':'}
R_LIBS_SITE=${R_LIBS_SITE%':'}
export R_LIBS_SITE
R_LIBS_SITE=${R_LIBS_SITE:+':'$R_LIBS_SITE':'}
R_LIBS_SITE=${R_LIBS_SITE/':''/nix/store/a755qq4n15smih5b3dl0383klvchp1as-r-boot-1.3-28/library'':'/':'}
R_LIBS_SITE='/nix/store/a755qq4n15smih5b3dl0383klvchp1as-r-boot-1.3-28/library'$R_LIBS_SITE
R_LIBS_SITE=${R_LIBS_SITE#':'}
R_LIBS_SITE=${R_LIBS_SITE%':'}
export R_LIBS_SITE
exec "/nix/store/wbx6cid3dks60as8dnry0gw9xnfm7409-R-4.2.1/bin/R"  "$@" 

This script-wrapping method works fine for the "canonical" use of running R through bash, but it breaks for us when we want to use it in inline-r. The problem is that the embedded R inside inline-r is not aware of these extra R libraries installed and their paths.

Using this simple patch, we spawn an external R process and interactively ask the R REPL for the libraries visible. Then we reconstruct an R_LIBS env. variable and pass it back to the haskell process. Then the embedded R can be aware of the extra installed R libraries. This method is the same as what is currently done for the R_HOME env variable.

facundominguez commented 1 year ago

Thanks @bezirg! Looks great. I'm running it in CI in #393.