Jeff Hughes
Jeff Hughes

Reputation: 173

How to change and remove default library location?

I am running R on a networked computer where I don't have write access to most locations on the C drive. However, my IT department decided to load the entire CRAN repository in the default library location on the C drive. I would strongly prefer to manage my packages on my own, and have a location on my networked drive I can install to, but I am trying to get R to ignore and forget the C drive location entirely.

I have tried creating an .Rprofile file to set the library location, but no matter what I do, .libPaths() does not seem to forget the default location. I have tried the following:

.Library <- file.path("N:/My Documents/R/win-library/3.2")
.Library.site <- file.path("N:/My Documents/R/win-library/3.2")
.lib.loc <- file.path("N:/My Documents/R/win-library/3.2")
.libPaths(.libPaths())

Where the path on the N drive is the new location I want to be the only library path. But when I restart R and run .libPaths(), I still see this:

[1] "\\\\fileu/users$/username/My Documents/R/win-library/3.2"
[2] "C:/Program Files/R/R-3.2.4/library"

(On a side note, I'm trying to switch from the symbolic '\fileu' reference to a reference to the N drive.)

I know there have been similar questions asked on this in the past (e.g., here), but the particular problem I'm having is that it's not forgetting the C drive location. I can't seem to erase that at all. Any help would be greatly appreciated!

Upvotes: 17

Views: 15041

Answers (3)

irritable_phd_syndrome
irritable_phd_syndrome

Reputation: 5067

I am using R/4.0.0 on CentOS 8.1. I was having difficulty installing a package because the R was not installing the packages listed in DESCRIPTION/[Imports, Suggests, LinkingTo] because it was finding them in R_HOME/library pointed to by .libPaths().

I struggled to get @Andrie's solution to work. According to ?.lib.loc

functions or variables listed here are no longer part of R as they are no longer needed

I consulted ?Startup and tried things like passing --no-site-file and --no-environ to R at the command line, but I was still unable to remove R_HOME/library from .libPaths().

SOLUTION :

I prepended .libPaths with the path of my local package install directory BEFORE R_HOME/library. I set this up in my ~/.Rprofile, e.g.

.First <- function(){
    .libPaths = .libPaths("~/Scratch/4.0.0-lib")
}

Then when R is started :

> .libPaths()
[1] "/gpfs0/home1/group/userXYZ/Scratch/4.0.0-lib"
[2] "/gpfs0/export/apps/easybuild/software/R/4.0.0-foss-2020a/lib64/R/library"

I then explicitly installed all the packages listed in DESCRIPTION/[Imports, Suggests, LinkingTo] that I needed directly into ~/Scratch/4.0.0-lib, so R would find them BEFORE the system packages. This worked as expected.

Upvotes: 2

Daenerys
Daenerys

Reputation: 31

If you want to change your library location permanently use this command: .libPaths("drive:/location/location")

If you want to change your library location for the particular session in RStudio (i.e. for a temporary change), use: assign(".lib.loc", "drive:/location/location", envir = environment(.libPaths))

Upvotes: 3

Andrie
Andrie

Reputation: 179428

Here be dragons.

assign(".lib.loc", "\your\preferred\library", envir = environment(.libPaths))

Upvotes: 22

Related Questions