17

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!

Community
  • 1
  • 1
Jeff Hughes
  • 173
  • 1
  • 1
  • 6
  • 1
    Are you using raw R or interfacing via RStudio? RStudio has its own paths to set... – Serban Tanasa Apr 26 '16 at 18:52
  • isn't that what you want? it first looks in `/my docs/...`, then in the default `/program files/...` if not found. the default is where all your packages live including base, so if you don't have those in your new libpath and no backup libpath, where will you get those packages? – rawr Apr 26 '16 at 19:09
  • It would probably be wiser to keep a subset of packages, those that you care about the most in your new folder. This way, you can keep them up to date, but don't have to worry about the many packages you may never (directly) use. – lmo Apr 26 '16 at 19:30
  • @SerbanTanasa I am typically using RStudio, but I did try `libPaths()` in the plain R console and it's still listing both. – Jeff Hughes Apr 26 '16 at 20:07
  • @rawr Well, I copied the base packages to my new libpath, so it should be able to find them there. But I explicitly want to get rid of the C:/Program Files reference. There are 7500+ packages in the Program Files folder and dealing with that is a nightmare I'd like to avoid. – Jeff Hughes Apr 26 '16 at 20:11
  • @lmo It's fine, except when I use the RStudio "Packages" tab, the program hangs for 2-3 minutes while it loads all the packages. It's incredibly frustrating. – Jeff Hughes Apr 26 '16 at 20:11

3 Answers3

22

Here be dragons.

assign(".lib.loc", "\your\preferred\library", envir = environment(.libPaths))
Andrie
  • 176,377
  • 47
  • 447
  • 496
  • Well, I've also read that setting .lib.loc directly is unsupported, but it also had the advantage of actually working successfully. So I'm marking this as the correct answer. Thanks, @Andrie! – Jeff Hughes Apr 27 '16 at 14:35
  • 1
    This answer seems to be out of date, `.lib.loc` returns `Error: object '.lib.loc' not found` and `?.lib.loc` directs me to a list of defunct functions : `The functions or variables listed here are no longer part of R as they are no longer needed.` – moodymudskipper Nov 06 '17 at 11:59
  • The only answer that works. I looked at a few threads. I placed this in Rprofile.site and worked correctly; I ran in console and R crashed. I had to use the correct syntax as well... for my network share it was '\\\\Share/Folder/Folder/Folder' – DonkeyKong Apr 19 '18 at 18:17
  • Great answer. I was looking for a way to set .libPaths to empty for a particular user in the Rprofile.site. This did the trick ! – qfazille Sep 06 '18 at 14:56
3

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))

Daenerys
  • 31
  • 1
2

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.

irritable_phd_syndrome
  • 4,631
  • 3
  • 32
  • 60