75

I have attempted to install R and R studio on the local drive on my work computer as opposed to the organization network folder because anything that runs through the network is really slow. When installing, the destination path shows that it's my local C:drive. However, when I install a new package, the default path shown is my network drive and there is no option to change:

.libPaths()
[1] "\\\\The library/path/I/don't/want"
[2] "C:/Program Files/R/R-3.2.1/library" 

I'm running windows 7 professional. How can I remove library path [1] and make path [2] my primary for all base packages and all new packages that I install?

8 Answers 8

78

Windows 7/10/11: If your C:\Program Files (or wherever R is installed) is blocked for writing, as mine is, then you'll get frustrated editing RProfile.site (as I did). As specified in the accepted answer, I updated R_LIBS_USER and it worked. However, even after reading the fine manual several times and extensive searching, it took me several hours to do this. In the spirit of saving someone else time...

Let's assume you want your packages to reside in C:\R\Library:

  1. Create the folder C:\R\Library. Next I need to add this folder to the R_LIBS_USER path:
  2. Click Start --> Control Panel --> User Accounts --> Change my environmental variables
  3. The Environmental Variables window pops up. If you see R_LIBS_USER, highlight it and click Edit. Otherwise click New. Both actions open a window with fields for Variable and Value.
  4. In my case, R_LIBS_USER was already there, and Value was a path to my desktop. I added to the path the folder that I created, separated by semicolon. C:\R\Library;C:\Users\Eric.Krantz\Desktop\R stuff\Packages.

(NOTE: In the last step, I could have removed the path to the Desktop location and simply left C:\R\Library).

4
  • 3
    Thank you for the step by step, appears to work for me in Windows 10 as well. Commented May 20, 2020 at 14:12
  • This worked for me but I'm getting an error with some packages (tidyverse, sf, AER so far): 'The procedure entry point EXTPTR_PTR could not be located in the dynamic link library." Anyone get this?
    – winitheju
    Commented Aug 27, 2020 at 20:30
  • @winitheju : see the note from sandJohnson at github.com/r-lib/rlang/issues/416 Commented Aug 28, 2020 at 13:53
  • Works for Windows 11 as well. In the search bar, type "Enviro" and "Edit environment variables for your account" comes up. That puts you at Step 3 above. Commented Nov 9, 2023 at 21:36
67

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 or Rprofile.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.

6
  • 9
    Thanks so much, I just set .libPaths("C:/Program Files/R/R-3.2.1/library") and now that's the only path that shows. Commented Jul 29, 2015 at 20:11
  • 1
    Interesting. I thought this added rather than overriding? Commented Jul 29, 2015 at 20:35
  • And it sure adds to an an existing .libPaths() on my Unix box. Commented Jul 29, 2015 at 20:40
  • In the help file it seemed to indicate to use the command to set the library path. Similar to setting the working directory. I could be wrong but that appears to be how it functioned for me. The first network path no longer appeared. The problem is when installing the base package I have to do it off network or it by default saves the program on my C:Drive as well as a copy on our network. I can't prevent the base library from being saved on the network, only any additional packages I install. Commented Jul 29, 2015 at 22:46
  • 1
    @DirkEddelbuettel I believe this function doesn't add to the existing .libPaths(). It takes the specified path (or vector of), appends the system lib path if it's not included, and sets. Any other paths that might have previously been added, including user-specific stuff from the initialization process, would need to be explicitly retained using .libPaths(c(mydir1,mydir2,.libPaths())) or some such. Commented Dec 7, 2016 at 16:35
26

Facing the very same problem (avoiding the default path in a network) I came up to this solution with the hints given in other answers.

The solution is editing the Rprofile file to overwrite the variable R_LIBS_USER which by default points to the home directory.

