39

How can I permanently remove a library in R?

.libPaths()
[1] "\\\\per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell/R/win-library/2.15"
[2] "C:/Program Files/R/R-2.15.2/library"                                            
[3] "C:/Program Files/RStudio/R/library"     

The first item is my corporate "My Documents" folder, and the apostrophe in the path from my surname is causing all kinds of grief when using R CMD INSTALL --build on a package I'm making, not to mention issues using packages installed there when I'm offline from the network.

I want to use C:/Program Files/R/R-2.15.2/library as the default instead, but I don't want to have to rely on an Rprofile.site.

What I've tried

> .libPaths(.libPaths()[2:3])
> .libPaths()
[1] "C:/Program Files/R/R-2.15.2/library" "C:/Program Files/RStudio/R/library" 

That seems to work, but only until I restart my R session, and then I'm back to the original .libPaths() output...

Restarting R session...

> .libPaths()
[1] "\\\\per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell/R/win-library/2.15"
[2] "C:/Program Files/R/R-2.15.2/library"                                            
[3] "C:/Program Files/RStudio/R/library" 

I thought maybe .libPaths() was using R_LIBS_USER

> Sys.getenv("R_LIBS_USER")
[1] "//per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell/R/win-library/2.15"

So I've tried to unset it using Sys.unsetenv("R_LIBS_USER") but it doesn't persist between sessions.

Additional Info

If it matters, here are some environment variables that might be relevant...

> Sys.getenv("R_HOME")
[1] "C:/PROGRA~1/R/R-215~1.2"
> Sys.getenv("R_HOME")
[1] "C:/PROGRA~1/R/R-215~1.2"
> Sys.getenv("R_USER")
[1] "//per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell"
> Sys.getenv("R_LIBS_USER")
[1] "//per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell/R/win-library/2.15"
> Sys.getenv("R_LIBS_SITE")
[1] ""

I've tried Sys.unsetenv("R_LIBS_USER") but this also doesn't stick between sessions

Tommy O'Dell
  • 7,019
  • 13
  • 56
  • 69
  • Since it is in an R character value, I would have suggested trying to escape the single-quote. – IRTFM Dec 15 '13 at 21:28

5 Answers5

27

Just set the environment variable R_LIBS in Windows to something like

R_LIBS=C:/Program Files/R/R-2.15.2/library

Restart R.

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • 1
    Setting this path variable helped for me instead: R_LIBS_USER – KarthikS Aug 14 '17 at 17:57
  • 3
    a working example looks like this Sys.setenv(R_LIBS_USER = 'C:/Program Files/R/R-2.15.2/library') – pdbentley Mar 10 '21 at 03:01
  • 1
    This just appended it to the problematic ones instead of replacing for me. I solved it with [this](https://stackoverflow.com/a/36873741/13210554) answer instead. – Dan Adams Mar 16 '22 at 16:12
22

This is bit late response to the question, but might be useful for others. I order to set up my own path (and remove one of the original ones) I have:

  • used .libPaths() inside R to check current library paths;
  • identified which paths to keep. In my case, it kept R's original library but removed link to my documents.
  • found R-Home path using R.home() or Sys.getenv("R_HOME");
    • R-Home\R-3.2.2\etc\Rprofile.site is read every time R kernel starts. Therefore, any modification will be persistent to every run of R.
  • Edited Rprofile.site by adding the following,

.libPaths(.libPaths()[2]) .libPaths("d:/tmp/R/win-library/3.2")

How it works?

  • First line remove all but one path (second from the original list), the second line adds an additional path. We end up with two paths.
  • note that I use Unix path notation despite using windows. R always use Unix notation, regardless of operating system
  • restarted R (using Ctr+Shift+F10)

This will work every time now.

Pragyaditya Das
  • 1,648
  • 6
  • 25
  • 44
DfAC
  • 385
  • 2
  • 6
  • This answer really helpful, but as far as I understand by executing `.libPaths("d:/tmp/R/win‑library/3.2")` after `.libPaths(.libPaths()[2])` you are setting only _"d:/tmp/R/win‑library/3.2"_ as a path, so first setting will be overridden. – Cryptor Dec 10 '16 at 20:00
  • 1
    Not really - I first remove all but second path using '.libPaths(.libPaths()[2])' and then add `.libPaths("d:/tmp/R/win-library/3.2")` as an aditonal path. I end up with two paths. – DfAC Dec 21 '16 at 17:28
  • 1
    it's not so obvious, I added comment to my original text – DfAC Dec 21 '16 at 17:34
  • 3
    This is not true on `ubuntu`: running `.libPaths(.libPaths()[2])` does not remove other libs. – Nikita Vlasenko Sep 25 '18 at 21:14
  • 1
    At least for R 3.5.2, according to .libPaths help document, it will only add parameter to existing path, not replacing existing paths. "If called with argument new, the library search path is set to the existing directories in unique(c(new, .Library.site, .Library))" – dracodoc Mar 05 '19 at 00:38
10

Use this function in .Rprofile

set_lib_paths <- function(lib_vec) {
  lib_vec <- normalizePath(lib_vec, mustWork = TRUE)
  shim_fun <- .libPaths
  shim_env <- new.env(parent = environment(shim_fun))
  shim_env$.Library <- character()
  shim_env$.Library.site <- character()
  environment(shim_fun) <- shim_env
  shim_fun(lib_vec)
}
set_lib_paths("~/code/library") # where "~/code/library" is your package directory

Original Source: https://milesmcbain.xyz/hacking-r-library-paths/

Ahmed El-Gabbas
  • 398
  • 3
  • 10
  • This is the correct procedure for removing an R library from the operating system when you do not have administrative privileges. – Lijia Yu Jul 12 '23 at 00:47
3

I have put the Sys.unsetenv("R_LIBS_USER") command in a .Rprofile file in my windows "own documents" folder. Seems to help. My problem was that being in an active directory environment made R upstart and package loading incredibly slow when connected via vpn.

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
Ute
  • 238
  • 1
  • 9
2

If you want to do this at RProfile file (@library/base/R/), you can search the lines where R_LIBS_* environment variables are set (for e.g. Sys.setenv(R_LIBS_SITE=....) and Sys.setenv(R_LIBS_USER=.....)) You can also search the code .libPaths(), which sets the library tree. So you can achieve your goal by a combination of commenting, unsetting and setting the R_LIBS variables before the .libPaths() call as you wish. For e.g. Something like:

Sys.unsetenv("R_LIBS")
Sys.unsetenv("R_LIBS_USER")
Sys.setenv(R_LIBS_SITE = "D:/R/libs/site")
Sys.setenv(R_LIBS_USER = "D:/R/libs/user")
Sys.setenv(R_LIBS = "D:/R/libs")
unm
  • 31
  • 3
  • has been some time since original question, but I have just done this and it was needed to put the Sys.unsetenv("R_LIBS_USER") line for changes to take effect. Upvoting – codesaurus Jan 13 '20 at 16:23