Path: blob/main/tests/docs/manuscript/qmd-single/renv/activate.R
3593 views
1local({23# the requested version of renv4version <- "0.17.3"56# the project directory7project <- getwd()89# figure out whether the autoloader is enabled10enabled <- local({1112# first, check config option13override <- getOption("renv.config.autoloader.enabled")14if (!is.null(override))15return(override)1617# next, check environment variables18# TODO: prefer using the configuration one in the future19envvars <- c(20"RENV_CONFIG_AUTOLOADER_ENABLED",21"RENV_AUTOLOADER_ENABLED",22"RENV_ACTIVATE_PROJECT"23)2425for (envvar in envvars) {26envval <- Sys.getenv(envvar, unset = NA)27if (!is.na(envval))28return(tolower(envval) %in% c("true", "t", "1"))29}3031# enable by default32TRUE3334})3536if (!enabled)37return(FALSE)3839# avoid recursion40if (identical(getOption("renv.autoloader.running"), TRUE)) {41warning("ignoring recursive attempt to run renv autoloader")42return(invisible(TRUE))43}4445# signal that we're loading renv during R startup46options(renv.autoloader.running = TRUE)47on.exit(options(renv.autoloader.running = NULL), add = TRUE)4849# signal that we've consented to use renv50options(renv.consent = TRUE)5152# load the 'utils' package eagerly -- this ensures that renv shims, which53# mask 'utils' packages, will come first on the search path54library(utils, lib.loc = .Library)5556# unload renv if it's already been loaded57if ("renv" %in% loadedNamespaces())58unloadNamespace("renv")5960# load bootstrap tools61`%||%` <- function(x, y) {62if (is.environment(x) || length(x)) x else y63}6465`%??%` <- function(x, y) {66if (is.null(x)) y else x67}6869bootstrap <- function(version, library) {7071# attempt to download renv72tarball <- tryCatch(renv_bootstrap_download(version), error = identity)73if (inherits(tarball, "error"))74stop("failed to download renv ", version)7576# now attempt to install77status <- tryCatch(renv_bootstrap_install(version, tarball, library), error = identity)78if (inherits(status, "error"))79stop("failed to install renv ", version)8081}8283renv_bootstrap_tests_running <- function() {84getOption("renv.tests.running", default = FALSE)85}8687renv_bootstrap_repos <- function() {8889# get CRAN repository90cran <- getOption("renv.repos.cran", "https://cloud.r-project.org")9192# check for repos override93repos <- Sys.getenv("RENV_CONFIG_REPOS_OVERRIDE", unset = NA)94if (!is.na(repos)) {9596# check for RSPM; if set, use a fallback repository for renv97rspm <- Sys.getenv("RSPM", unset = NA)98if (identical(rspm, repos))99repos <- c(RSPM = rspm, CRAN = cran)100101return(repos)102103}104105# check for lockfile repositories106repos <- tryCatch(renv_bootstrap_repos_lockfile(), error = identity)107if (!inherits(repos, "error") && length(repos))108return(repos)109110# if we're testing, re-use the test repositories111if (renv_bootstrap_tests_running()) {112repos <- getOption("renv.tests.repos")113if (!is.null(repos))114return(repos)115}116117# retrieve current repos118repos <- getOption("repos")119120# ensure @CRAN@ entries are resolved121repos[repos == "@CRAN@"] <- cran122123# add in renv.bootstrap.repos if set124default <- c(FALLBACK = "https://cloud.r-project.org")125extra <- getOption("renv.bootstrap.repos", default = default)126repos <- c(repos, extra)127128# remove duplicates that might've snuck in129dupes <- duplicated(repos) | duplicated(names(repos))130repos[!dupes]131132}133134renv_bootstrap_repos_lockfile <- function() {135136lockpath <- Sys.getenv("RENV_PATHS_LOCKFILE", unset = "renv.lock")137if (!file.exists(lockpath))138return(NULL)139140lockfile <- tryCatch(renv_json_read(lockpath), error = identity)141if (inherits(lockfile, "error")) {142warning(lockfile)143return(NULL)144}145146repos <- lockfile$R$Repositories147if (length(repos) == 0)148return(NULL)149150keys <- vapply(repos, `[[`, "Name", FUN.VALUE = character(1))151vals <- vapply(repos, `[[`, "URL", FUN.VALUE = character(1))152names(vals) <- keys153154return(vals)155156}157158renv_bootstrap_download <- function(version) {159160# if the renv version number has 4 components, assume it must161# be retrieved via github162nv <- numeric_version(version)163components <- unclass(nv)[[1]]164165# if this appears to be a development version of 'renv', we'll166# try to restore from github167dev <- length(components) == 4L168169# begin collecting different methods for finding renv170methods <- c(171renv_bootstrap_download_tarball,172if (dev)173renv_bootstrap_download_github174else c(175renv_bootstrap_download_cran_latest,176renv_bootstrap_download_cran_archive177)178)179180for (method in methods) {181path <- tryCatch(method(version), error = identity)182if (is.character(path) && file.exists(path))183return(path)184}185186stop("failed to download renv ", version)187188}189190renv_bootstrap_download_impl <- function(url, destfile) {191192mode <- "wb"193194# https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17715195fixup <-196Sys.info()[["sysname"]] == "Windows" &&197substring(url, 1L, 5L) == "file:"198199if (fixup)200mode <- "w+b"201202args <- list(203url = url,204destfile = destfile,205mode = mode,206quiet = TRUE207)208209if ("headers" %in% names(formals(utils::download.file)))210args$headers <- renv_bootstrap_download_custom_headers(url)211212do.call(utils::download.file, args)213214}215216renv_bootstrap_download_custom_headers <- function(url) {217218headers <- getOption("renv.download.headers")219if (is.null(headers))220return(character())221222if (!is.function(headers))223stopf("'renv.download.headers' is not a function")224225headers <- headers(url)226if (length(headers) == 0L)227return(character())228229if (is.list(headers))230headers <- unlist(headers, recursive = FALSE, use.names = TRUE)231232ok <-233is.character(headers) &&234is.character(names(headers)) &&235all(nzchar(names(headers)))236237if (!ok)238stop("invocation of 'renv.download.headers' did not return a named character vector")239240headers241242}243244renv_bootstrap_download_cran_latest <- function(version) {245246spec <- renv_bootstrap_download_cran_latest_find(version)247type <- spec$type248repos <- spec$repos249250message("* Downloading renv ", version, " ... ", appendLF = FALSE)251252baseurl <- utils::contrib.url(repos = repos, type = type)253ext <- if (identical(type, "source"))254".tar.gz"255else if (Sys.info()[["sysname"]] == "Windows")256".zip"257else258".tgz"259name <- sprintf("renv_%s%s", version, ext)260url <- paste(baseurl, name, sep = "/")261262destfile <- file.path(tempdir(), name)263status <- tryCatch(264renv_bootstrap_download_impl(url, destfile),265condition = identity266)267268if (inherits(status, "condition")) {269message("FAILED")270return(FALSE)271}272273# report success and return274message("OK (downloaded ", type, ")")275destfile276277}278279renv_bootstrap_download_cran_latest_find <- function(version) {280281# check whether binaries are supported on this system282binary <-283getOption("renv.bootstrap.binary", default = TRUE) &&284!identical(.Platform$pkgType, "source") &&285!identical(getOption("pkgType"), "source") &&286Sys.info()[["sysname"]] %in% c("Darwin", "Windows")287288types <- c(if (binary) "binary", "source")289290# iterate over types + repositories291for (type in types) {292for (repos in renv_bootstrap_repos()) {293294# retrieve package database295db <- tryCatch(296as.data.frame(297utils::available.packages(type = type, repos = repos),298stringsAsFactors = FALSE299),300error = identity301)302303if (inherits(db, "error"))304next305306# check for compatible entry307entry <- db[db$Package %in% "renv" & db$Version %in% version, ]308if (nrow(entry) == 0)309next310311# found it; return spec to caller312spec <- list(entry = entry, type = type, repos = repos)313return(spec)314315}316}317318# if we got here, we failed to find renv319fmt <- "renv %s is not available from your declared package repositories"320stop(sprintf(fmt, version))321322}323324renv_bootstrap_download_cran_archive <- function(version) {325326name <- sprintf("renv_%s.tar.gz", version)327repos <- renv_bootstrap_repos()328urls <- file.path(repos, "src/contrib/Archive/renv", name)329destfile <- file.path(tempdir(), name)330331message("* Downloading renv ", version, " ... ", appendLF = FALSE)332333for (url in urls) {334335status <- tryCatch(336renv_bootstrap_download_impl(url, destfile),337condition = identity338)339340if (identical(status, 0L)) {341message("OK")342return(destfile)343}344345}346347message("FAILED")348return(FALSE)349350}351352renv_bootstrap_download_tarball <- function(version) {353354# if the user has provided the path to a tarball via355# an environment variable, then use it356tarball <- Sys.getenv("RENV_BOOTSTRAP_TARBALL", unset = NA)357if (is.na(tarball))358return()359360# allow directories361if (dir.exists(tarball)) {362name <- sprintf("renv_%s.tar.gz", version)363tarball <- file.path(tarball, name)364}365366# bail if it doesn't exist367if (!file.exists(tarball)) {368369# let the user know we weren't able to honour their request370fmt <- "* RENV_BOOTSTRAP_TARBALL is set (%s) but does not exist."371msg <- sprintf(fmt, tarball)372warning(msg)373374# bail375return()376377}378379fmt <- "* Bootstrapping with tarball at path '%s'."380msg <- sprintf(fmt, tarball)381message(msg)382383tarball384385}386387renv_bootstrap_download_github <- function(version) {388389enabled <- Sys.getenv("RENV_BOOTSTRAP_FROM_GITHUB", unset = "TRUE")390if (!identical(enabled, "TRUE"))391return(FALSE)392393# prepare download options394pat <- Sys.getenv("GITHUB_PAT")395if (nzchar(Sys.which("curl")) && nzchar(pat)) {396fmt <- "--location --fail --header \"Authorization: token %s\""397extra <- sprintf(fmt, pat)398saved <- options("download.file.method", "download.file.extra")399options(download.file.method = "curl", download.file.extra = extra)400on.exit(do.call(base::options, saved), add = TRUE)401} else if (nzchar(Sys.which("wget")) && nzchar(pat)) {402fmt <- "--header=\"Authorization: token %s\""403extra <- sprintf(fmt, pat)404saved <- options("download.file.method", "download.file.extra")405options(download.file.method = "wget", download.file.extra = extra)406on.exit(do.call(base::options, saved), add = TRUE)407}408409message("* Downloading renv ", version, " from GitHub ... ", appendLF = FALSE)410411url <- file.path("https://api.github.com/repos/rstudio/renv/tarball", version)412name <- sprintf("renv_%s.tar.gz", version)413destfile <- file.path(tempdir(), name)414415status <- tryCatch(416renv_bootstrap_download_impl(url, destfile),417condition = identity418)419420if (!identical(status, 0L)) {421message("FAILED")422return(FALSE)423}424425message("OK")426return(destfile)427428}429430renv_bootstrap_install <- function(version, tarball, library) {431432# attempt to install it into project library433message("* Installing renv ", version, " ... ", appendLF = FALSE)434dir.create(library, showWarnings = FALSE, recursive = TRUE)435436# invoke using system2 so we can capture and report output437bin <- R.home("bin")438exe <- if (Sys.info()[["sysname"]] == "Windows") "R.exe" else "R"439r <- file.path(bin, exe)440441args <- c(442"--vanilla", "CMD", "INSTALL", "--no-multiarch",443"-l", shQuote(path.expand(library)),444shQuote(path.expand(tarball))445)446447output <- system2(r, args, stdout = TRUE, stderr = TRUE)448message("Done!")449450# check for successful install451status <- attr(output, "status")452if (is.numeric(status) && !identical(status, 0L)) {453header <- "Error installing renv:"454lines <- paste(rep.int("=", nchar(header)), collapse = "")455text <- c(header, lines, output)456writeLines(text, con = stderr())457}458459status460461}462463renv_bootstrap_platform_prefix <- function() {464465# construct version prefix466version <- paste(R.version$major, R.version$minor, sep = ".")467prefix <- paste("R", numeric_version(version)[1, 1:2], sep = "-")468469# include SVN revision for development versions of R470# (to avoid sharing platform-specific artefacts with released versions of R)471devel <-472identical(R.version[["status"]], "Under development (unstable)") ||473identical(R.version[["nickname"]], "Unsuffered Consequences")474475if (devel)476prefix <- paste(prefix, R.version[["svn rev"]], sep = "-r")477478# build list of path components479components <- c(prefix, R.version$platform)480481# include prefix if provided by user482prefix <- renv_bootstrap_platform_prefix_impl()483if (!is.na(prefix) && nzchar(prefix))484components <- c(prefix, components)485486# build prefix487paste(components, collapse = "/")488489}490491renv_bootstrap_platform_prefix_impl <- function() {492493# if an explicit prefix has been supplied, use it494prefix <- Sys.getenv("RENV_PATHS_PREFIX", unset = NA)495if (!is.na(prefix))496return(prefix)497498# if the user has requested an automatic prefix, generate it499auto <- Sys.getenv("RENV_PATHS_PREFIX_AUTO", unset = NA)500if (auto %in% c("TRUE", "True", "true", "1"))501return(renv_bootstrap_platform_prefix_auto())502503# empty string on failure504""505506}507508renv_bootstrap_platform_prefix_auto <- function() {509510prefix <- tryCatch(renv_bootstrap_platform_os(), error = identity)511if (inherits(prefix, "error") || prefix %in% "unknown") {512513msg <- paste(514"failed to infer current operating system",515"please file a bug report at https://github.com/rstudio/renv/issues",516sep = "; "517)518519warning(msg)520521}522523prefix524525}526527renv_bootstrap_platform_os <- function() {528529sysinfo <- Sys.info()530sysname <- sysinfo[["sysname"]]531532# handle Windows + macOS up front533if (sysname == "Windows")534return("windows")535else if (sysname == "Darwin")536return("macos")537538# check for os-release files539for (file in c("/etc/os-release", "/usr/lib/os-release"))540if (file.exists(file))541return(renv_bootstrap_platform_os_via_os_release(file, sysinfo))542543# check for redhat-release files544if (file.exists("/etc/redhat-release"))545return(renv_bootstrap_platform_os_via_redhat_release())546547"unknown"548549}550551renv_bootstrap_platform_os_via_os_release <- function(file, sysinfo) {552553# read /etc/os-release554release <- utils::read.table(555file = file,556sep = "=",557quote = c("\"", "'"),558col.names = c("Key", "Value"),559comment.char = "#",560stringsAsFactors = FALSE561)562563vars <- as.list(release$Value)564names(vars) <- release$Key565566# get os name567os <- tolower(sysinfo[["sysname"]])568569# read id570id <- "unknown"571for (field in c("ID", "ID_LIKE")) {572if (field %in% names(vars) && nzchar(vars[[field]])) {573id <- vars[[field]]574break575}576}577578# read version579version <- "unknown"580for (field in c("UBUNTU_CODENAME", "VERSION_CODENAME", "VERSION_ID", "BUILD_ID")) {581if (field %in% names(vars) && nzchar(vars[[field]])) {582version <- vars[[field]]583break584}585}586587# join together588paste(c(os, id, version), collapse = "-")589590}591592renv_bootstrap_platform_os_via_redhat_release <- function() {593594# read /etc/redhat-release595contents <- readLines("/etc/redhat-release", warn = FALSE)596597# infer id598id <- if (grepl("centos", contents, ignore.case = TRUE))599"centos"600else if (grepl("redhat", contents, ignore.case = TRUE))601"redhat"602else603"unknown"604605# try to find a version component (very hacky)606version <- "unknown"607608parts <- strsplit(contents, "[[:space:]]")[[1L]]609for (part in parts) {610611nv <- tryCatch(numeric_version(part), error = identity)612if (inherits(nv, "error"))613next614615version <- nv[1, 1]616break617618}619620paste(c("linux", id, version), collapse = "-")621622}623624renv_bootstrap_library_root_name <- function(project) {625626# use project name as-is if requested627asis <- Sys.getenv("RENV_PATHS_LIBRARY_ROOT_ASIS", unset = "FALSE")628if (asis)629return(basename(project))630631# otherwise, disambiguate based on project's path632id <- substring(renv_bootstrap_hash_text(project), 1L, 8L)633paste(basename(project), id, sep = "-")634635}636637renv_bootstrap_library_root <- function(project) {638639prefix <- renv_bootstrap_profile_prefix()640641path <- Sys.getenv("RENV_PATHS_LIBRARY", unset = NA)642if (!is.na(path))643return(paste(c(path, prefix), collapse = "/"))644645path <- renv_bootstrap_library_root_impl(project)646if (!is.null(path)) {647name <- renv_bootstrap_library_root_name(project)648return(paste(c(path, prefix, name), collapse = "/"))649}650651renv_bootstrap_paths_renv("library", project = project)652653}654655renv_bootstrap_library_root_impl <- function(project) {656657root <- Sys.getenv("RENV_PATHS_LIBRARY_ROOT", unset = NA)658if (!is.na(root))659return(root)660661type <- renv_bootstrap_project_type(project)662if (identical(type, "package")) {663userdir <- renv_bootstrap_user_dir()664return(file.path(userdir, "library"))665}666667}668669renv_bootstrap_validate_version <- function(version) {670671loadedversion <- utils::packageDescription("renv", fields = "Version")672if (version == loadedversion)673return(TRUE)674675# assume four-component versions are from GitHub;676# three-component versions are from CRAN677components <- strsplit(loadedversion, "[.-]")[[1]]678remote <- if (length(components) == 4L)679paste("rstudio/renv", loadedversion, sep = "@")680else681paste("renv", loadedversion, sep = "@")682683fmt <- paste(684"renv %1$s was loaded from project library, but this project is configured to use renv %2$s.",685"Use `renv::record(\"%3$s\")` to record renv %1$s in the lockfile.",686"Use `renv::restore(packages = \"renv\")` to install renv %2$s into the project library.",687sep = "\n"688)689690msg <- sprintf(fmt, loadedversion, version, remote)691warning(msg, call. = FALSE)692693FALSE694695}696697renv_bootstrap_hash_text <- function(text) {698699hashfile <- tempfile("renv-hash-")700on.exit(unlink(hashfile), add = TRUE)701702writeLines(text, con = hashfile)703tools::md5sum(hashfile)704705}706707renv_bootstrap_load <- function(project, libpath, version) {708709# try to load renv from the project library710if (!requireNamespace("renv", lib.loc = libpath, quietly = TRUE))711return(FALSE)712713# warn if the version of renv loaded does not match714renv_bootstrap_validate_version(version)715716# execute renv load hooks, if any717hooks <- getHook("renv::autoload")718for (hook in hooks)719if (is.function(hook))720tryCatch(hook(), error = warning)721722# load the project723renv::load(project)724725TRUE726727}728729renv_bootstrap_profile_load <- function(project) {730731# if RENV_PROFILE is already set, just use that732profile <- Sys.getenv("RENV_PROFILE", unset = NA)733if (!is.na(profile) && nzchar(profile))734return(profile)735736# check for a profile file (nothing to do if it doesn't exist)737path <- renv_bootstrap_paths_renv("profile", profile = FALSE, project = project)738if (!file.exists(path))739return(NULL)740741# read the profile, and set it if it exists742contents <- readLines(path, warn = FALSE)743if (length(contents) == 0L)744return(NULL)745746# set RENV_PROFILE747profile <- contents[[1L]]748if (!profile %in% c("", "default"))749Sys.setenv(RENV_PROFILE = profile)750751profile752753}754755renv_bootstrap_profile_prefix <- function() {756profile <- renv_bootstrap_profile_get()757if (!is.null(profile))758return(file.path("profiles", profile, "renv"))759}760761renv_bootstrap_profile_get <- function() {762profile <- Sys.getenv("RENV_PROFILE", unset = "")763renv_bootstrap_profile_normalize(profile)764}765766renv_bootstrap_profile_set <- function(profile) {767profile <- renv_bootstrap_profile_normalize(profile)768if (is.null(profile))769Sys.unsetenv("RENV_PROFILE")770else771Sys.setenv(RENV_PROFILE = profile)772}773774renv_bootstrap_profile_normalize <- function(profile) {775776if (is.null(profile) || profile %in% c("", "default"))777return(NULL)778779profile780781}782783renv_bootstrap_path_absolute <- function(path) {784785substr(path, 1L, 1L) %in% c("~", "/", "\\") || (786substr(path, 1L, 1L) %in% c(letters, LETTERS) &&787substr(path, 2L, 3L) %in% c(":/", ":\\")788)789790}791792renv_bootstrap_paths_renv <- function(..., profile = TRUE, project = NULL) {793renv <- Sys.getenv("RENV_PATHS_RENV", unset = "renv")794root <- if (renv_bootstrap_path_absolute(renv)) NULL else project795prefix <- if (profile) renv_bootstrap_profile_prefix()796components <- c(root, renv, prefix, ...)797paste(components, collapse = "/")798}799800renv_bootstrap_project_type <- function(path) {801802descpath <- file.path(path, "DESCRIPTION")803if (!file.exists(descpath))804return("unknown")805806desc <- tryCatch(807read.dcf(descpath, all = TRUE),808error = identity809)810811if (inherits(desc, "error"))812return("unknown")813814type <- desc$Type815if (!is.null(type))816return(tolower(type))817818package <- desc$Package819if (!is.null(package))820return("package")821822"unknown"823824}825826renv_bootstrap_user_dir <- function() {827dir <- renv_bootstrap_user_dir_impl()828path.expand(chartr("\\", "/", dir))829}830831renv_bootstrap_user_dir_impl <- function() {832833# use local override if set834override <- getOption("renv.userdir.override")835if (!is.null(override))836return(override)837838# use R_user_dir if available839tools <- asNamespace("tools")840if (is.function(tools$R_user_dir))841return(tools$R_user_dir("renv", "cache"))842843# try using our own backfill for older versions of R844envvars <- c("R_USER_CACHE_DIR", "XDG_CACHE_HOME")845for (envvar in envvars) {846root <- Sys.getenv(envvar, unset = NA)847if (!is.na(root))848return(file.path(root, "R/renv"))849}850851# use platform-specific default fallbacks852if (Sys.info()[["sysname"]] == "Windows")853file.path(Sys.getenv("LOCALAPPDATA"), "R/cache/R/renv")854else if (Sys.info()[["sysname"]] == "Darwin")855"~/Library/Caches/org.R-project.R/R/renv"856else857"~/.cache/R/renv"858859}860861862renv_json_read <- function(file = NULL, text = NULL) {863864jlerr <- NULL865866# if jsonlite is loaded, use that instead867if ("jsonlite" %in% loadedNamespaces()) {868869json <- catch(renv_json_read_jsonlite(file, text))870if (!inherits(json, "error"))871return(json)872873jlerr <- json874875}876877# otherwise, fall back to the default JSON reader878json <- catch(renv_json_read_default(file, text))879if (!inherits(json, "error"))880return(json)881882# report an error883if (!is.null(jlerr))884stop(jlerr)885else886stop(json)887888}889890renv_json_read_jsonlite <- function(file = NULL, text = NULL) {891text <- paste(text %||% read(file), collapse = "\n")892jsonlite::fromJSON(txt = text, simplifyVector = FALSE)893}894895renv_json_read_default <- function(file = NULL, text = NULL) {896897# find strings in the JSON898text <- paste(text %||% read(file), collapse = "\n")899pattern <- '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'900locs <- gregexpr(pattern, text, perl = TRUE)[[1]]901902# if any are found, replace them with placeholders903replaced <- text904strings <- character()905replacements <- character()906907if (!identical(c(locs), -1L)) {908909# get the string values910starts <- locs911ends <- locs + attr(locs, "match.length") - 1L912strings <- substring(text, starts, ends)913914# only keep those requiring escaping915strings <- grep("[[\\]{}:]", strings, perl = TRUE, value = TRUE)916917# compute replacements918replacements <- sprintf('"\032%i\032"', seq_along(strings))919920# replace the strings921mapply(function(string, replacement) {922replaced <<- sub(string, replacement, replaced, fixed = TRUE)923}, strings, replacements)924925}926927# transform the JSON into something the R parser understands928transformed <- replaced929transformed <- gsub("{}", "`names<-`(list(), character())", transformed, fixed = TRUE)930transformed <- gsub("[[{]", "list(", transformed, perl = TRUE)931transformed <- gsub("[]}]", ")", transformed, perl = TRUE)932transformed <- gsub(":", "=", transformed, fixed = TRUE)933text <- paste(transformed, collapse = "\n")934935# parse it936json <- parse(text = text, keep.source = FALSE, srcfile = NULL)[[1L]]937938# construct map between source strings, replaced strings939map <- as.character(parse(text = strings))940names(map) <- as.character(parse(text = replacements))941942# convert to list943map <- as.list(map)944945# remap strings in object946remapped <- renv_json_remap(json, map)947948# evaluate949eval(remapped, envir = baseenv())950951}952953renv_json_remap <- function(json, map) {954955# fix names956if (!is.null(names(json))) {957lhs <- match(names(json), names(map), nomatch = 0L)958rhs <- match(names(map), names(json), nomatch = 0L)959names(json)[rhs] <- map[lhs]960}961962# fix values963if (is.character(json))964return(map[[json]] %||% json)965966# handle true, false, null967if (is.name(json)) {968text <- as.character(json)969if (text == "true")970return(TRUE)971else if (text == "false")972return(FALSE)973else if (text == "null")974return(NULL)975}976977# recurse978if (is.recursive(json)) {979for (i in seq_along(json)) {980json[i] <- list(renv_json_remap(json[[i]], map))981}982}983984json985986}987988# load the renv profile, if any989renv_bootstrap_profile_load(project)990991# construct path to library root992root <- renv_bootstrap_library_root(project)993994# construct library prefix for platform995prefix <- renv_bootstrap_platform_prefix()996997# construct full libpath998libpath <- file.path(root, prefix)9991000# attempt to load1001if (renv_bootstrap_load(project, libpath, version))1002return(TRUE)10031004# load failed; inform user we're about to bootstrap1005prefix <- paste("# Bootstrapping renv", version)1006postfix <- paste(rep.int("-", 77L - nchar(prefix)), collapse = "")1007header <- paste(prefix, postfix)1008message(header)10091010# perform bootstrap1011bootstrap(version, libpath)10121013# exit early if we're just testing bootstrap1014if (!is.na(Sys.getenv("RENV_BOOTSTRAP_INSTALL_ONLY", unset = NA)))1015return(TRUE)10161017# try again to load1018if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE)) {1019message("* Successfully installed and loaded renv ", version, ".")1020return(renv::load())1021}10221023# failed to download or load renv; warn the user1024msg <- c(1025"Failed to find an renv installation: the project will not be loaded.",1026"Use `renv::activate()` to re-initialize the project."1027)10281029warning(paste(msg, collapse = "\n"), call. = FALSE)10301031})103210331034