2

I am setting up a cluster where all nodes have access to /nfs/software, so a good place to install.packages() would be under /nfs/software/R. How do I set R_LIBS_SITE so that this is automatically part of all users' R environment? I tried prepending to the path given for R_LIBS_SITE in /etc/R/Renviron but help(Startup) says "do not change ‘R_HOME/etc/Renviron’ itself", which I'm not sure is the same file since R_HOME expands to /usr/lib/R, but has no effect in any case. Making entries in the various Renviron.site and Rprofile.site files does not seem to have the desired effect. What am I missing here?

Some other questions have danced around this (here and here, maybe others), but people seem to settle for having a user-specific library in their HOME.

Community
  • 1
  • 1
Neil Best
  • 817
  • 9
  • 17
  • It was failing silently because the library folder `/nfs/software/R/i486-pc-linux-gnu-library/2.13` had not been created yet. Editing the `R_LIBS_SITE= `. . . line in /etc/R/Renviron still seems like the way to go. The desired path is the first entry in `.libPaths()` now, but `update.packages()` did its work in `/usr/local/lib/R/site-library`, the second entry. What is going on here? – Neil Best Jun 02 '11 at 18:20
  • I finally realized that I had to log in to the NFS server, set up R there, change the `/etc` files, and install the packages from there because of NFS root squashing. I'm sure Dirk is correct on all points, but I'm moving on with what I have done since it seems to be working. Thanks for letting me document a stream of consciousness here while I work this out! I hope this is helpful to someone. – Neil Best Jun 02 '11 at 19:41

2 Answers2

2

Make sure you have owner and/or group write permissions for the directory you want to write into.

The file /etc/R/Renviron.site is the preferred choice for local overrides to /etc/R/Renviron.

Another way is to simply ... impose the directory when installing packages. I tend to do that on the (bash rather than R) shell via this script derived from an example in the littler package:

$ cat bin/install.r 
#!/usr/bin/env r
#
# a simple example to install one or more packages

if (is.null(argv) | length(argv)<1) {

  cat("Usage: installr.r pkg1 [pkg2 pkg3 ...]\n")
  q()

}

## adjust as necessary, see help('download.packages')
repos <- "http://cran.us.r-project.org"
#repos <- "http://cran.r-project.org"

## this makes sense on Debian where no packages touch /usr/local
lib.loc <- "/usr/local/lib/R/site-library"

install.packages(argv, lib.loc, repos)

and you can easily customize a helper like this for your preferred location. With the script installed in ~/bin/, I often do

$ ~/bin/install.r xts plyr doRedis

and it will faithfully install these packages along with their depends. The littler package has a similar script update.r.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • after all of that I tried `littler` for the first time and I get this: `$ r -e "print(.libPaths())" [1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library" [3] "/usr/lib/R/library"` Does `r` start up differently in this respect? – Neil Best Jun 03 '11 at 17:31
  • On a system where I do not have `~/R/library/`, `r` and `Rscript` give the same result. On the other hand, where `~/R/library/` exists. `Rscript` adds it to the front. There may be new startup code in `R` which `r` does not do. I tend not to use `~/R/library/` so current behaviour is fine by me. – Dirk Eddelbuettel Jun 03 '11 at 17:54
  • Note, setting R_LIBS_SITE in `/etc/R/Renviron.site` means the setting won't be respected if R is started with a `--vanilla` flag. If you want to set the default `R_LIBS_SITE` more permanently (so the setting persists even with `--vanilla`), , the only options are to set it at outside of R (e.g., `/etc/bashrc`), modify `/etc/R/Renviron` (against official recommendations, but seemingly harmless), or to build R from source with the site-lib specified, perhaps thought `--prefix=` or similar. – t-kalinowski Oct 25 '19 at 02:12
  • I think you misunderstand what `--vanilla` is for: it is meant to skip the init files, and generally just a debugging too. Setting in options in the different `*.site` is the recommended way. – Dirk Eddelbuettel Oct 25 '19 at 02:14
0

follow-up on Dirk Eddelbuettel (thanks Dirk!)

an adaptation of Dirk's suggestion that may be run within R:

# R function to install one or more packages
Rinstall <- function(pkg) {
  if (is.null(pkg) | length(pkg)<1) {
    q()
  }
  if(.Platform$OS.type == "windows") {
  lib.dir <- "c:/R/library"
  } else {
  lib.dir <- "~/R/library"
  }
  repos.loc <- "http://cran.us.r-project.org"
  install.packages(pkg, lib.dir, repos.loc, dependencies=c('Depends','Suggests'))  # make sure you get dependencies
}

Usage:

Rinstall(c("package1", "package2"))

Naturally you want to adapt the repos.loc and lib.dir based on your system. As I work on both Windows and Linux machines I also inserted a conditional statement to check which system I'm on.

P.S. Don't hesitate to simplify the code, I'm a total newbie.

PatrickT
  • 10,037
  • 9
  • 76
  • 111