-
Notifications
You must be signed in to change notification settings - Fork 298
/
installallpkgs.R
61 lines (55 loc) · 2.39 KB
/
installallpkgs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# ----------------------------------------------------------------------
# installallpkgs.R
# ----------------------------------------------------------------------
#
# This script attempts to find all of the R packages used in all
# of the R presentation files in the current working folder and then
# attempts to install them using "biocLite".
#
# This script was initially developed for use with course materials of
# "BIOSTAT 578A: Bioinformatics for Big Omics Data", specifically the
# files (*.Rpres, *.Rmd) found at https://github.com/raphg/Biostat-578/
#
# The idea is to download the course materials with "git clone", then
# run this script to install all of the packages used in presentation files
# so that the slides will display properly when run using, e.g., RStudio.
# Since many of the presentaions do not include installation commands
# for all of the required packages, the slides may not display properly
# without installing packages beforehand.
#
# Usage:
#
# 1. Use setwd() to set the working directory to the folder containing
# the R script and R presentation files.
#
# 2. Run this command: source("installallpkgs.R")
# ... where you should use the actual file path to this script.
#
# Author: Brian High with modification from Raphael Gottardo
# Date: 2015-10-06
# License: http://creativecommons.org/licenses/by-sa/3.0/deed.en_US
# Compile a list of Rpres and Rmd filenames in the current directory
# Tip: Use pattern="*.(Rpres|R|Rmd)" to also check *.R scripts.
filenames <- list.files(".", pattern="*.(Rpres|Rmd)", full.names=FALSE)
# Parse each file to find the packages used and compile into a list
allpkgs <- c()
for (filename in filenames) {
pkgs <- readLines(filename, warn = FALSE)
pkgs <- unlist(strsplit(x = pkgs, split = ";[ ]*"))
pkgs <- pkgs[grepl("(library|require|install\\.packages|biocLite)\\(", pkgs)]
pkgs <- gsub(".*\\((.*)\\).*", "\\1", pkgs)
pkgs <- unlist(strsplit(x = pkgs, split = ",[ ]*"))
pkgs <- gsub('["()]', "", pkgs)
pkgs <- unique(pkgs[!grepl("=", pkgs)])
allpkgs <- c(allpkgs,pkgs)
}
# Remove duplicates
allpkgs <- unique(allpkgs)
# Remove non-packages
allpkgs <- allpkgs[allpkgs!="txdbpkg"]
# Save a copy of the package list
write(allpkgs, "packages_list.txt")
# Install all packages using biocLite.
# * biocLite can pull packages from Bioconductor and CRAN
source("http://bioconductor.org/biocLite.R")
biocLite(allpkgs, ask=FALSE)