Skip to content

Commit f3af591

Browse files
committed
Start using packrat
1 parent 4ae5875 commit f3af591

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+407
-0
lines changed

.Rprofile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#### -- Packrat Autoloader (version 0.4.4) -- ####
2+
source("packrat/init.R")
3+
#### -- End Packrat Autoloader -- ####

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ data/NaturalEarth
77
data/WDPA
88
*.pyc
99
*.html
10+
packrat/lib*/

packrat/init.R

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
local({
2+
3+
libDir <- file.path('packrat', 'lib', R.version$platform, getRversion())
4+
5+
## Escape hatch to allow RStudio to handle initialization
6+
if (!is.na(Sys.getenv("RSTUDIO", unset = NA)) &&
7+
is.na(Sys.getenv("RSTUDIO_PACKRAT_BOOTSTRAP", unset = NA))) {
8+
Sys.setenv("RSTUDIO_PACKRAT_BOOTSTRAP" = "1")
9+
setHook("rstudio.sessionInit", function(...) {
10+
# Ensure that, on sourcing 'packrat/init.R', we are
11+
# within the project root directory
12+
if (exists(".rs.getProjectDirectory")) {
13+
owd <- getwd()
14+
setwd(.rs.getProjectDirectory())
15+
on.exit(setwd(owd), add = TRUE)
16+
}
17+
source("packrat/init.R")
18+
})
19+
return(invisible(NULL))
20+
}
21+
22+
## Unload packrat in case it's loaded -- this ensures packrat _must_ be
23+
## loaded from the private library. Note that `requireNamespace` will
24+
## succeed if the package is already loaded, regardless of lib.loc!
25+
if ("packrat" %in% loadedNamespaces())
26+
try(unloadNamespace("packrat"), silent = TRUE)
27+
28+
if (suppressWarnings(requireNamespace("packrat", quietly = TRUE, lib.loc = libDir))) {
29+
30+
# Check 'print.banner.on.startup' -- when NA and RStudio, don't print
31+
print.banner <- packrat::get_opts("print.banner.on.startup")
32+
if (print.banner == "auto" && is.na(Sys.getenv("RSTUDIO", unset = NA))) {
33+
print.banner <- TRUE
34+
} else {
35+
print.banner <- FALSE
36+
}
37+
return(packrat::on(print.banner = print.banner))
38+
}
39+
40+
## Bootstrapping -- only performed in interactive contexts,
41+
## or when explicitly asked for on the command line
42+
if (interactive() || "--bootstrap-packrat" %in% commandArgs(TRUE)) {
43+
44+
message("Packrat is not installed in the local library -- ",
45+
"attempting to bootstrap an installation...")
46+
47+
## We need utils for the following to succeed -- there are calls to functions
48+
## in 'restore' that are contained within utils. utils gets loaded at the
49+
## end of start-up anyhow, so this should be fine
50+
library("utils", character.only = TRUE)
51+
52+
## Install packrat into local project library
53+
packratSrcPath <- list.files(full.names = TRUE,
54+
file.path("packrat", "src", "packrat")
55+
)
56+
57+
## No packrat tarballs available locally -- try some other means of installation
58+
if (!length(packratSrcPath)) {
59+
60+
message("> No source tarball of packrat available locally")
61+
62+
## There are no packrat sources available -- try using a version of
63+
## packrat installed in the user library to bootstrap
64+
if (requireNamespace("packrat", quietly = TRUE) && packageVersion("packrat") >= "0.2.0.99") {
65+
message("> Using user-library packrat (",
66+
packageVersion("packrat"),
67+
") to bootstrap this project")
68+
}
69+
70+
## Couldn't find a user-local packrat -- try finding and using devtools
71+
## to install
72+
else if (requireNamespace("devtools", quietly = TRUE)) {
73+
message("> Attempting to use devtools::install_github to install ",
74+
"a temporary version of packrat")
75+
library(stats) ## for setNames
76+
devtools::install_github("rstudio/packrat")
77+
}
78+
79+
## Try downloading packrat from CRAN if available
80+
else if ("packrat" %in% rownames(available.packages())) {
81+
message("> Installing packrat from CRAN")
82+
install.packages("packrat")
83+
}
84+
85+
## Fail -- couldn't find an appropriate means of installing packrat
86+
else {
87+
stop("Could not automatically bootstrap packrat -- try running ",
88+
"\"'install.packages('devtools'); devtools::install_github('rstudio/packrat')\"",
89+
"and restarting R to bootstrap packrat.")
90+
}
91+
92+
# Restore the project, unload the temporary packrat, and load the private packrat
93+
packrat::restore(prompt = FALSE, restart = TRUE)
94+
95+
## This code path only reached if we didn't restart earlier
96+
unloadNamespace("packrat")
97+
requireNamespace("packrat", lib.loc = libDir, quietly = TRUE)
98+
return(packrat::on())
99+
100+
}
101+
102+
## Multiple packrat tarballs available locally -- try to choose one
103+
## TODO: read lock file and infer most appropriate from there; low priority because
104+
## after bootstrapping packrat a restore should do the right thing
105+
if (length(packratSrcPath) > 1) {
106+
warning("Multiple versions of packrat available in the source directory;",
107+
"using packrat source:\n- ", shQuote(packratSrcPath))
108+
packratSrcPath <- packratSrcPath[[1]]
109+
}
110+
111+
112+
lib <- file.path("packrat", "lib", R.version$platform, getRversion())
113+
if (!file.exists(lib)) {
114+
dir.create(lib, recursive = TRUE)
115+
}
116+
lib <- normalizePath(lib, winslash = "/")
117+
118+
message("> Installing packrat into project private library:")
119+
message("- ", shQuote(lib))
120+
121+
surround <- function(x, with) {
122+
if (!length(x)) return(character())
123+
paste0(with, x, with)
124+
}
125+
126+
## The following is performed because a regular install.packages call can fail
127+
peq <- function(x, y) paste(x, y, sep = " = ")
128+
installArgs <- c(
129+
peq("pkgs", surround(packratSrcPath, with = "'")),
130+
peq("lib", surround(lib, with = "'")),
131+
peq("repos", "NULL"),
132+
peq("type", surround("source", with = "'"))
133+
)
134+
installCmd <- paste(sep = "",
135+
"utils::install.packages(",
136+
paste(installArgs, collapse = ", "),
137+
")")
138+
139+
fullCmd <- paste(
140+
surround(file.path(R.home("bin"), "R"), with = "\""),
141+
"--vanilla",
142+
"--slave",
143+
"-e",
144+
surround(installCmd, with = "\"")
145+
)
146+
system(fullCmd)
147+
148+
## Tag the installed packrat so we know it's managed by packrat
149+
## TODO: should this be taking information from the lockfile? this is a bit awkward
150+
## because we're taking an un-annotated packrat source tarball and simply assuming it's now
151+
## an 'installed from source' version
152+
153+
## -- InstallAgent -- ##
154+
installAgent <- 'InstallAgent: packrat 0.4.4'
155+
156+
## -- InstallSource -- ##
157+
installSource <- 'InstallSource: source'
158+
159+
packratDescPath <- file.path(lib, "packrat", "DESCRIPTION")
160+
DESCRIPTION <- readLines(packratDescPath)
161+
DESCRIPTION <- c(DESCRIPTION, installAgent, installSource)
162+
cat(DESCRIPTION, file = packratDescPath, sep = "\n")
163+
164+
# Otherwise, continue on as normal
165+
message("> Attaching packrat")
166+
library("packrat", character.only = TRUE, lib.loc = lib)
167+
168+
message("> Restoring library")
169+
restore(restart = FALSE)
170+
171+
# If the environment allows us to restart, do so with a call to restore
172+
restart <- getOption("restart")
173+
if (!is.null(restart)) {
174+
message("> Packrat bootstrap successfully completed. ",
175+
"Restarting R and entering packrat mode...")
176+
return(restart())
177+
}
178+
179+
# Callers (source-erers) can define this hidden variable to make sure we don't enter packrat mode
180+
# Primarily useful for testing
181+
if (!exists(".__DONT_ENTER_PACKRAT_MODE__.") && interactive()) {
182+
message("> Packrat bootstrap successfully completed. Entering packrat mode...")
183+
packrat::on()
184+
}
185+
186+
Sys.unsetenv("RSTUDIO_PACKRAT_BOOTSTRAP")
187+
188+
}
189+
190+
})

0 commit comments

Comments
 (0)