42

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"))
6
  • 13
    Put it in your .Rprofile Commented Jun 24, 2014 at 13:18
  • oh.. such a simple thought. thanks
    – Koundy
    Commented Jun 24, 2014 at 13:20
  • The solution by @JoshuaUlrich does not work for people who do not have administrative rights on their computer.
    – Therkel
    Commented Apr 14, 2016 at 8:55
  • 1
    @Therkel: that's incorrect. This worked for the OP and they explicitly said they do not have admin rights. Nothing about putting .libPaths in your .Rprofile requires admin rights. Commented Apr 14, 2016 at 11:23
  • Yes you do not require any admin rights. Just keep the .Rprofile file in your default working directory.
    – Koundy
    Commented Apr 14, 2016 at 12:04

4 Answers 4

20

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.

4
  • Personally, I find help(Startup) to be more confusing than helpful in this instance.
    – Brian D
    Commented Jun 9, 2020 at 19:44
  • 2
    I was not claiming it is easy reading as there are a lot of moving parts to consider, and it does that. But it is comprehensive. Which is really all I said. Commented Jun 9, 2020 at 20:07
  • @DirkEddelbuettel can you please refer a similar thing for windows ?Thanks
    – ok1more
    Commented Oct 1, 2020 at 15:22
  • I showed R commands, and those are naturally portable across operating systems. If you thought this was only for Linux or macOS you were mistaken. See help(.libPaths) and help(Startup) in your R session, and maybe look for some tutorials or blog on "customizing your R environment". Windows users do it the same way. Commented Oct 1, 2020 at 15:26
11

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'
1
  • It is preferable to edit Rprofile.site instead -- it is meant for site-local changes. This can matter during upgrades. Commented Aug 18, 2020 at 14:14
5

2 major solutions:

  1. In .Rprofile you could leave code that sets .libPaths()

  2. Just set the appropriate environment variable such as R_LIBS in Windows or R_LIBS_SITE= in Linux, etc, to whatever you like, e.g.

     R_LIBS=C:/R/Library 
    
5
  • @DirkEddelbuettel What's the situation where that would be the case? I'm really just quoting stat.ethz.ch
    – Hack-R
    Commented Jul 17, 2016 at 1:38
  • Any Debian or Ubuntu box. Commented Jul 17, 2016 at 1:38
  • @DirkEddelbuettel Oh I explicitly specified Windows
    – Hack-R
    Commented Jul 17, 2016 at 1:39
  • You can (should, even) also remove the worrds ".. in Windows" up there as your answer is correct without it. Commented Jul 17, 2016 at 1:39
  • I didn''t ask you to qualify, just remove the per-OS settings. Both OS will listen to both variables, see help(Startup). Commented Jul 17, 2016 at 1:43
1

On Mac, I find that in a 'fresh' installation there is no user library and packages are all installed in the version-specific system library. Eventually, a user library is created (possibly accidentally or via an RStudio prompt) and is set to the default location for future package installations. So, if you're not paying close attention you end up with some packages installed in the system library and some in the user library.

The user library is listed first and the system library is listed second:

.libPaths()
> [1] "/Users/<username>/Library/R/3.6/library"                         
> [2] "/Library/Frameworks/R.framework/Versions/3.6/Resources/library"

I like to keep everything in one place (the system library), so to accomplish this (permanently) I do the following:

In a Terminal window, create a .Rprofile file in your user directory:

nano ~/.Rprofile

In that file, add the following line of R code:

.libPaths( c(.libPaths()[2], .libPaths()[1]) )

Alternatively, add a similar line of R code but with hardcoded paths:

.libPaths( c("/Library/Frameworks/R.framework/Versions/3.6/Resources/library", "/Users/<username>/Library/R/3.6/library") )

Replace <username> with your actual username. Then Ctrl-O (save) and Ctrl-X (exit) that file.

Either line switches the order of the paths listed above, so that path [2] (system) is first (Default) and path [1] (user) is second.

Importantly, by putting this line of code in your user's .Rprofile it will be run every time R starts and you (hopefully) won't have to worry about this again.

1
  • this solution seems to not work as well or at all if you do not have admin permissions on the machine when you open RStudio.
    – Brian D
    Commented Sep 20, 2023 at 16:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.