Skip to content

Commit

Permalink
renv
Browse files Browse the repository at this point in the history
  • Loading branch information
bbitarello committed Mar 4, 2024
1 parent 89e0182 commit 711e3ed
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 55 deletions.
1 change: 0 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ knitr::opts_chunk$set(

# balselr

This package is under construction. If you want to run NCD1 or NCD2, please use this [other repo](https://github.com/bbitarello/NCD-Statistics) for now.

<!-- badges: start -->
![](images/balselr2.png){width=40% height=40%}
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

# balselr

This package is under construction. If you want to run NCD1 or NCD2,
please use this [other
repo](https://github.com/bbitarello/NCD-Statistics) for now.

<!-- badges: start -->

<img src="images/balselr2.png" style="width:40.0%;height:40.0%" />
Expand Down
9 changes: 2 additions & 7 deletions renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -534,13 +534,8 @@
},
"renv": {
"Package": "renv",
"Version": "1.0.0",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"utils"
],
"Hash": "c321cd99d56443dbffd1c9e673c0c1a2"
"Version": "1.0.4",
"Source": "Repository"
},
"rlang": {
"Package": "rlang",
Expand Down
106 changes: 63 additions & 43 deletions renv/activate.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@
local({

# the requested version of renv
version <- "1.0.0"
version <- "1.0.4"
attr(version, "sha") <- NULL

# the project directory
project <- getwd()

# use start-up diagnostics if enabled
diagnostics <- Sys.getenv("RENV_STARTUP_DIAGNOSTICS", unset = "FALSE")
if (diagnostics) {
start <- Sys.time()
profile <- tempfile("renv-startup-", fileext = ".Rprof")
utils::Rprof(profile)
on.exit({
utils::Rprof(NULL)
elapsed <- signif(difftime(Sys.time(), start, units = "auto"), digits = 2L)
writeLines(sprintf("- renv took %s to run the autoloader.", format(elapsed)))
writeLines(sprintf("- Profile: %s", profile))
print(utils::summaryRprof(profile))
}, add = TRUE)
}

# figure out whether the autoloader is enabled
enabled <- local({

Expand All @@ -16,6 +31,14 @@ local({
if (!is.null(override))
return(override)

# if we're being run in a context where R_LIBS is already set,
# don't load -- presumably we're being run as a sub-process and
# the parent process has already set up library paths for us
rcmd <- Sys.getenv("R_CMD", unset = NA)
rlibs <- Sys.getenv("R_LIBS", unset = NA)
if (!is.na(rlibs) && !is.na(rcmd))
return(FALSE)

# next, check environment variables
# TODO: prefer using the configuration one in the future
envvars <- c(
Expand All @@ -35,9 +58,22 @@ local({

})

if (!enabled)
# bail if we're not enabled
if (!enabled) {

# if we're not enabled, we might still need to manually load
# the user profile here
profile <- Sys.getenv("R_PROFILE_USER", unset = "~/.Rprofile")
if (file.exists(profile)) {
cfg <- Sys.getenv("RENV_CONFIG_USER_PROFILE", unset = "TRUE")
if (tolower(cfg) %in% c("true", "t", "1"))
sys.source(profile, envir = globalenv())
}

return(FALSE)

}

# avoid recursion
if (identical(getOption("renv.autoloader.running"), TRUE)) {
warning("ignoring recursive attempt to run renv autoloader")
Expand Down Expand Up @@ -504,7 +540,7 @@ local({

# open the bundle for reading
# We use gzcon for everything because (from ?gzcon)
# > Reading from a connection which does not supply a gzip magic
# > Reading from a connection which does not supply a 'gzip' magic
# > header is equivalent to reading from the original connection
conn <- gzcon(file(bundle, open = "rb", raw = TRUE))
on.exit(close(conn))
Expand Down Expand Up @@ -767,10 +803,12 @@ local({
renv_bootstrap_validate_version <- function(version, description = NULL) {

# resolve description file
description <- description %||% {
path <- getNamespaceInfo("renv", "path")
packageDescription("renv", lib.loc = dirname(path))
}
#
# avoid passing lib.loc to `packageDescription()` below, since R will
# use the loaded version of the package by default anyhow. note that
# this function should only be called after 'renv' is loaded
# https://github.com/rstudio/renv/issues/1625
description <- description %||% packageDescription("renv")

# check whether requested version 'version' matches loaded version of renv
sha <- attr(version, "sha", exact = TRUE)
Expand Down Expand Up @@ -841,7 +879,7 @@ local({
hooks <- getHook("renv::autoload")
for (hook in hooks)
if (is.function(hook))
tryCatch(hook(), error = warning)
tryCatch(hook(), error = warnify)

# load the project
renv::load(project)
Expand Down Expand Up @@ -982,10 +1020,15 @@ local({

}

renv_bootstrap_version_friendly <- function(version, sha = NULL) {
renv_bootstrap_version_friendly <- function(version, shafmt = NULL, sha = NULL) {
sha <- sha %||% attr(version, "sha", exact = TRUE)
parts <- c(version, sprintf("[sha: %s]", substring(sha, 1L, 7L)))
paste(parts, collapse = " ")
parts <- c(version, sprintf(shafmt %||% " [sha: %s]", substring(sha, 1L, 7L)))
paste(parts, collapse = "")
}

renv_bootstrap_exec <- function(project, libpath, version) {
if (!renv_bootstrap_load(project, libpath, version))
renv_bootstrap_run(version, libpath)
}

renv_bootstrap_run <- function(version, libpath) {
Expand All @@ -1012,19 +1055,14 @@ local({

}


renv_bootstrap_in_rstudio <- function() {
commandArgs()[[1]] == "RStudio"
}

renv_json_read <- function(file = NULL, text = NULL) {

jlerr <- NULL

# if jsonlite is loaded, use that instead
if ("jsonlite" %in% loadedNamespaces()) {

json <- catch(renv_json_read_jsonlite(file, text))
json <- tryCatch(renv_json_read_jsonlite(file, text), error = identity)
if (!inherits(json, "error"))
return(json)

Expand All @@ -1033,7 +1071,7 @@ local({
}

# otherwise, fall back to the default JSON reader
json <- catch(renv_json_read_default(file, text))
json <- tryCatch(renv_json_read_default(file, text), error = identity)
if (!inherits(json, "error"))
return(json)

Expand All @@ -1046,14 +1084,14 @@ local({
}

renv_json_read_jsonlite <- function(file = NULL, text = NULL) {
text <- paste(text %||% read(file), collapse = "\n")
text <- paste(text %||% readLines(file, warn = FALSE), collapse = "\n")
jsonlite::fromJSON(txt = text, simplifyVector = FALSE)
}

renv_json_read_default <- function(file = NULL, text = NULL) {

# find strings in the JSON
text <- paste(text %||% read(file), collapse = "\n")
text <- paste(text %||% readLines(file, warn = FALSE), collapse = "\n")
pattern <- '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
locs <- gregexpr(pattern, text, perl = TRUE)[[1]]

Expand Down Expand Up @@ -1101,14 +1139,14 @@ local({
map <- as.list(map)

# remap strings in object
remapped <- renv_json_remap(json, map)
remapped <- renv_json_read_remap(json, map)

# evaluate
eval(remapped, envir = baseenv())

}

renv_json_remap <- function(json, map) {
renv_json_read_remap <- function(json, map) {

# fix names
if (!is.null(names(json))) {
Expand All @@ -1135,7 +1173,7 @@ local({
# recurse
if (is.recursive(json)) {
for (i in seq_along(json)) {
json[i] <- list(renv_json_remap(json[[i]], map))
json[i] <- list(renv_json_read_remap(json[[i]], map))
}
}

Expand All @@ -1155,26 +1193,8 @@ local({
# construct full libpath
libpath <- file.path(root, prefix)

# attempt to load
if (renv_bootstrap_load(project, libpath, version))
return(TRUE)

if (renv_bootstrap_in_rstudio()) {
setHook("rstudio.sessionInit", function(...) {
renv_bootstrap_run(version, libpath)

# Work around buglet in RStudio if hook uses readline
tryCatch(
{
tools <- as.environment("tools:rstudio")
tools$.rs.api.sendToConsole("", echo = FALSE, focus = FALSE)
},
error = function(cnd) {}
)
})
} else {
renv_bootstrap_run(version, libpath)
}
# run bootstrap code
renv_bootstrap_exec(project, libpath, version)

invisible()

Expand Down

0 comments on commit 711e3ed

Please sign in to comment.