Migrating R Libraries

Migrating R libraries

Option #3 (copying old library to new library) should work ... but if and only if you then run:

update.packages(checkBuilt=TRUE)

In this manner the packages that need to be rebuilt for new versions will get updated. It is often the case that new versions add requirements (such as the impending requirement in 2.14.x for NAMESPACEs).

Edit: Seeing this is just moving around the deck chairs .... I'm going to back off from endorsing #3 if you are moving any of the base R installation. It has worked for me in a Mac, but I have not seen a promise in the R Installation and Administration Guide or the R FAQ that it should work. You can accomplish #1 (which is probably safest in various conditions) by this sequence:

# In original installation, get the non-default package list:
save.pkg.list <- installed.packages()[is.na(installed.packages()[ , "Priority"]), 1]
save(save.pkg.list, file="pkglist.Rdata")
# If you want to use remove.packages() at this point it's fine.
# Or just delete their directories.

With a freshly installed version of R with the .Libpaths set to your preferences (or even the same old installation):

load("pkglist.Rdata")
install.packages(save.pkg.list)

Just moving the packages to a new library if the R executables was not changed might succeed (assuming you also change the .Libpaths) but I do not have a Linux installation to test it or know how any pointers set by configure operations would be affected.

Migrating R library from /usr/lib/R/library to a custom library

I solved this by listing all packages that have Priority NA or "Recommended" in R, and moving only that with a bash script. This will ignore all the base R packages that R seems to require being stored where they currently are. So from R create a text file to read from the bash script:

ip <- installed.packages(lib.loc="/usr/lib/R/library/")[ , "Priority"]
write.table(installed.packages(lib.loc="/usr/lib/R/library/")[is.na(ip) | ip=="recommended", 1], file="ip.txt", row.names=FALSE, quote=FALSE)

Note that you will need to change /usr/lib/R/library/ to the correct folder you want to copy from.

Then run this bash script (remember set your own target dir, stored in the output variable, and your username, stored in the user variable):

#!/bin/bash

set -e
{
input="/usr/lib/R/library/"
output="/home/username/.R_libs/"
user="username"
packages="ip.txt"

for p in $(cat ${packages})
do
if [ -e ${input}/${p} ]; then
echo Moving package ${p}
mv ${input}/${p} ${output}/${p}
else
echo Package ${p} is not present in ${input}
fi
done
echo Changing perimssions...
chown -R ${user} .R_libs/*
echo All done!
}

Everything should be moved correctly.

Re-install R / Rstudio and migrating library

If you are installing the exact same R version with the same configuration and everything, you could avoid reinstalling it if you just move the folder with installed libraries somewhere else and then create a .Rprofile file that would set .libPaths (not 100% sure what it's called) to the folder where your installed libraries are.

How to migrate Rstudio files and installed packages ( by version to a new computer )

I suppose the first thing to do would be to make sure that the set of currently installed packages is up-to-date. Then build a target string of package names. This would be one possible methods.

  update.packages(checkBuilt=TRUE, ask=FALSE)  #check spelling of arguments
new_pacs <- paste( setdiff( installedPreviously$Package, baseR$Package),
collapse=",")
install.packages( new_pacs, dependencies=TRUE)

If this is a really big set of packages then it's very possible that you will need to repeat steps 2 and 3 a few times. The management of dependencies is not sophisticated, and it's likely that there will be packages which have "second cousins" that are not named in the DESCRIPTION files of package being installed at the beginning. It's even possible that you might want to limit the number of package installed as a group to say 30 at a time. That would allow you to identify some of the missing but critical packages early in the process.

Another method (which seems cumbersome but was effective) used in the past is to simply copy the entire contents of an existing library directory to the new directory (excluding any that are already there by refusing to overwrite.) And then run the update.packages call above.

Yet another method would be to depend on the Packrat system. It's now built into the Rstudio project management tools.

Move r packages to new computer which has no internet

The function .libPaths will give you a vector of all the libraries on your machine. Run this on your old machine to find all of them. You can simply copy all these files into the libraries on your new machine (run .libPaths on it too to find out where).

Alternatively, if you want to set up a real repository (i.e. basically a CRAN mirror) on your computer or on a network drive you can update, you can put binary or source packages into a folder and run tools::write_PACKAGES on that folder. You can them run install.packages using the contriburl argument and point it to your repository folder.

How to update all packages for a new R version quickly and easily?

Don't know if this is quick and easy, but I think the pacman package can be useful.

  1. Under the previous version, use pacman::p_lib() to return a vector of your installed packages, and save them onto disk use saveRDS().

For instance,

mypks <- pacman::p_lib()
saveRDS(mypks, "~/mypks.rds")

  1. Update R.

  2. Import the vector from step 1 using readRDS() and run install.packages() with the object.

For instance,

mypks <- readRDS("~/mypks.rds")
install.packages(mypks)

R versions and associated libraries

Based on the comments above and testing on my machine R seems to use the libraries associated with whatever R version is being used in the following directories (e.g. c:/Users/user1/Documents/R/win-library/4.1; c:/Program Files/R/R-4.1.2/library). As @r2evans has stated above this can be changed using the .libPaths() function.



Related Topics



Leave a reply



Submit