0

How can I find the absolute path of the personal R package library for a certain user on a Unix system (including Linux, OSX, Solaris)? To get the home path of the current user, we can use:

path.expand(Sys.getenv("R_LIBS_USER"))

However, I would like to lookup the path for another user, given that the current user has sufficient privileges to do so. It should work, even if the user directories are not in the default /home.

Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207
  • Getting an environment variable of another user is difficult to do robustly, because it involves simulating their login, which involves reading through a number of init files such as .profile or .bashrc, possibly involving running programs whose output is captured and parsed by those init files. Essentially, you want to run the Unix command `su - user -c 'printenv R_LIBS_USER'` and look at its output for a line that looks like `R_LIBS_USER=PPP`, where PPP is a pathname. – Mark Plotnick Nov 10 '13 at 00:30
  • OK that makes sense. What if we assume the user has no custom profile or init scripts, and simply uses the system wide defaults? – Jeroen Ooms Nov 10 '13 at 01:23
  • 1
    I don't know where your R system puts user libraries by default, but once you know that, do something like `libdir="/R/library"; paste(path.expand("~otheruser"),libdir,sep="")` – Mark Plotnick Nov 10 '13 at 14:21
  • Great, I didn't know about `~otheruser`. – Jeroen Ooms Nov 10 '13 at 18:57

1 Answers1

0

Based on Mark's suggestions, the best I've come up with is:

userlib <- function(username){
  userlib <- path.expand(sub("~", paste0("~", username), Sys.getenv("R_LIBS_USER"), fixed=TRUE));
  if(file.exists(userlib)){
    return(userlib)
  } else {
    return("")
  }
}

This seems to give what I need, at least on Linux and OSX.

Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207