How to Change the Default Library in Rstudio to a Different Default Library or Custom Library

Changing the Default Library in Rstudio to a Different Default Library (Temporary)

We offer private, customized 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:

.libpaths()

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:



Much Better!

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[2], myPaths[1])  # switch them
.libPaths(myPaths)  # reassign them

Now view the results using the Tools -> Install Packages… menu:



Now the common library is the default.

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.
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()
myPaths <- c(myPaths, ‘C:/CustomR’)
.libPaths(myPaths) # add new path
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:



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
Accelebrate's R Programming training courses for you and your staff.

Request Pricing

Learn faster

Our live, instructor-led lectures are far more effective than pre-recorded classes

Satisfaction guarantee

If your team is not 100% satisfied with your training, we do what's necessary to make it right

Learn online from anywhere

Whether you are at home or in the office, we make learning interactive and engaging

Multiple Payment Options

We accept check, ACH/EFT, major credit cards, and most purchase orders



Recent Training Locations

Alabama

Birmingham

Huntsville

Montgomery

Alaska

Anchorage

Arizona

Phoenix

Tucson

Arkansas

Fayetteville

Little Rock

California

Los Angeles

Oakland

Orange County

Sacramento

San Diego

San Francisco

San Jose

Colorado

Boulder

Colorado Springs

Denver

Connecticut

Hartford

DC

Washington

Florida

Fort Lauderdale

Jacksonville

Miami

Orlando

Tampa

Georgia

Atlanta

Augusta

Savannah

Hawaii

Honolulu

Idaho

Boise

Illinois

Chicago

Indiana

Indianapolis

Iowa

Cedar Rapids

Des Moines

Kansas

Wichita

Kentucky

Lexington

Louisville

Louisiana

New Orleans

Maine

Portland

Maryland

Annapolis

Baltimore

Frederick

Hagerstown

Massachusetts

Boston

Cambridge

Springfield

Michigan

Ann Arbor

Detroit

Grand Rapids

Minnesota

Minneapolis

Saint Paul

Mississippi

Jackson

Missouri

Kansas City

St. Louis

Nebraska

Lincoln

Omaha

Nevada

Las Vegas

Reno

New Jersey

Princeton

New Mexico

Albuquerque

New York

Albany

Buffalo

New York City

White Plains

North Carolina

Charlotte

Durham

Raleigh

Ohio

Akron

Canton

Cincinnati

Cleveland

Columbus

Dayton

Oklahoma

Oklahoma City

Tulsa

Oregon

Portland

Pennsylvania

Philadelphia

Pittsburgh

Rhode Island

Providence

South Carolina

Charleston

Columbia

Greenville

Tennessee

Knoxville

Memphis

Nashville

Texas

Austin

Dallas

El Paso

Houston

San Antonio

Utah

Salt Lake City

Virginia

Alexandria

Arlington

Norfolk

Richmond

Washington

Seattle

Tacoma

West Virginia

Charleston

Wisconsin

Madison

Milwaukee

Alberta

Calgary

Edmonton

British Columbia

Vancouver

Manitoba

Winnipeg

Nova Scotia

Halifax

Ontario

Ottawa

Toronto

Quebec

Montreal

Puerto Rico

San Juan