33

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.

4
  • 1
    Not sure exactly what you are trying to do but you might want to look at this question and related therein.
    – Fred
    Aug 20, 2011 at 18:37
  • +1 for knowing the difference between a library and a package :-) Aug 20, 2011 at 21:31
  • @gsk3: I hope I fixed up all possible errors of that type. I don't want to get brutalized for such a terminological issue. :)
    – Iterator
    Aug 20, 2011 at 22:44
  • 1
    @Fred: +1 - portability is a different question, but, if nothing else, the discussion of portability & a completely self-contained installation made me realize that I should retain all of the package sources. That would make it trivial to be sure that the versions are the same if I uninstall and reinstall.
    – Iterator
    Aug 21, 2011 at 18:38

2 Answers 2

36

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.

5
  • Thanks, this is interesting. Can you clarify 3 things: (1) The mention of versions in "need to be rebuilt for new versions", this refers to R, correct? (2) If (1) is correct, then what happens if the package doesn't need to be rebuilt (i.e. it's older than the current version of R)? (3) When you say if & only if, I construe that to mean that this is a necessary & sufficient condition ... do you really mean that? :) If so, then I may be missing something about why that is the case. I'm not disagreeing, but this option is new to me and I don't yet grasp the logic.
    – Iterator
    Aug 20, 2011 at 22:49
  • (1) Yes.eg a change from R 2.13.x to R 2.14.x where x can be 0, 1 or 2. As I understand it the simple act copying might result in a functional package for a new major version, eg a change from 2.13 to 2.14, but it would by no means be guaranteed. So I was probably too categorical. The "should work" means that there will a check on the dependencies of the package on the version and if you do not do an update.packages, then no check will be run..
    – IRTFM
    Aug 20, 2011 at 23:10
  • However, if I am only moving the directories and not upgrading R, then would this do any good?
    – Iterator
    Aug 21, 2011 at 14:11
  • A different way to put it: if there is no internet connection, then this method seems to be problematic. Option #3 doesn't require an internet connection, but #1 does. However, I realize now that if I do a reinstall or massive update, I'll probably benefit by retaining all of the sources.
    – Iterator
    Aug 21, 2011 at 18:37
  • This sounds like the safest solution. Moreover, when I save the package sources, I can improve the likelihood that everything will be reproduceable.
    – Iterator
    Aug 22, 2011 at 18:09
1

Combining the accepted answer, with this one, I found a simpler solution that worked:

lib_loc <- "C:/Users/apdev/Documents/R/win-library/3.3"
to_install <- unname(installed.packages(lib.loc = lib_loc)[, "Package"])
to_install
remove.packages(to_install, lib="C:/Users/apdev/Documents/R/win-library/3.3")
install.packages(pkgs = to_install, lib="C:/Program Files/R/R-3.6.1/library")

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.