270

The install.packages() function in R is the automatic unzipping utility that gets and install packages in R.

  1. How do I find out what directory R has chosen to store packages?

  2. How can I change the directory in which R stores and accesses packages?

mdml
  • 22,442
  • 8
  • 58
  • 66
Milktrader
  • 9,278
  • 12
  • 51
  • 69

4 Answers4

309

The install.packages command looks through the .libPaths() variable. Here's what mine defaults to on OSX:

> .libPaths()
[1] "/Library/Frameworks/R.framework/Resources/library"

I don't install packages there by default, I prefer to have them installed in my home directory. In my .Rprofile, I have this line:

.libPaths( "/Users/tex/lib/R" )

This adds the directory /Users/tex/lib/R to the front of the .libPaths() variable.

bathyscapher
  • 1,615
  • 1
  • 13
  • 18
James Thompson
  • 46,512
  • 18
  • 65
  • 82
64

This is documented in the 'R Installation and Administration' manual that came with your installation.

On my Linux box:

R> .libPaths()
[1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"      
[3] "/usr/lib/R/library"           
R> 

meaning that the default path is the first of these. You can override that via an argument to both install.packages() (from inside R) or R CMD INSTALL (outside R).

You can also override by setting the R_LIBS_USER variable.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • I've got a Windows Vista machine, but assuming the same procedure exists, do you specify the directory in the install.packages() argument list along with the package name? – Milktrader Apr 10 '10 at 22:08
  • 4
    Have you discovered 'help(install.packages)' yet? Also, see 'help(Startup)' as per my last comment. And do read those manuals. Lastly, on *doze I tend to just set R_LIBS="C:/opt/R/library" in a file .Renviron. I know you are going to ask about that too -- so do read help(Startup). Ok? ;-) – Dirk Eddelbuettel Apr 10 '10 at 22:30
  • 4
    I've got a dozen R manuals on my Kindle (including the 3,000+ page one) and wasn't aware of the Startup help file. I've been reading the manuals and hit a dead-end. Go figure it was a simple answer all along. Do I lose points for RTFM questions <- I don't have many to lose. Thanks again Dirk. See you at R/Finance next week. Ramping up on R skills as best I can so I don't get dusted during the presentations. – Milktrader Apr 10 '10 at 22:37
  • 1
    The 3000+ page is a collation of all the help page -- least helpful. The aforemention 'Installation and Admin' one should be very useful for what you are after here. – Dirk Eddelbuettel Apr 11 '10 at 00:58
  • is there an equivalent way to find these from the bash command line? i.e., an environment variable that specifies where the R libraries are installed if `$R_LIBS` and `$R_LIBS_USER` are not set? (I want to be able to execute an R script in the `inst/` directory of an installed package) on any Unix system. – David LeBauer Jan 07 '14 at 18:07
  • Bubble the problem up: define R_LIBS / R_LIBS_USER somewhere else both your script and R can access -- maybe ~/.bashrc or /etc/environment. – Dirk Eddelbuettel Jan 07 '14 at 18:22
  • I found out the hard way that the directory R_LIBS_USER must exist for it to be used. – Matt Chambers Nov 17 '15 at 22:34
  • 1
    great answer! just what I was looking for! – stats_noob Mar 25 '23 at 03:50
  • Also thirteen years old (!!) and unchanged (!!). Sometimes that happens. R itself is a marvel of stability. – Dirk Eddelbuettel Mar 25 '23 at 13:01
  • @DirkEddelbuettel do you mind commenting why `libPaths()` defaults to `[1] "/home/user-name-here/R/x86_64-pc-linux-gnu-library/4.0" "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"? "/usr/lib/R/library"` rather than `"/usr/local/lib/R/site-library"`? Has there been a change to the default? Debian bullseye on my end. Could it be due to R installation at user rather than sys level? – Johan May 05 '23 at 19:23
  • 1
    @Johan you hit one of many answers I have given on this, maybe try the other ones, maybe come to the r-sig-debian list. It has been hashed out a few times over the years. In short, there is what I do _as defaults_ for the package but there is what you may do at your end. – Dirk Eddelbuettel May 05 '23 at 19:25
  • @DirkEddelbuettel sorry, I'll search more throughly. Thanks for advice. apologies. See you on r-sig-debian. – Johan May 05 '23 at 19:26
  • 1
    No need for apologies but it is important topic and in the span of fourteen plus years it has comes up a few times. – Dirk Eddelbuettel May 05 '23 at 19:27
18

Thanks for the direction from the above two answerers. James Thompson's suggestion worked best for Windows users.

  1. Go to where your R program is installed. This is referred to as R_Home in the literature. Once you find it, go to the /etc subdirectory.

    C:\R\R-2.10.1\etc
    
  2. Select the file in this folder named Rprofile.site. I open it with VIM. You will find this is a bare-bones file with less than 20 lines of code. I inserted the following inside the code:

    # my custom library path
    .libPaths("C:/R/library")
    

    (The comment added to keep track of what I did to the file.)

  3. In R, typing the .libPaths() function yields the first target at C:/R/Library

NOTE: there is likely more than one way to achieve this, but other methods I tried didn't work for some reason.

CodeFox
  • 3,321
  • 1
  • 29
  • 41
Milktrader
  • 9,278
  • 12
  • 51
  • 69
  • 2
    I copy pasted said line `.libPaths=("C:/R/library")` to my _Rprofile.site_ file. I launch R 2.13.1 64 bit and get this error: _Error: cannot change value of locked binding for '.libPaths'_ I am using Windows 7. – Fred Jul 15 '11 at 15:18
  • 2
    See [Dave's answer](https://stackoverflow.com/a/37883160/1811525): it should be `.libPaths("...")` (a call and not an assignment). – CodeFox Oct 29 '18 at 06:20
1

You do not want the '='

Use .libPaths("C:/R/library") in you Rprofile.site file

And make sure you have correct " symbol (Shift-2)

PKumar
  • 10,971
  • 6
  • 37
  • 52
Dave
  • 21
  • 1