Skip to content

Instantly share code, notes, and snippets.

@etiennebr
Created February 1, 2015 19:42
Show Gist options
  • Save etiennebr/02e0dd93dfe0fbecc99a to your computer and use it in GitHub Desktop.
Save etiennebr/02e0dd93dfe0fbecc99a to your computer and use it in GitHub Desktop.
Install required packages mentionned in a project
# list required packages
list_r_files = function() {
list.files(patt = "(?i)\\.(r|rmd)$", full = TRUE, recursive = TRUE)
}
regular_expression <- function(x, p) regmatches(x, regexec(p, x))
last = function(x) x[length(x)]
scan_packages = function(x) {
expr = '(library|require)\\("(.+)"\\)'
m = regular_expression(readLines(x), expr)
unlist(lapply(m, last))
}
installed_packages = function() unique(unlist(library()$results[, 1]))
# usage
pack = unique(unlist(lapply(list_r_files(), scan_packages)))
print(setdiff(pack, installed_packages()))
install.packages(setdiff(pack, installed_packages()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment