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



Contact Us:

Accelebrate’s training classes are available for private groups of 3 or more people at your site or online anywhere worldwide.

Don't settle for a "one size fits all" public class! Have Accelebrate deliver exactly the training you want, privately at your site or online, for less than the cost of a public class.

For pricing and to learn more, please contact us.

Contact Us Train For Us

Toll-free in US/Canada:
877 849 1850
International:
+1 678 648 3113

Toll-free in US/Canada:
866 566 1228
International:
+1 404 420 2491

925B Peachtree Street, NE
PMB 378
Atlanta, GA 30309-3918
USA

Subscribe to our Newsletter:

Never miss the latest news and information from Accelebrate:

Microsoft Gold Partner

Please see our complete list of
Microsoft Official Courses

Recent Training Locations

Alabama

Huntsville

Montgomery

Birmingham

Alaska

Anchorage

Arizona

Phoenix

Tucson

Arkansas

Fayetteville

Little Rock

California

San Francisco

Oakland

San Jose

Orange County

Los Angeles

Sacramento

San Diego

Colorado

Denver

Boulder

Colorado Springs

Connecticut

Hartford

DC

Washington

Florida

Fort Lauderdale

Miami

Jacksonville

Orlando

Saint Petersburg

Tampa

Georgia

Atlanta

Augusta

Savannah

Idaho

Boise

Illinois

Chicago

Indiana

Indianapolis

Iowa

Ceder Rapids

Des Moines

Kansas

Wichita

Kentucky

Lexington

Louisville

Louisiana

Baton Rouge

New Orleans

Maine

Portland

Maryland

Annapolis

Baltimore

Hagerstown

Frederick

Massachusetts

Springfield

Boston

Cambridge

Michigan

Ann Arbor

Detroit

Grand Rapids

Minnesota

Saint Paul

Minneapolis

Mississippi

Jackson

Missouri

Kansas City

St. Louis

Nebraska

Lincoln

Omaha

Nevada

Reno

Las Vegas

New Jersey

Princeton

New Mexico

Albuquerque

New York

Buffalo

Albany

White Plains

New York City

North Carolina

Charlotte

Durham

Raleigh

Ohio

Canton

Akron

Cincinnati

Cleveland

Columbus

Dayton

Oklahoma

Tulsa

Oklahoma City

Oregon

Portland

Pennsylvania

Pittsburgh

Philadelphia

Rhode Island

Providence

South Carolina

Columbia

Charleston

Spartanburg

Greenville

Tennessee

Memphis

Nashville

Knoxville

Texas

Dallas

El Paso

Houston

San Antonio

Austin

Utah

Salt Lake City

Virginia

Richmond

Alexandria

Arlington

Washington

Tacoma

Seattle

West Virginia

Charleston

Wisconsin

Madison

Milwaukee

Alberta

Edmonton

Calgary

British Columbia

Vancouver

Nova Scotia

Halifax

Ontario

Ottawa

Toronto

Quebec

Montreal

Puerto Rico

San Juan

© 2013-2019 Accelebrate, Inc. All Rights Reserved. All trademarks are owned by their respective owners.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.