-->

Migrating R libraries

2019-01-16 06:26发布

问题:

I'd like to move several R libraries (*) from one drive to another, on Linux, and would like to know whether a simple move is feasible and safe or if I should uninstall and reinstall the packages. I realize that the locations of libraries are identified via .libPaths() and have looked through the "R Installation and Administration" manual to find out about migrating libraries, but don't see a recommended process.

I perceive three options:

  1. Run remove.packages() for all of the non-base packages, and install anew via install.packages(lib = "/path/to/new/location").
  2. Move the libraries (directories) using mv and use symlinks to point to the new locations (and eventually remove the symlink)
  3. Use the mv command in Linux to move the directories wholesale and update .Library.site in R_HOME/etc/Rprofile.site, as suggested in the R Installation and Administration manual

Option #1 is blunt. Option #2 should work, but seems a bit unsound.

Is #3 safe or are there serious problems with it? The issues I've identified are: directory permissions and the possibility that any package's setup stores absolute paths rather than relative paths (which seems unsound and unnecessary).

Regarding the storage of absolute paths, I found that rJava stores the location of R_HOME in a file called run. This isn't a library problem per se, but it is one indication of a package (and a good package at that) keeping a private copy of an absolute path.

(*) There are several libraries and many scores of packages. Naturally, just the libraries (directories) are moved, but packages could be affected.


UPDATE 1 / Clarification: Just to clarify: I am only migrating libraries, not changing the version of R or the versions of the packages. Updating R or the packages may be done separately, but the question is just whether or not moving the libraries is feasible. It seems that if it is necessary to update or reinstall all packages in order to be sure things are installed correctly, then that is a path more akin to option #1 than option #3.

UPDATE 2: Answers to another SO post have some good ideas on how to avoid this problem when upgrading. I'm not upgrading R, but Dirk Eddelbuettel's suggestion of not installing packages in the filetree of R is wise.

回答1:

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.