[r] Change R default library path using .libPaths in Rprofile.site fails to work

I am running R on Windows, not as an administrator. When I install a package, the following command doesn't work:

> install.packages("zoo")
Installing package(s) into ‘C:/Program Files/R/R-2.15.2/library’
(as ‘lib’ is unspecified)
Warning in install.packages :
  'lib = "C:/Program Files/R/R-2.15.2/library"' is not writable

To install a package, I have to specify a library location:

install.packages("zoo", lib="C:/software/Rpackages")

To load a package, I also have to specify the library location:

library("zoo", lib.loc="C:/software/Rpackages")

All of this is OK, but I wanted to see if I could add C:/software/Rpackages to the library path somehow and thus not have to type it each time.

As I searched online, I found that one way to do this is to edit the Rprofile.site file and to add the line

.libPaths("C:/software/Rpackages")

However, after doing this, and starting RStudio, this is the output that I get

> .libPaths()
[1] "C:/Program Files/R/R-2.15.2/library" "C:/Program Files/RStudio/R/library" 

The .libPaths command that I added to the Rprofile.site doesn't seem to have had any effect! Why is this the case? Or more importantly, how can I fix the problem so that I can install and load packages without typing in the library location?

Note: if I start RStudio the .libPaths() command seems to work as it is supposed to

.libPaths("C:/software/Rpackages")
> .libPaths()
[1] "C:/software/Rpackages"               "C:/Program Files/R/R-2.15.2/library"

Isn't that strange?

This question is related to r rstudio r-faq library-path

The answer is


getwd()
# [1] "C:/Users/..../software/My R studio"

copy the above link with double inverted comma

.libPaths(new="C:/Users/..../software/My R studio")

Your default path will change for installing pakages


If your default package library has been changed after installing a new version of R or by any other means, you can append both the libraries to use all the packages with the help of the commands below. Get the existing library path :

.libPaths()

Now,set the existing and the old path :

.libPaths(c(.libPaths(), "~/yourOldPath"))

Hope it helps.


just change the default folder for your R libraries in a directory with no Administrator rights, e.g.

.libPaths("C:/R/libs")

I managed to solve the problem by placing the code in the .Rprofile file in the default working directory.

First, I found the location of the default working directory

> getwd()
[1] "C:/Users/me/Documents"

Then I used a text editor to write a simple .Rprofile file with the following line in it

.libPaths("C:/software/Rpackages")

Finally, when I start R and run .libPaths() I get the desired output:

> .libPaths()
[1] "C:/software/Rpackages"               "C:/Program Files/R/R-2.15.2/library"
[3] "C:/Program Files/RStudio/R/library"

I was looking into this because R was having issues installing into the default location and was instead just putting the packages into the temp folder. It turned out to be the latest update for Mcaffee Endpoint Security which apparently has issues with R. You can disable the threat protection while you install the packages and it will work properly.


