Shingo's Blog

Shingo's Blog

Updating to R 4.0 -Migrate packages installed in R3.6-

Updating to R 4.0 -Migrate packages installed in R3.6-

I updated my R version from R4.0 from 3.6. Then, try to run the code I wrote in R3.6. The problem I encountered is that R cannot load an elementary library like tidyverse, devtools

The problem is that the compatibility between the packages installed under R3.6 and R4.0 is not ensured. It's necessary to reinstall the packages to the new directory. First, check the directory that stores the package files.

libPaths()
[1] "/home/*****/R/x86_64-pc-linux-gnu-library/4.0"
[2] "/usr/lib/R/library"                           
[3] "/usr/local/lib/R/site-library"                
[4] "/usr/lib/R/site-library"

By checking the files in each directory, it turned out that most of my files were stored in "/usr/lib/R/library". And newly installed packages were stored in "/home/*/R/x86_64-pc-linux-gnu-library/4.0" which is a newly created directory for R4.0.

I referred to the following article but did not work when I simply run the script in this article. So I tweaked the script a little bit.

First, check all the packages.

old_packages <- installed.packages(lib.loc = "/home/*****/.local/R/site-library")
.libPaths("/home/*****/R/x86_64-pc-linux-gnu-library/4.0/")

As I found that my new directory for R 4.0 is not at the top of the list of directories returned by .libPaths function, I added the second line. Then, make a list of packages missing in the new folder. If you haven't installed any package into your new directory for R 4.0, all the user-installed packages are in the old folder (packages in system library are installed automatically when we install/update R).

new_packages <- installed.packages()
missing_df <- as.data.frame(old_packages[
  !old_packages[, "Package"] %in% new_packages[, "Package"], 
])
install.packages(missing_df$Package,lib="/home/obata/R/x86_64-pc-linux-gnu-library/4.0/",dependencies = TRUE)

It is supposed to work, but I kept on getting the message that. package '****' was installed before R 4.0.0: please re-install it. This did not work. It seems that some conflict occurs when I preserve the previous version of packages. So I tried to make a clean slate in my library environment. What I did was remove all the packages from either of the directory listed by .libPaths() using the following script. Before removing all the packages, it is vital to get the names of the packages you need to re-install later.

old_packages <- installed.packages()

Without this line, you will lose all the package names you installed!! And that's what I did....

Then, wipe out all the packages except packages in the system library.

# create a list of all installed packages
 ip <- as.data.frame(installed.packages())
 head(ip)
# if you use MRO, make sure that no packages in this library will be removed
 ip <- subset(ip, !grepl("MRO", ip$LibPath))
# we don't want to remove base or recommended packages either\
 ip <- ip[!(ip[,"Priority"] %in% c("base", "recommended")),]
# determine the library where the packages are installed
 path.lib <- unique(ip$LibPath)
# create a vector with all the names of the packages you want to remove
 pkgs.to.remove <- ip[,1]
 head(pkgs.to.remove)
# remove the packages
 sapply(pkgs.to.remove, remove.packages, lib = path.lib)

The detail of the code is available from here.

With this, I wiped out all the user-installed packages. I note that it does not include a system library (e.g., base, graphics, grDevices) that is essential to perform minimum work in the rest of the tasks. Then, confirm that there are no packages except the system library.

 packageStatus()

Then, run the previous code again.

install.packages(old_packages$Package,lib="/home/*****/R/x86_64-pc-linux-gnu-library/4.0/",dependencies = TRUE)

It takes for a long time, but I am sure that you can download it without receiving errors.

References

 
Share this
Proudly part of