Created
February 1, 2015 19:42
-
-
Save etiennebr/02e0dd93dfe0fbecc99a to your computer and use it in GitHub Desktop.
Install required packages mentionned in a project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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