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"))
For completeness, and as I can't show this in a comment:
-
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> -
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>
-
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:
- Alter the system files such as
Renviron
orRenviron.site
if you have the proper permissions - Else alter the per-user file
~/.Renviron
- Alternatively, alter the environment variables
R_LIBS
orR_LIBS_USER
orR_LIBS_SITE
at the system level if you have the proper permissions - Else alter the variables
R_LIBS
orR_LIBS_USER
orR_LIBS_SITE
at the user level. - 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.
2 major solutions:
In
.Rprofile
you could leave code that sets.libPaths()
-
Just set the appropriate environment variable such as
R_LIBS
in Windows orR_LIBS_SITE=
in Linux, etc, to whatever you like, e.g.R_LIBS=C:/R/Library
find the Rprofile
file (no dot) in R installation folder
find -name Rprofile
#Getting something like:
#lib/R/library/base/R/Rprofile
Open the file and search for:
invisible(.libPaths(c(unlist(strsplit(Sys.getenv("R_LIBS"), ":")),
unlist(strsplit(Sys.getenv("R_LIBS_USER"), ":") )
)))
comment other paths and add your desired path
invisible(.libPaths(c(#unlist(strsplit(Sys.getenv("R_LIBS"), ":")),
#unlist(strsplit(Sys.getenv("R_LIBS_USER"), ":") )
"/home/users/username/R/4.0.1/lib/R/library" # desired path
)))
This can be useful also: (for .bashrc
file)
export R_LIBS='/home/users/username/R/4.0.1/lib/R/library'
export R_LIBS_USER='/home/users/username/R/4.0.1/lib/R/library'