From df1bbfb5e927b3733b333a1d195d2a4f68f23d19 Mon Sep 17 00:00:00 2001 From: Anthony Sena Date: Wed, 13 Sep 2023 12:14:46 -0400 Subject: [PATCH] Preserve renv config options --- R/ModuleEnv.R | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/R/ModuleEnv.R b/R/ModuleEnv.R index c95b58f9..62878711 100644 --- a/R/ModuleEnv.R +++ b/R/ModuleEnv.R @@ -67,6 +67,20 @@ withModuleRenv <- function(code, script <- gsub(name, rep, script) } + # Attach renv options() from the calling environment to the renv::run context + # renv options are prefixed with "renv." as described in + # https://rstudio.github.io/renv/reference/config.html + envOptions <- options() + renvOptions <- envOptions[grepl("renv\\.", names(envOptions))] + if (length(renvOptions) > 0) { + for (i in 1:length(renvOptions)) { + script <- c(.copyOptionForScript( + optionName = names(renvOptions)[[i]], + optionValue = renvOptions[[i]] + ), script) + } + } + # Enforce attachment of Strategus from calling process - note one inside the renv if (useLocalStrategusLibrary) { script <- c(.getLocalLibraryScipt("Strategus"), script) @@ -96,3 +110,15 @@ withModuleRenv <- function(code, libPath <- file.path(find.package(x), "../") sprintf("library(%s, lib.loc = '%s')", x, libPath) } + +.copyOptionForScript <- function(optionName, optionValue) { + if (is.logical(optionValue) || is.numeric(optionValue)) { + sprintf("options(%s = %s)", optionName, optionValue) + } else if (is.character(optionValue) && length(optionValue) == 1) { + sprintf("options(%s = '%s')", optionName, optionValue) + } else if (is.character(optionValue) && length(optionValue) > 1) { + sprintf("options(%s = c('%s'))", optionName, paste(optionValue, collapse = "','")) + } else { + paste0("# option = ", optionName, " - could not be passed to this file, likely because it is a function.") + } +}