Home > Enterprise >  Sharing R library's between linux subsystem for windows and windows
Sharing R library's between linux subsystem for windows and windows

Time:09-21

I have a window ubuntu VM. I have R installed in the windows. I recently tried to call an R script in my ubuntu but it told me the library is not installed. Is there a way to tell ubuntu's R installation to use my windows R libraries so I don't have to reinstall them all on ubuntu?

StratifiedFullModel/SecondRun$ Rscript StratifiedFullModels.R
Loading required package: car
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages(x, dependencies = TRUE) :
  'lib = "/usr/local/lib/R/site-library"' is not writable
Error in install.packages(x, dependencies = TRUE) :
  unable to install packages
Calls: lapply -> FUN -> install.packages
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘car’
Execution halted

This is strange because the script is set up to check for packages, one of which is car, and either load them or install them. I suspect it is not doing so because of some problem with permissions.

This is problematic because I may want to write scripts that just auto install packages without me having to manually do it which would be time consuming if I share a script with a colleague or if I transfer work to a new machine.

Edit: Tried turing R on in ubuntu and running install.packages("car") returned the following error:

> install.packages("car")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("car") :
  'lib = "/usr/local/lib/R/site-library"' is not writable
Would you like to use a personal library instead? (yes/No/cancel) cancel
Error in install.packages("car") : unable to install packages
>

I just cancelled the install. I think this confirms my suspicion that there is an issue with the permissions.

CodePudding user response:

Note that this answer is based on my experience with WSL rather than R specifically. I'd welcome updates or more authoritative answers from anyone who has used R under WSL.

I believe you are going to run into problems if you attempt to use, at least, some Windows R libraries from WSL. A 'pure-R' library should work, in theory, but it looks to me from some quick searches that R can also include compiled code which would be platform-specific. In this case, if any libraries end up in native-code, then a Windows library is going to have issues when called from Linux with Linux path structures (e.g. /home instead of C:\Users), processes, and other OS constructs.

As for the permissions issue, R is using a default library directory (under /usr/local/lib/R/site-library) that is only writable by root. It appears that you can change the library installation directory with something like:

> install.packages("car", lib="/home/<username>/.local/lib/R/site-library/")

Of course, you'll need to create that directory first. See enter image description here

  • Related