rworkflowreproducible-research

How can I use `SpaDES.project::setupProject` and `SpaDES.experiment::experiment2`?


I've setup my project, but my goal is to create two simList objects that I want to pass to experiment2. This fails because I'm passing an object needed in the simulation via .... Am I using ... incorrectly? If not, how can I pass additional objects?

options(repos = c(CRAN = "https://cloud.r-project.org"))

if (getRversion() < "4.2.1") {
  warning(paste("dismo::maxent may create a fatal error",
                "when using R version < v4.2.1 and from RStudio.\n", 
                "Please upgrade R, or run this script outside of RStudio.\n",
                "See https://github.com/rspatial/dismo/issues/13"))
}

## check Java
if (!require(rJava)) {
  stop(paste("Your Java installation may have problems, please check.\n", 
             "See https://www.java.com/en/download/manual.jsp for Java installation.\n",
             "Alternatively, 'rJava' could be having issues assessing your system Java installation."))
}

## install SpaDES.project -- it'll setup everything for us ;)
if (!requireNamespace("SpaDES.project"))
  install.packages("SpaDES.project", repos= c("https://predictiveecology.r-universe.dev", getOption("repos")))

if (!requireNamespace("SpaDES.tools"))
  install.packages("SpaDES.tools", repos= c("https://predictiveecology.r-universe.dev", getOption("repos")))

if (!requireNamespace("terra"))
  install.packages("terra")

library(SpaDES.project)

## make a random study area.
##  Here use seed to make sure the same study area is always generated
studyArea <- SpaDES.tools::randomStudyArea(size = 1e10, seed = 123)
studyAreaRas <- terra::rasterize(studyArea, 
                                 terra::rast(extent = terra::ext(studyArea), 
                                             crs = terra::crs(studyArea, proj = TRUE), 
                                             resolution = 1000))

projOut <- setupProject(name = "SpaDES4Dummies_Part2",
                        paths = list(projectPath = normalizePath(file.path(tempdir(), "SpaDES4Dummies_Part2"))), ## use a temporary dir
                        modules = c("CeresBarros/SpaDES4Dummies"), ## get the full repo project, we'll work around it to keep only the modules we need
                        require = c("SpaDES.core",
                                    "ggpubr",
                                    "PredictiveEcology/SpaDES.experiment@development",
                                    "SpaDES.tools", 
                                    "DiagrammeR"),
                        options = list("reproducible.rasterRead" = "terra::rast",
                                       "reproducible.useTerra" = TRUE),
                        studyAreaRas = studyAreaRas
)

## only keep necessary modules
projOut$modules <- c("climateData", "speciesAbundanceData", "projectSpeciesDist") # <-- use only 3 modules
projOut$paths$modulePath <- "modules/SpaDES4Dummies/modules"  # specify that the actual module path is inside

## parameters/objects for workflow computations
projOut$times <- list(start = 1, end = 5, timeunit = "year")
projOut$params <- list(
  "speciesAbundanceData" = list(
    ".plots" = c("png"),
    ".useCache" = FALSE
  ),
  "climateData" = list(
    ".plots" = c("png"),
    ".useCache" = FALSE
  ),
  "projectSpeciesDist" = list(
    "statModel" = "MaxEnt",
    ".plots" = c("png"),
    ".useCache" = FALSE
  ))

## before we run the workflow, dismo needs a few tweaks to run MaxEnt
maxentFile <- reproducible::preProcess(targetFile = "maxent.jar",
                                       url = "https://github.com/mrmaxent/Maxent/blob/master/ArchivedReleases/3.4.4/maxent.jar?raw=true",
                                       destinationPath = projOut$paths$inputPath,
                                       fun = NA)
file.copy(from = maxentFile$targetFilePath, 
          to = file.path(system.file("java", package = "dismo"), "maxent.jar"))


## initialise workflows using MaxEnt (parameters set above)
## SpaDES.experiment::experiment2, will take care of subdirectories to store outputs
wrkflwMaxEnt <- do.call(simInit, projOut)

Solution

  • You just have to ?simInit to see what arguments it is expecting. So, in this case, you need them as objects...

        projOut$objects$studyArea <- SpaDES.tools::randomStudyArea(size = 1e10, seed = 123)
        projOut$objects$studyAreaRas <- terra::rasterize(projOut$objects$studyArea, 
                                         terra::rast(extent = terra::ext(projOut$objects$studyArea), 
                                                     crs = terra::crs(projOut$objects$studyArea, proj = TRUE), 
                                                     resolution = 1000))