for private training for 3 or more people at your site or online.
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.
First, launch RStudio. Then, see where your default library currently lies. You can view the current library path(s) by running the following command in the R console in RStudio:
You will see output like this:
In this particular case, there is a default common library, C:/Program Files/R/R-3.4.1/library, along with a personal library, C:/User/Kevin/Documents/R/win-library/3.4. If you click on the Install Packages… menu item from the Tools menu:
You can see the first library shows up as the installation location. In this instance, it is the personal library.
It is important to make sure you are running RStudio with the correct privileges. Notice what happens if I select the dropdown in Install to Library:
Where’s the common library? In this case, I do not see it because I didn’t run RStudio as an administrator, even though I am an administrator on this machine. Relaunching properly gets me 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:
Now the common library is the default.
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.
Don’t forget about the forward slash in the path. Also, if you forget to create the directory, R will ignore the command.
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:
myPaths <- c(myPaths, ‘C:/CustomR’)
.libPaths(myPaths) # add new path
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:
Navigate to the ./library/base/R path beneath it and find the Rprofile file, like in this case:
Open up this file in a standard text editor. It is the startup file used by R to handle global settings and is run every time you launch an instance. To avoid breaking any existing code, I find it best to put custom code at the bottom. Use the code above to ensure the correct libraries are inserted into your environment at startup. For the example above, I used this:
Restart RStudio (as administrator is necessary) and check out your new settings.
Happy trails and keep coding!
In-Depth R Programming Training
For in-depth R Programming training, click here to view all of
Our live, instructor-led lectures are far more effective than pre-recorded classes
If your team is not 100% satisfied with your training, we do what's necessary to make it right
Whether you are at home or in the office, we make learning interactive and engaging
We accept check, ACH/EFT, major credit cards, and most purchase orders
New York City
Salt Lake City