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

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.


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.


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/

Tags:

R