Skip to content

Instantly share code, notes, and snippets.

@slowkow
Last active May 10, 2021 22:47
Show Gist options
  • Save slowkow/ba23b44ebc50c2a1a62daf9de370a714 to your computer and use it in GitHub Desktop.
Save slowkow/ba23b44ebc50c2a1a62daf9de370a714 to your computer and use it in GitHub Desktop.
Generate a DESCRIPTION file with all of your installed packages

Problem

We need to install a lot of R packages each time we upgrade to a new version of R.

Solution

  1. Before installing the new R, run Rscript make_description.R to write a list of all installed packages.
  2. Install the new version of R.
  3. Use devtools to reinstall the packages.

We can get the make_description.R script and run it:

cd ~
Rscript make_description.R
head -n20 DESCRIPTION
Package: MyPackages
Version: 0.0.0.9000
Title: My Packages
Description: My Packages
Encoding: UTF-8
Depends:
    R (>= 3.0.0)
biocViews:
Remotes:
    github::baptiste/egg,
    github::eclarke/ggbeeswarm,
    github::mojaveazure/loomR,
    github::thomasp85/patchwork,
    github::AnalytixWare/shinysky
Imports:
    askpass,
    assertthat,
    backports,
    beeswarm,
    BH,

Now that we have saved a list of all our R packages, we can download and install the new version of R.

Let's install devtools, and then use devtools to install our old packages:

Rscript -e 'install.packages("devtools")'
Rscript -e 'devtools::install_deps()'
#!/usr/bin/env Rscript
# make_description.R
# Kamil Slowikowski
# 2019-05-15
# Get installed packages.
ip <- as.data.frame(installed.packages()[ , c(1, 3:4)])
ip <- ip[is.na(ip$Priority), 1:2, drop = FALSE]
packages <- sort(unique(as.character(ip$Package)))
is_github <- function(x) {
exists("GithubRepo", packageDescription(x))
}
ix <- sapply(packages, is_github)
github_packages <- packages[ix]
cran_packages <- packages[!ix]
add_github <- function(x) {
pd <- packageDescription(x)
if (exists("GithubRepo", pd)) {
x <- sprintf("github::%s/%s", pd$GithubUsername, pd$GithubRepo)
}
return(x)
}
github_packages <- unlist(lapply(github_packages, add_github))
out_file <- "DESCRIPTION"
# Template for the DESCRIPTION file.
temp <- "Package: MyPackages
Version: 0.0.0.9000
Title: My Packages
Description: My Packages
Encoding: UTF-8
Depends:
R (>= 3.0.0)
biocViews:"
cat(temp, file = out_file)
cat("
Remotes:
",
paste(github_packages, collapse = ",\n "),
file = out_file,
append = TRUE,
sep = ""
)
cat("
Imports:
",
paste(packages, collapse = ",\n "),
file = out_file,
append = TRUE,
sep = ""
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment