Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automatically create personal library in R

Tags:

r

rscript

When you try to install a package in R and you don't have access rights to the default library path, R will ask you:

Would you like to use a personal library instead?

Would you like to create a personal library '~/path' to install packages into?

However, if you are running an Rscript, those messages will not show up and installation will fail. I could predefine a specific path and instruct install.packages to use it, but I don't want to create an additional library path that would be specific to this Rscript. I just want to use the default personal library. Is there a way to force creation of a personal library without requiring interaction?

like image 739
burger Avatar asked Oct 05 '16 16:10

burger


People also ask

How do you set a library in R?

R uses a single package library for each installed version of R on your machine. Fortunately it is easy to modify the path where R installs your packages. To do this, you simply call the function . libPaths() and specify the library location.

What is libPaths in R?

libPaths is used for getting or setting the library trees that R knows about and hence uses when looking for packages (the library search path). If called with argument new , by default, the library search path is set to the existing directories in unique(c(new, . Library. site, . Library)) and this is returned.


1 Answers

You can use Sys.getenv("R_LIBS_USER") to get the local library search location.

This is what I ended up doing, which seems to be working (the hardest part was testing the solution, since the problem only occurs the first time you try to install a package):

# create local user library path (not present by default)
dir.create(path = Sys.getenv("R_LIBS_USER"), showWarnings = FALSE, recursive = TRUE)
# install to local user library path
install.packages(p, lib = Sys.getenv("R_LIBS_USER"), repos = "https://cran.rstudio.com/")
# Bioconductor version (works for both Bioconductor and CRAN packages)
BiocManager::install(p, update = FALSE, lib = Sys.getenv("R_LIBS_USER"))

As @hrbrmstr pointed out in the comments, it may not be a good idea to force-install packages, so use at your own risk.

like image 131
burger Avatar answered Nov 14 '22 18:11

burger