Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new spadesOptions -- spades.reqdPkgsDontLoad - to explicitly prevent loading #290

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Description: Provides the core framework for a discrete event system to
URL:
https://spades-core.predictiveecology.org/,
https://github.com/PredictiveEcology/SpaDES.core
Date: 2024-10-13
Version: 2.1.5.9002
Date: 2024-11-13
Version: 2.1.5.9003
Authors@R: c(
person("Alex M", "Chubaty", , "achubaty@for-cast.ca", role = c("aut"),
comment = c(ORCID = "0000-0001-7146-8135")),
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

* fix issue with `Plots()` where plots were discarded if no filename was specified;
* minor documentation improvements;
* new option: `spades.reqdPkgsDontLoad`, a character vector. If anything is specified,
then it will not be loaded with `require` or `library`, but it will be installed, if needed,
and if `spades.useRequire = TRUE`, which is the default. Default for this new option is
"box", which is one of potentially many in the R universe that throws an error if it is
loaded.

# SpaDES.core 2.1.5

Expand Down
6 changes: 6 additions & 0 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
#' and the differences can be seen in a hidden object in the stashed `simList`.
#' There is a message which describes how to find that. \cr
#'
#' `spades.reqdPkgsDontLoad` \tab `"box"` \tab Specify any packages that should not
#' be \emph{loaded} i.e., no `library` or `require`, but they should be installed if
#' listed. The default (`"box"`) is a package that returns a warning if it is
#' loaded, and so it is excluded from loading.\cr
#'
#' `spades.saveFileExtensions` \tab `NULL` \tab
#' a `data.frame` with 3 columns, `exts`, `fun`, and `package` indicating which
#' file extension, and which function from which package will be used when
Expand Down Expand Up @@ -217,6 +222,7 @@ spadesOptions <- function() {
spades.plots = NULL,
spades.qsThreads = 1L,
spades.recoveryMode = 1,
spades.reqdPkgsDontLoad = "box",
spades.restartRInterval = 0,
spades.restartR.clearFiles = TRUE,
spades.restartR.RDataFilename = "sim_restartR.RData",
Expand Down
12 changes: 12 additions & 0 deletions R/simulation-parseModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -643,3 +643,15 @@ newEnvsByModule <- function(sim, modu) {
sim@.xData$.mods[[modu]]$.objects <- new.env(parent = emptyenv())
sim
}


reqdPkgsDontLoad <- function(allPkgs, pkgsDontLoad = getOption("spades.reqdPkgsDontLoad", NULL)) {
if (spadesReqdPkgsDontLoad(pkgsDontLoad)) {
allPkgs <- allPkgs[!Require::extractPkgName(allPkgs) %in% pkgsDontLoad]
}
allPkgs
}

spadesReqdPkgsDontLoad <- function(pkgsDontLoad) {
is.character(pkgsDontLoad)
}
11 changes: 10 additions & 1 deletion R/simulation-simInit.R
Original file line number Diff line number Diff line change
Expand Up @@ -1547,9 +1547,18 @@ loadPkgs <- function(reqdPkgs) {
# Check for SpaDES.core minimum version
checkSpaDES.coreMinVersion(allPkgs)
allPkgs <- grep("^SpaDES.core\\>", allPkgs, value = TRUE, invert = TRUE)

pkgsDontLoad <- getOption("spades.reqdPkgsDontLoad", NULL)
allPkgs <- reqdPkgsDontLoad(allPkgs, pkgsDontLoad)

if (getOption("spades.useRequire")) {
getCRANrepos(ind = 1) # running this first is neutral if it is set
Require(allPkgs, standAlone = FALSE, upgrade = FALSE)
Require(allPkgs, require = require, standAlone = FALSE, upgrade = FALSE)
if (!is.null(pkgsDontLoad)) {
verbose <- getOption("reproducible.verbose")
Require::Require(pkgsDontLoad, require = FALSE, standAlone = FALSE,
upgrade = FALSE, verbose = verbose - 1)
}
# RequireWithHandling(allPkgs, standAlone = FALSE, upgrade = FALSE)
} else {
allPkgs <- unique(Require::extractPkgName(allPkgs))
Expand Down
5 changes: 5 additions & 0 deletions man/spadesOptions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions tests/testthat/test-simulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -1037,3 +1037,63 @@ test_that("debug using logging", {
})
expect_true(length(mess1) == 0)
})

test_that("options('reproducible.reqdPkgsDontLoad", {
dontLoad <- "ggplot2" # can't be sp, raster because already loaded

skip_if_not_installed(dontLoad)
unloadNamespace(dontLoad)
withr::local_options(spades.reqdPkgsDontLoad = dontLoad)

testInit()

newModule("test", tmpdir, open = FALSE)

# Sept 18 2018 -- Changed to use "seconds" -- better comparison with simple loop
cat(file = file.path(tmpdir, "test", "test.R"), '
defineModule(sim, list(
name = "test",
description = "insert module description here",
keywords = c("insert key words here"),
authors = person(c("Eliot", "J", "B"), "McIntire", email = "eliot.mcintire@nrcan-rncan.gc.ca", role = c("aut", "cre")),
childModules = character(0),
version = list(SpaDES.core = "0.1.0", test = "0.0.1"),
spatialExtent = terra::ext(rep(0, 4)),
timeframe = as.POSIXlt(c(NA, NA)),
timeunit = "year",
citation = list("citation.bib"),
documentation = list("README.md", "test.Rmd"),',
paste0(" reqdPkgs = list(\'", dontLoad, "\'),"),'
parameters = rbind(
),
inputObjects = bindrows(
),
outputObjects = bindrows(
)
))

doEvent.test = function(sim, eventTime, eventType, debug = FALSE) {
switch(
eventType,
init = {
})
return(invisible(sim))
}

', fill = TRUE)
expect_false(isNamespaceLoaded(dontLoad))
warn <- capture_warnings(
sim <- simInit(modules = "test", paths = list(modulePath = tmpdir),
times = list(start = 0, end = 1, timeunit = "year"))
)
expect_false(isNamespaceLoaded(dontLoad))

options(spades.reqdPkgsDontLoad = NULL)
warn <- capture_warnings(
sim <- simInit(modules = "test", paths = list(modulePath = tmpdir),
times = list(start = 0, end = 1, timeunit = "year"))
)
expect_true(isNamespaceLoaded(dontLoad))
unloadNamespace(dontLoad)

})
Loading