-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworks_with.R
59 lines (57 loc) · 2.09 KB
/
works_with.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
options(repos=c(
"http://www.bioconductor.org/packages/release/bioc",
"http://r-forge.r-project.org",
"http://cran.utstat.utoronto.ca/",
"http://probability.ca/cran",
"http://cran.r-project.org"))
### Write down what package versions work with your R code, and
### attempt to download and load those packages. The first argument is
### the version of R that you used, e.g. "3.0.2" and then the rest of
### the arguments are package versions. For
### CRAN/Bioconductor/R-Forge/etc packages, write
### e.g. RColorBrewer="1.0.5" and if RColorBrewer is not installed
### then we use install.packages to get the most recent version, and
### warn if the installed version is not the indicated version. For
### GitHub packages, write "user/repo@commit"
### e.g. "tdhock/animint@f877163cd181f390de3ef9a38bb8bdd0396d08a4" and
### we use install_github to get it, if necessary.
works_with_R <- function(Rvers,...){
pkg_ok_have <- function(pkg,ok,have){
stopifnot(is.character(ok))
if(!as.character(have) %in% ok){
warning("works with ",pkg," version ",
paste(ok,collapse=" or "),
", have ",have)
}
}
pkg_ok_have("R",Rvers,getRversion())
pkg.vers <- list(...)
for(pkg.i in seq_along(pkg.vers)){
vers <- pkg.vers[[pkg.i]]
pkg <- if(is.null(names(pkg.vers))){
""
}else{
names(pkg.vers)[[pkg.i]]
}
if(pkg == ""){# Then it is from GitHub.
## suppressWarnings is quieter than quiet.
if(!suppressWarnings(require(requireGitHub))){
## If requireGitHub is not available, then install it using
## devtools.
if(!suppressWarnings(require(devtools))){
install.packages("devtools")
require(devtools)
}
install_github("tdhock/requireGitHub")
require(requireGitHub)
}
requireGitHub(vers)
}else{# it is from a CRAN-like repos.
if(!suppressWarnings(require(pkg, character.only=TRUE))){
install.packages(pkg)
}
pkg_ok_have(pkg, vers, packageVersion(pkg))
library(pkg, character.only=TRUE)
}
}
}