30

Due to the new R 2.11 release, I want to implement Dirk's suggestion here.

So for that I am asking - How can I (permanently) change R's library path? (The best solution would be one that can be run from within R)

Community
  • 1
  • 1
Tal Galili
  • 24,605
  • 44
  • 129
  • 187
  • Please see the 'R Installation and Admin manual' and/or `help(Startup)`; there are several options that are clearly documented. You have several environment variables you can set; and several options about where to set them. My previous answer provides one; you did not say why you do not want to or cannot use it. But all other options are clearly documented; the question has also been re-hashed dozens of times on r-help. – Dirk Eddelbuettel Apr 23 '10 at 12:36
  • I have been look here - http://cran.r-project.org/bin/windows/base/rw-FAQ.html#What_0027s-the-best-way-to-upgrade_003f with no success. I'll check in that manual then. – Tal Galili Apr 23 '10 at 12:40
  • o.k, using the example in the text, I get an error... Error: 16:10: unexpected '/' 16: R_LIBS=C:/ – Tal Galili Apr 23 '10 at 12:47
  • Yes, you need to protect strings with quotes – Dirk Eddelbuettel Apr 23 '10 at 13:33
  • Thanks Dirk, actually - the error was because I didn't work with Renviron.site but instead used Rprofile.site ("You live, you learn...") – Tal Galili Apr 23 '10 at 16:10
  • Note that those files are (on all systems) inside the R 'tree' and will be replaced by the next version. Consider user-local files, or system-wide environment variables. – Dirk Eddelbuettel Apr 23 '10 at 17:38
  • Thanks Dirk, I did - I'll publish what I wrote tomorrow, and would love for your feedback. Best, Tal – Tal Galili Apr 23 '10 at 21:44
  • 1
    `help(Startup)` unfortunately does not document the new usage of .libPaths() as a function, probably the most practical way out in many cases. Nor does it give a pointer to .libPaths (?library does, though.) –  Oct 24 '15 at 11:23

4 Answers4

37

You can edit Rprofile in the base library (in 'C:/Program Files/R.Files/library/base/R' by default) to include code to be run on startup. Append

########        User code        ########
.libPaths('C:/my/dir')

to Rprofile using any text editor (like Notepad) to cause R to add 'C:/my/dir' to the list of libraries it knows about.

(Notepad can't save to Program Files, so save your edited Rprofile somewhere else and then copy it in using Windows Explorer.)

Gred
  • 471
  • 4
  • 2
22

This post is just to mention an additional option. In case you need to set custom R libs in your Linux shell script you may easily do so by

export R_LIBS="~/R/lib"

See R admin guide on complete list of options.

smile-on
  • 2,073
  • 1
  • 20
  • 20
21

I've used this successfully inside R script:

library("reshape2",lib.loc="/path/to/R-packages/")

useful if for whatever reason libraries are in more than one place.

Andrie
  • 176,377
  • 47
  • 447
  • 496
IsC
  • 211
  • 2
  • 2
4

I'm late to the party but I encountered the same thing when I tried to get fancy and move my library and then had files being saved to a folder that was outdated:

.libloc <<- "C:/Program Files/rest_of_your_Library_FileName"

One other point to mention is that for Windows Computers, if you copy the address from Windows Explorer, you have to manually change the '\' to a '/' for the directory to be recognized.

Andrie
  • 176,377
  • 47
  • 447
  • 496
CameronJ
  • 41
  • 2