R | View and modify the default installation path of R package

In【R: Several ways to install R package】【R: R version update and R package migration (detailed steps)] In these two articles, we introduced the common installation methods of R packages and how to migrate them in different R versions.

This article mainly introduces how to view the default installation location of R packages and how to modify the installation location of R packages.

1. Check the installation location of R package

Exploit code.libPaths()View The storage location of the installed R package on the computer (Figure 1). It can be seen that the R package installed by the author has two storage locations.. “D:/Program Files/R/R-4.3.1/library” and “C:/Users/hp/AppData/Local/R/win-library/4.3”

> .libPaths()
[1] "C:/Users/hp/AppData/Local/R/win-library/4.3"
[2] "D:/Program Files/R/R-4.3.1/library" 

Insert image description here
Figure 1 R package installation and installation location

2. Query installed R packages

Exploit codelibrary()Viewed Install the R package (Figure 2), corresponding to the Figure 1R package Installation location, after running the code, a new page will pop up in the RStudio script editor, respectively showing "C:/Users/hp/AppData/Local/R/win-library/4.3"< /span>All under the two installation paths Installed R packages. “D:/Program Files/R/R-4.3.1/library” and

library()

Insert image description here
Insert image description here
Figure 2 R packages installed under different paths

3. Modify the installation location of R package

Exploit code.libPaths(“D:/Program Files/R/R-4.3.1/library”)Modify the R package installation location.

.libPaths("D:/Program Files/R/R-4.3.1/library")

But when installing the R package, I was reminded about the problem in Figure 3. After consulting the information, the possible reasons are As follows:

  • Run RStudio as administrator
  • Firewall causes

Reference link:
'1. lib = “C:/Users/xxx/Documents/R/win-library/4.0”’ is not writable
'2. lib = “C:/Users/xxx/Documents/R/win-library/4.0”’ is not writable
Insert image description here
Figure 3 R package installation location cannot be read a>

Solution:
Close RStudio according to the prompts in the above link, reopen RStudio as an administrator, after using the above code again to change the R package installation path, as shown in Figure 4, the R package can be installed normally under the set path.

Insert image description here
Figure 4 R package installation path modified successfully

Note:
This method can only temporarily modify the R package installation location. After restarting RStudio, it will be restored to the default R package installation path.

4. Permanently modify the installation location of the R package

As shown inFigure 1, the R package has two locations when downloading and installing, one is created by default< a i=3>The download address of the binary package is in the downloaded_packages of the Users folder on the C drive; the other is The specific installation address of the R package.

The destdir() parameter is an element that specifiesthe download location of the binary compressed package. Since "destdir() parameter" is a parameter in the "install packages() function", if it is not set, it will be placed in In the downloaded_packages of the temporary session on drive C.

The lib() parameter is an element that specifiesthe installation location of the binary compressed package. "lib() parameter" is a parameter in the "install packages() function". If it is not set, it will be installed in .libPath by default. () under the first path (Figure 1).

Therefore, the following code can be used to modify the R package installation path:

install.packages("R包名称",destdir = "二进制包存储路径",lib = "R包存储路径")

Normally, the installation location of the R package does not need to be changed, because the downloaded_packages folder in the C drive will automatically delete the downloaded binary compressed package when R or RStudio is closed, so there is no need to worry. Downloading too many R packages will affect the C drive capacity.
Therefore, in actual circumstances, you can set **.libPaths()**. For details, please refer to: 3. Modification of R package installation location .

However, if you still want to permanently modify the default installation path of the R package, you need to modify the configuration file. For details, please refer to the content in the following link:

Reference content:
R language changes the default storage location of the downloaded installation package install.package

Guess you like

Origin blog.csdn.net/qq_43210428/article/details/133563175
Recommended