Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

How to Change .libPaths() permanently in R?

Whenever I change the library path order using the .libPaths() function, it reverts back to the default if I restart R. How can I change this permanently? I am working on a Linux computing cluster (I don't have admin rights) so, I want to add my local library to R permanently.

I have to do this every time I start R

.libPaths(c("/home/...","/home...","/local/library"))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For completeness, and as I can't show this in a comment:

  1. Default on all Debian and Ubuntu systems with the shipped R package:

    edd@max:~$ R -q -e 'print(.libPaths())' R> print(.libPaths()) [1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library" [3] "/usr/lib/R/library"
    R> R>

  2. Which we can alter by modifying R_LIBS_SITE:

    edd@max:~$ R_LIBS_SITE="/usr/lib/R/Library" R -q -e 'print(.libPaths())' R> print(.libPaths()) [1] "/usr/lib/R/library" R> R>

  3. But modifying R_LIBS does not work:

    edd@max:~$ R_LIBS="/usr/lib/R/Library" R -q -e 'print(.libPaths())' R> print(.libPaths()) [1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library" [3] "/usr/lib/R/library"
    R> R> edd@max:~$

See help(Startup) for the full and detailed treatment. On Debian and Ubuntu we have been setting these three directories as the default for well over a decade. As it is set via R_LIBS_SITE here, this is the variable you need to alter here. In general, you need to override the variable holding the value and you may not know ex ante which one that is.

As for the original answer, on Debian and Ubuntu we use the file /etc/R/Renviron. As help(Startup) details, you can set any number of ways to alter this permanently for your startup -- and all these points hold for all different OSs:

  1. Alter the system files such as Renviron or Renviron.site if you have the proper permissions
  2. Else alter the per-user file ~/.Renviron
  3. Alternatively, alter the environment variables R_LIBS or R_LIBS_USER or R_LIBS_SITE at the system level if you have the proper permissions
  4. Else alter the variables R_LIBS or R_LIBS_USER or R_LIBS_SITE at the user level.
  5. Lastly, call .libPaths(...new path to be added here...) in your R startup files as e.g. in .Rprofile.

Do see help(Startup) for a fuller-length discussion.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...