Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quickly replicate/update local library under $R_LIBS_USER?

Tags:

r

Suppose that

  1. the version of R I have installed is 3.3.1;
  2. my environment variable $R_LIBS_USER is set to $HOME/my/R_lib/%V; and
  3. I have a directory $HOME/my/R_lib/3.3.1 containing a large number of packages I've installed over time.

Now I want to upgrade my version of R, to 3.4.1, say.

I'm looking for a convenient way to install a new collection of packages under the new directory $HOME/my/R_lib/3.4.1 that is the "version-3.4.1-equivalent" of the library I currently have under $HOME/my/R_lib/3.3.1.

(IOW, I'm looking for a functionality similar to what one can do with Python's pip installer's "freeze" option, which, essentially, produces the input one would have to give the installer in the future to reproduce the current installation.)

like image 885
kjo Avatar asked Feb 02 '18 18:02

kjo


Video Answer


1 Answers

You can use function installed.packages for that purpose. Namely:

installed.packages(lib.loc = "$HOME/my/R_lib/3.3.1")

The returned object contains a lot of informations (most fields from each package DESCRIPTION file) but the names of the packages are in the first column. So something like the following should do the trick:

inst <- installed.packages(lib.loc = "$HOME/my/R_lib/3.3.1")
install.packages(inst[,1], lib="$HOME/my/R_lib/3.4.1", dependencies=FALSE)

To answer the additional question in the comments:

If your old library contained packages from other sources than CRAN, you will have to do some gymnastic based on the content of the DESCRIPTION files, and thus will depend on how well the package author documented it.
Argument field in installed.packages allows you to select some additional fields from those files. Fields of interest to determine the source of a package are fields Repository, URL and Maintainer. Here are some ideas on how to split them apart:

CRAN vs non-CRAN:

inst <- installed.packages(lib.loc = "$HOME/my/R_lib/3.3.1", 
                           fields=c("URL","Repository","Maintainer"))
inst <- as.data.frame(inst, row.names=NA, stringsAsFactors=FALSE)
cran <- inst[inst$Repository%in%"CRAN",]
non_cran <- inst[!inst$Repository%in%"CRAN" & !inst$Priority%in%"base",]

Bioconductor packages:

bioc <- inst[grepl("Bioconductor",inst$Maintainer),]
source("https://bioconductor.org/biocLite.R")
biocLite(pkgs=bioc$Packages)

Github packages:

git <- non_cran[grepl("github", non_cran$URL),]
install.packages("devtools")
library(devtools)
for(i in seq(nrow(git))){
    install_github(repo=gsub("http://github.com/","",git$URL[i]))
}
like image 114
plannapus Avatar answered Oct 21 '22 02:10

plannapus