17

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?

burger
  • 5,683
  • 9
  • 40
  • 63
  • 1
    Update `.libPaths()` ? – zx8754 Oct 05 '16 at 16:40
  • 4
    You should fully read `help(".libPaths")` that the help on `install.packages` provides a link to. _"The library search path is initialized at startup from the environment variable `R_LIBS` (which should be a colon-separated list of directories at which R library trees are rooted) followed by those in environment variable `R_LIBS_USER`. Only directories which exist at the time will be included. By default `R_LIBS` is unset, and `R_LIBS_USER` is set to directory `‘R/R.version$platform-library/x.y’` of the home directory (or `‘Library/R/x.y/library’` for CRAN OS X builds), for R x.y.z.`_ – hrbrmstr Oct 05 '16 at 17:37
  • 6
    (ran out of space) Having said that, I'm not a big fan of an analysis script actually installing packages. I may also be in the minority and also be a compulsively suspicious cybersecurity dude as well, but I'd rather there be a pkg install script that I hand run or a preflight check in a script that then informs me what i am missing, especially if a script relies on an older or newer version of a package and i'm running a particular version for a reason. – hrbrmstr Oct 05 '16 at 17:41
  • It's fine to have a install script that runs beforehand in a separate context than the analysis script. But if that install script is itself fully automated, there needs to be a straightforward way to have that install user (non-root) packages without needing any manual interaction. For example `pip` has the trivial `user` flag that can be passed to tell it to use the current user's home directory instead of a system directory. `install.packages` already has the functionality to do this and determine where user packages should go. It should have a `user` optional arg that I can set to `TRUE`. – theferrit32 Jun 13 '19 at 21:55
  • The way I'm working around it now is to have the install script create `~/.local/lib/R`, and then run R with `R_LIBS_USER=~/.local/lib/R R` – theferrit32 Jun 13 '19 at 22:08
  • 1
    I disagree with the premise that R scripts are necessarily analysis scripts. I think there are many situations where we may want to write a priming script in R that handles dependencies – Shape Oct 07 '20 at 09:52

1 Answers1

16

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.

burger
  • 5,683
  • 9
  • 40
  • 63
  • 1
    Note that biocLite should no longer be used to install packages from Bioconductor. Use `BiocManager::install()` – llrs Jan 28 '21 at 11:45
  • Important to note: While you *can* set `R_LIBS_USER` in your environment, and `R` will respect that, `Sys.getenv("R_LIBS_USER")` *will* return something sensible even if `R_LIBS_USER` is *not* set in your environment. – bers May 30 '22 at 10:27