You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用SpaDES.project::setupProject与experiment2创建两个simList对象?

问题

我已完成项目搭建,目标是创建两个simList对象并传入SpaDES.experiment::experiment2,但因通过...传递模拟所需对象导致失败。请问我是否错误使用了...?若否,该如何传递额外对象?

相关代码如下:

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)
解决方案

1. ...参数的使用判断

你没有错误使用...,但SpaDES.experiment::experiment2的...是用来传递给底层simInit或模拟运行的额外参数,而非直接传递模拟所需的对象。对象需要提前注入到simList中,不能通过...传递。

2. 传递额外对象的正确方法

方法一:在simInit阶段注入对象

如果需要添加额外对象,可以直接在projOut的objects列表中定义,之后再初始化simList:

# 添加需要的额外对象到projOut
projOut$objects <- list(studyAreaRas = studyAreaRas, extraObj = yourExtraObject)
# 初始化simList
wrkflwMaxEnt <- do.call(simInit, projOut)

方法二:给已创建的simList添加对象

如果已经生成了simList,可以用SpaDES.core::addObjects动态添加对象:

# 给已有的simList添加额外对象
wrkflwMaxEnt <- addObjects(wrkflwMaxEnt, extraObj = yourExtraObject)

方法三:在experiment2中统一传递对象

experiment2支持通过专属的objects参数,统一传递所有模拟需要的共享对象:

experiment2(
  simList = list(wrkflwMaxEnt, wrkflwOtherModel),
  objects = list(studyAreaRas = studyAreaRas, extraObj = yourExtraObject),
  # 其他实验参数(如重复次数)
  replicates = 3
)

3. 针对你的场景的具体调整

你当前已初始化一个MaxEnt模型的simList,若要创建第二个(比如GLM模型),可复制配置修改参数后重新初始化,再将两个simList传入experiment2:

# 创建第二个simList(GLM模型)
projOutGLM <- projOut
projOutGLM$params$projectSpeciesDist$statModel <- "GLM"
wrkflwGLM <- do.call(simInit, projOutGLM)

# 传入experiment2运行实验
experimentResults <- SpaDES.experiment::experiment2(
  simList = list(wrkflwMaxEnt, wrkflwGLM),
  # 若有未注入simList的额外对象,在此传递
  objects = list(additionalObj = yourAdditionalObj),
  replicates = 3
)

内容的提问来源于stack exchange,提问作者Ceres

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 08:40:06