If you want to change your library path permanently (without calling .libPath() every time when entering in R, this works for me:

  1. create .Rprofile under your home directory. (~/.Rprofile)

  2. type .libPaths(c( .libPaths(), "your new path" )) in .Rprofile file, save.

  3. open R (any directory) and check, just type .libPaths(), you can find your libaray path is updated!


I found what I think is a solution here (thank you Carl Schwarz at SFU) for adding a personal library that is permanently (you don't have to define it each session) recognized whether using R or Rstudio, and Rstudio treats it as the default on my Mac machine. I hadn't seen it laid out this explicitly on SO, so I summarized the steps they provided, for Windows and then for Mac.

For a Windows 7 OS:

  1. Create a directory on the drive where you want to have your personal library, e.g. C:\User\Rlibs (or another that you have permissions to)

  2. Search for/go to "Edit environment variable for your account" in the Windows search bar to edit control panel settings

  3. Click "New..." in the middle of the "Environmental Variables" window

  4. In the "New User Variable" window, type R_LIBS for the "Variable name", and the path to the personal library directory you created, e.g. C:\User\Rlibs

  5. Click OK and you should see the Variable/Value pair in the User variables window

  6. Click OK again

Now when you start R (or Rstudio) and type the command .libPaths() you should see the personal library you created as well as the R system library.

For Mac:

  1. In your "Home" or "username" directory create a folder called Rlibs

  2. Launch the Terminal application

  3. Type: echo "R_LIBS=~/Rlibs"> .Renvrion Make sure case matches.

  4. Type ls -a to see the full list of files in the directory, which should now include .Renvrion

  5. Verify that the .Renviron file has been set properly: more .Renviron

Launch R/Rstudio and type .libPaths() and you should see the new path to your personal library.



I read the readme. In that they mentioned use .libPaths() in command line to check which paths are there. I had 2 library paths earlier. When I used the command .libpath("C:/Program Files/R/R-3.2.4revised/library") where I wanted, it changed the library path. When I typed in .libPaths() at the command line again it showed me the correct path. Hope this helps


The proper solution is to set environment variable R_LIBS_USER to the value of the file path to your desired library folder as opposed to getting RStudio to recognize a Rprofile.site file.

To set environment variable R_LIBS_USER in Windows, go to the Control Panel (System Properties -> Advanced system properties -> Environment Variables -> User Variables) to a desired value (the path to your library folder), e.g.

Variable name: R_LIBS_USER 
Variable value: C:/software/Rpackages  

If for some reason you do not have access to the control panel, you can try running rundll32 sysdm.cpl,EditEnvironmentVariables from the command line on Windows and add the environment variable from there.

Setting R_LIBS_USER will ensure that the library shows up first in .libPaths() regardless of starting RStudio directly or by right-clicking an file and "Open With" to start RStudio.

The Rprofile solution can work if RStudio is always started by clicking the RStudio shortcut. In this case, setting the default working directory to the directory that houses your Rprofile will be sufficient. The Rprofile solution does not work when clicking on a file to start RStudio because that changes the working directory away from the default working directory.


Since most of the answers here are related to Windows & Mac OS, (and considering that I also struggled with this) I decided to post the process that helped me solve this problem on my Arch Linux setup.

Step 1:

  • Do a global search of your system (e.g. ANGRYSearch) for the term Renviron (which is the configuration file where the settings for the user libraries are set).
  • It should return only two results at the following directory paths:
    1. /etc/R/
    2. /usr/lib/R/etc/
      NOTE: The Renviron config files stored at 1 & 2 (above) are hot-linked to each other (which means changes made to one file will automatically be applied [ in the same form / structure ] to the other file when the file being edited is saved - [ you also need sudo rights for saving the file post-edit ] ).

Step 2:

  • Navigate into the 1st directory path ( /etc/R/ ) and open the Renviron file with your favourite text editor.
  • Once inside the Renviron file search for the R_LIBS_USER tag and update the text in the curly braces section to your desired directory path.

    EXAMPLE:
    ... Change From ( original entry ):
    R_LIBS_USER=${R_LIBS_USER-'~/R/x86_64-pc-linux-gnu-library/4.0'}
    ... Change To ( your desired entry ):
    R_LIBS_USER=${R_LIBS_USER-'~/Apps/R/rUserLibs'}

Step 3:

  • Save the Renviron file you've just edited ... DONE !!

If you do not have admin-rights, it can also be helpful to open the Rprofile.site-file located in \R-3.1.0\etc and add:

.First <- function(){
  .libPaths("your path here")
}

This evaluates the .libPath() command directly at start


On Ubuntu, the recommended way of changing the default library path for a user, is to set the R_LIBS_USER variable in the ~/.Renviron file.

touch ~/.Renviron
echo "R_LIBS_USER=/custom/path/in/absolute/form" >> ~/.Renviron

I've had real trouble understanding this. gorkypl gave the correct solution above when I last re-installed my OS & Rstudio but this time round, setting my environment variable didn't resolve.

Uninstallled both R and Rstudio, creating directories C:\R and C:\Rstudio then reinstalled both.

Define R_LIBS_USER user variable to your prefered directory (as per gorkypl's answer) and restart your machine for User variable to be loaded. Open Rstudio, errors should be gone.

You can also use Sys.setenv() to modify R_LIBS_USER to the path of your alternative library which is easier and does not need to restart your computer.

To see what R_LIBS_USER is set to: ?Sys.getenv()

Reading help(Startup) is useful.


Examples related to r

How to get AIC from Conway–Maxwell-Poisson regression via COM-poisson package in R? R : how to simply repeat a command? session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium How to show code but hide output in RMarkdown? remove kernel on jupyter notebook Function to calculate R2 (R-squared) in R Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph R multiple conditions in if statement What does "The following object is masked from 'package:xxx'" mean?

Examples related to rstudio

Change R default library path using .libPaths in Rprofile.site fails to work Update R using RStudio R - Markdown avoiding package loading messages Reset the graphical parameters back to default values without use of dev.off()

Examples related to r-faq

What does "The following object is masked from 'package:xxx'" mean? What does "Error: object '<myvariable>' not found" mean? How do I deal with special characters like \^$.?*|+()[{ in my regex? What does %>% function mean in R? How to plot a function curve in R Use dynamic variable names in `dplyr` Error: unexpected symbol/input/string constant/numeric constant/SPECIAL in my code How should I deal with "package 'xxx' is not available (for R version x.y.z)" warning? How to select the row with the maximum value in each group R data formats: RData, Rda, Rds etc

Examples related to library-path

Change R default library path using .libPaths in Rprofile.site fails to work