Change R default library path using .libPaths in Rprofile.site fails to work
I generally try to keep all of my packages in one library, but if you want to add a library why not append the new library (which must already exist in your filesystem) to the existing library path?
.libPaths( c( .libPaths(), "~/userLibrary") )
# obviously this would need to be a valid file directory in your OS
# min just happened to be on a Mac that day
Or (and this will make the userLibrary the first place to put new packages):
.libPaths( c( "~/userLibrary" , .libPaths() ) )
Then I get (at least back when I wrote this originally):
> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/2.15/Resources/library"
[2] "/Users/user_name/userLibrary"
The .libPaths
function is a bit different than most other nongraphics functions. It works via side-effect. The functions Sys.getenv
and Sys.setenv
that report and alter the R environment variables have been split apart but .libPaths
can either report or alter its target.
The information about the R startup process can be read at ?Startup
help page and there is RStudio material at: https://support.rstudio.com/hc/en-us/articles/200549016-Customizing-RStudio
In your case it appears that RStudio is not respecting the Rprofile.site settings or perhaps is overriding them by reading an .Rprofile setting from one of the RStudio defaults. It should also be mentioned that the result from this operation also appends the contents of calls to .Library
and .Library.site
, which is further reason why an RStudio- (or any other IDE or network installed-) hosted R might exhibit different behavior.
Since Sys.getenv()
returns the current system environment for the R process, you can see the library and other paths with:
Sys.getenv()[ grep("LIB|PATH", names(Sys.getenv())) ]
The two that matter for storing and accessing packages are (now different on a Linux box):
R_LIBS_SITE /usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library
R_LIBS_USER /home/david/R/x86_64-pc-linux-gnu-library/3.5.1/
How do I change the default library path for R packages
See help(Startup)
and help(.libPaths)
as you have several possibilities where this may have gotten set. Among them are
- setting
R_LIBS_USER
- assigning
.libPaths()
in.Rprofile
orRprofile.site
and more.
In this particular case you need to go backwards and unset whereever \\\\The library/path/I/don't/want
is set.
To otherwise ignore it you need to override it use explicitly i.e. via
library("somePackage", lib.loc=.libPaths()[-1])
when loading a package.
How to change and remove default library location?
Here be dragons.
assign(".lib.loc", "\your\preferred\library", envir = environment(.libPaths))
Change default library path for one session only in R
Functions in R that load packages do so by querying the active library paths, as provided by .libPaths()
:
> .libPaths()
[1] "/Users/kevinushey/Library/R/4.0/library"
[2] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library"
You can customize the library paths used in a particular session with the same function -- just pass it the library paths you wish to use. For example:
> dir.create("~/r-lib")
> .libPaths("~/r-lib")
> .libPaths()
[1] "/Users/kevinushey/r-lib"
[2] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library"
Then library()
, ::
and so on will load packages from those library paths.
Note that system-wide and site-wide library paths will always be appended to your requested library paths -- see ?.libPaths
for more details.
Migrating R libraries
Option #3 (copying old library to new library) should work ... but if and only if you then run:
update.packages(checkBuilt=TRUE)
In this manner the packages that need to be rebuilt for new versions will get updated. It is often the case that new versions add requirements (such as the impending requirement in 2.14.x for NAMESPACEs).
Edit: Seeing this is just moving around the deck chairs .... I'm going to back off from endorsing #3 if you are moving any of the base R installation. It has worked for me in a Mac, but I have not seen a promise in the R Installation and Administration Guide or the R FAQ that it should work. You can accomplish #1 (which is probably safest in various conditions) by this sequence:
# In original installation, get the non-default package list:
save.pkg.list <- installed.packages()[is.na(installed.packages()[ , "Priority"]), 1]
save(save.pkg.list, file="pkglist.Rdata")
# If you want to use remove.packages() at this point it's fine.
# Or just delete their directories.
With a freshly installed version of R with the .Libpaths set to your preferences (or even the same old installation):
load("pkglist.Rdata")
install.packages(save.pkg.list)
Just moving the packages to a new library if the R executables was not changed might succeed (assuming you also change the .Libpaths) but I do not have a Linux installation to test it or know how any pointers set by configure operations would be affected.
Related Topics
Add (Insert) a Column Between Two Columns in a Data.Frame
Using a Pre-Defined Color Palette in Ggplot
Saving Grid.Arrange() Plot to File
Re-Ordering Factor Levels in Data Frame
Is Set.Seed Consistent Over Different Versions of R (And Ubuntu)
Split Date into Different Columns for Year, Month and Day
Remove All of X Axis Labels in Ggplot
Avoid Ggplot Sorting the X-Axis While Plotting Geom_Bar()
Efficiently Sum Across Multiple Columns in R
How to Escape a Backslash in R
Select Values from Different Columns Based on a Variable Containing Column Names
Geometric Mean: Is There a Built-In
How to Drop Columns by Name Pattern in R
Why Is As.Date Slow on a Character Vector