SoFunction
Updated on 2025-03-01

How to change the path of R language default storage package

How to change the storage path of packages in R?

Method 1

You can use the following command in R

.libPaths("C:/Program Files/R/R-3.3.1/library")

Method 2

When installing a package, use the following command

("thepackage",lib="/path/to/directory/with/libraries")

Supplement: How to permanently change .libPaths() in R? R language modification of the storage path of the libPath package

Write in front

Sometimes when we newly install R language, we want to use the previous library, for example, we have installed itopenR, but if you want to install the packages you have previously installed, you can define library so that you don’t have to reinstall many packages. There are two types of definitions: temporary and permanent. The permanent method is recommended.

1. Temporary method:

You can use the following command in R

.libPaths(“C:/Program Files/R/R-3.3.1/library”)

2. Permanent modification method:

2.1, enter your working directory

cd ~

2.2, enter R language and view your .libPath()

> .libPaths()
[1] "/home/dengfei/R/x86_64-pc-linux-gnu-library/3.5"
[2] "/home/dengfei/anaconda3/lib/R/library" 

2.3, Exit R, edit a new file.Rprofile

Note that the .Rprofile here is the file name

View file name:

(base) [dengfei@localhost ~]$ ls .Rprofile 
.Rprofile

View file content:

(base) [dengfei@localhost ~]$ cat .Rprofile 
.libPaths(c("/opt/microsoft/ropen/3.5.1/lib64/R/library","/home/dengfei/R/x86_64-pc-linux-gnu-library/3.5","/home/dengfei/anaconda3/lib/R/library"))

For example, you need to put the directory: "/opt/microsoft/ropen/3.5.1/lib64/R/library" into it, and put your default lib and new lib into the file. Note that there are multiple ones here, so use c()

.libPaths(c("/opt/microsoft/ropen/3.5.1/lib64/R/library","/home/dengfei/R/x86_64-pc-linux-gnu-library/3.5","/home/dengfei/anaconda3/lib/R/library"))

2.4, enter R again, then check .libPath(), and find that it has been successful

> .libPaths()
[1] "/opt/microsoft/ropen/3.5.1/lib64/R/library"  
[2] "/home/dengfei/R/x86_64-pc-linux-gnu-library/3.5"
[3] "/home/dengfei/anaconda3/lib/R/library" 

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.