Here the steps:

  1. Create the target destination folder for the libraries, e.g., ~\target.
  2. Find the Rprofile file. In my case it was at C:\Program Files\R\R-3.3.3\library\base\R\Rprofile.
  3. Edit the file and change the definition the variable R_LIBS_USER. In my case, I replaced the this line file.path(Sys.getenv("R_USER"), "R", with file.path("~\target", "R",.

The documentation that support this solution is here

Original file with:

 if(!nzchar(Sys.getenv("R_LIBS_USER")))
     Sys.setenv(R_LIBS_USER=
                file.path(Sys.getenv("R_USER"), "R",
                          "win-library",
                          paste(R.version$major,
                                sub("\\..*$", "", R.version$minor),
                                sep=".")
                          )) 

Modified file:

if(!nzchar(Sys.getenv("R_LIBS_USER")))
     Sys.setenv(R_LIBS_USER=
                file.path("~\target", "R",
                          "win-library",
                          paste(R.version$major,
                                sub("\\..*$", "", R.version$minor),
                                sep=".")
                          ))
1
  • 5
    I recently used the procedure on a machin with Win 7 and "~\target" did not work, I had to use "~/target". I have never understood properly why and when the use of "/" or "\" matters.
    – Marco
    Commented Jun 13, 2017 at 22:14
7

Windows 10 on a Network

Having your packages stored on the network drive can slow down the performance of R / R Studio considerably, and you spend a lot of time waiting for the libraries to load/install, due to the bottlenecks of having to retrieve and push data over the server back to your local host. See the following for instructions on how to create an .RProfile on your local machine:

  1. Create a directory called C:\Users\xxxxxx\Documents\R\3.4 (or whatever R version you are using, and where you will store your local R packages- your directory location may be different than mine)
  2. On R Console, type Sys.getenv("HOME") to get your home directory (this is where your .RProfile will be stored and R will always check there for packages- and this is on the network if packages are stored there)
  3. Create a file called .Rprofile and place it in :\YOUR\HOME\DIRECTORY\ON_NETWORK (the directory you get after typing Sys.getenv("HOME") in R Console)
  4. File contents of .Rprofile should be like this:

#search 2 places for packages- install new packages to first directory- load built-in packages from the second (this is from your base R package- will be different for some)

.libPaths(c("C:\Users\xxxxxx\Documents\R\3.4", "C:/Program Files/Microsoft/R Client/R_SERVER/library"))

message("*** Setting libPath to local hard drive ***")

#insert a sleep command at line 12 of the unpackPkgZip function. So, just after the package is unzipped.

trace(utils:::unpackPkgZip, quote(Sys.sleep(2)), at=12L, print=TRUE)

message("*** Add 2 second delay when installing packages, to accommodate virus scanner for R 3.4 (fixed in R 3.5+)***")

# fix problem with tcltk for sqldf package: https://github.com/ggrothendieck/sqldf#problem-involvling-tcltk

options(gsubfn.engine = "R")

message("*** Successfully loaded .Rprofile ***")
  1. Restart R Studio and verify that you see that the messages above are displayed.

Now you can enjoy faster performance of your application on local host, vs. storing the packages on the network and slowing everything down.

1
  • Also, if you are having problems loading packages, you may need to increase the time further to accommodate the virus scanner for R 3.4 (fixed in R 3.5+) : trace(utils:::unpackPkgZip, quote(Sys.sleep(20)), at=12L, print=TRUE) message("*** Add 20 second delay when installing packages, to accommodate virus scanner for R 3.4 (fixed in R 3.5+)***") Commented Mar 24, 2021 at 16:05
6

I was struggling for a while with this as my work computer (with Windows 10) created the default user library on a network drive, which would slow down R and RStudio to an unusable state.

In case this helps someone, this is the easiest way I found, without requiring admin rights:

  • make sure the directory you want to install your packages into exists. If you want to respect the convention, use: C:\Users\username\R\win-library\rversion (for example, something like: C:\Users\janebloggs\R\win-library\3.6)
  • create a .Renviron file in your home directory (which might be on the network drive?), and in it, write one single line that defines the R_LIBS_USER variable to be your custom path:

R_LIBS_USER=C:\Users\janebloggs\R\win-library\3.6

(feel free to add comments too, with lines starting with #)

If a .Renviron file exists, R will read it at startup and use the variables as they are defined in there, before running the code in the .Rprofile. You can read about it in help(Startup).

Now it should be persistent between sessions!

2
  • 1
    This is the only solution that worked for me on a couple of different computers (my own and work) - can this also be the selected answer?
    – ChrisP
    Commented Jun 3, 2020 at 16:20
  • 1
    @ChrisP : There can't be several "ticked" answers, no, although the person who asked the questions can change their mind. The currently selected answer does (vaguely) cover my answer though, by mentioning that the R_LIBS_USER variable can be defined by hand. It's just a shame it is not highlighted as the most straight-forward of the persistent solutions.
    – stragu
    Commented Jun 5, 2020 at 5:06
2

After a couple of hours of trying to solve the issue in several ways, some of which are described here, for me (on Win 10) the option of creating a Renviron file worked, but a little different from what was written here above.

The task is to change the value of the variable R_LIBS_USER. To do this two steps needed:

  1. Create the file named Renviron (without dot) in the folder \Program\etc\ (Program is the directory where R is installed--for example, for me it was C:\Program Files\R\R-4.0.0\etc)
  2. Insert a line in Renviron with new path: R_LIBS_USER = "C:/R/Library"

After that, reboot R and use .libPaths() to confirm the default directory changed.

3
  • For me the problem was that I couldn't create or find an .Renviron file on my PC even after changing File Explorer settings to show hidden files. The solution was to use the versions package to install an older version of the usethis package and then use usethis::edit_r_environ() to edit the .Renviron file by adding the following line: R_LIBS_USER = "C:/target/directory", where the target directory is wherever you want to map .libPaths() to instead of the network drive.
    – coip
    Commented Mar 11, 2021 at 17:37
  • 1
    If you have multiple versions of R on your computer--which normally store packages in different subfolders--modifying .Renviron will direct all versions to the same folder, which can create compatibility problems with various packages and their dependencies. The workaround is to create multiple subfolders for each version (e.g. "C:/R/Library/3.5" and C:/R/Library/4.0" and then use %v at the end of your R_LIBS_USER statement: R_LIBS_USER = C:/R/Library/%v. That way, whichever version of R you open will load up packages from the appropriate subfolder.
    – coip
    Commented Mar 15, 2021 at 23:10
  • 1
    @coip Two quick notes of thanks and elaboration: (1) for those who are not handy with environment text, the %v can go at any point in the string, e.g C:/R/%v/Library, though using the default structure is probably better; and (2) the %v is also very elegant because if you have packages saved to a default directory different than the version of R, this can cause problems in operation, because by definition, any 'base' packages are being pulled from the version of R, not your default, and this may cause conflicts in operations. Using the %v allows you always to be harmonious in your packages Commented Oct 29, 2021 at 14:24
2

I think I tried all of the above and it didn't work for me. This worked, though:

  1. In home directory, make a file called ".Renviron"

  2. In that file, write:

     .libPaths(new = "/my/path/to/libs")
    
  3. Save and restart R if you had it open

1
0

For linux (I'm using Fedora 38 XFCE), in the ~/.bash_profile file I added:

# R
export R_PROFILE_USER="${HOME}/.Rprofile"
export R_LIBS_USER="${HOME}/path/wanted/as/R/library"

Afterwards, I created the ~/.Rprofile file with this content:

.libPaths("~/path/wanted/as/R/library")

Pay attention to the absence of the ${HOME} environment variable (replaced by tilde, ~, expansion) in the path indicated for .libPaths() into ~/.Rprofile file, because otherwise it will not work.

Not the answer you're looking for? Browse other questions tagged or ask your own question.