Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a library from .libPaths() permanently without Rprofile.site

Tags:

r

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

like image 352
Tommy O'Dell Avatar asked Mar 05 '13 07:03

Tommy O'Dell


3 Answers

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.

like image 70
agstudy Avatar answered Oct 20 '22 18:10

agstudy


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.

like image 33
DfAC Avatar answered Oct 20 '22 19:10

DfAC


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/

like image 9
Ahmed El-Gabbas Avatar answered Oct 20 '22 18:10

Ahmed El-Gabbas