How to Change the Default Library in Rstudio
Changing the Default Library in Rstudio to a Different Default Library (Temporary)
Many users have R installed in a personal workspace, but want to install packages into a common area so that their entire team can use them and preserve version consistency. It is also common to need to install R packages, but users can encounter problems due to lacking administrator permissions on the computer on which they are working. Using the default install.packages command can result in a series of frustrating messages like this:
’lib = “<myRlibrary>”’ is not writable
To address these issues, you will need to change the RStudio package library to a location for which you have access permissions.
You will see output like this:
On a one-time basis, you can simply choose where you want to install your package, but let’s say you don’t want to have to remember to select the common library each time. To change the default, you just need to swap positions. Let’s try that by running the following commands in the R console:
myPaths <- .libPaths() # get the paths myPaths <- c(myPaths, myPaths) # switch them .libPaths(myPaths) # reassign them
Now view the results using the Tools -> Install Packages… menu:
Adding a New Library Path for Packages - Temporary
Let’s say you want to install and use packages in a custom library, say C:\CustomR. You need to add it to the current list of library paths and make it the default if appropriate.
So assuming you created a directory called C:\CustomR, and you want to add it as a new path, you will need to run the following commands in the R console:
myPaths <- .libPaths()This is just one way to add a path but not the only way. I like this particular approach because it is very flexible. Go back to the Tools -> Install Packages… menu and see the result:
Making Your Library Changes Permanent
Finally, you many have noticed that the steps above only last for the current R session. To make the changes permanent, you will need to change the Rprofile file for your instance of R/RStudio. This is a little tricky if you have multiple version of R running. To determine what you are interested in and where to go, do the following:
Run the Tools->Global Options menu in RStudio. You should be in the General tab. Take note of the R version path: