1

I'm trying to use setupProjector setupModules to download/source .R files from github. I'm clearly doing something wrong as the following is not adding any modules to the simPath$modulePath as I would expect it to? Or is it not supposed to do that?

In particular I want to:

  1. create a new SpaDES project called "SpaDES4Dummies_Part2" in a temporary directory ()
  2. in <tempdir>/SpaDES4Dummies_Part2 I want to create the necessary folder structure (inputs, modules, outputs, cache, etc)
  3. I want to "populate" <tempdir>/SpaDES4Dummies_Part2/modules with modules from the CeresBarros/SpaDES4Dummies GitHub repo
mainPath <- file.path(tempdir(), "SpaDES4Dummies_Part2")
pkgPath <- file.path(mainPath, "packages", version$platform,
                     paste0(version$major, ".", strsplit(version$minor, "[.]")[[1]][1]))
dir.create(pkgPath, recursive = TRUE)
.libPaths(pkgPath, include.site = FALSE) ## install packages in project library (proj-lib)

if (!"Require" %in% installed.packages(lib.loc = pkgPath))
  install.packages("Require")

## use binary linux packages if on Ubuntu
Require::setLinuxBinaryRepo()

Require::Require(c("PredictiveEcology/SpaDES.project@4fbd77466d5b1f3bef15d7fd383ed797fd92aa7a",
                   "SpaDES.core"),
                 require = FALSE, upgrade = FALSE, standAlone = TRUE)

library(SpaDES.project)

## setup project paths
SpaDES.core::setPaths(cachePath = file.path(mainPath, "cache"),
                      inputPath = file.path(mainPath, "inputs"),
                      modulePath = file.path(mainPath, "modules"),
                      outputPath = file.path(mainPath, "outputs"))

simPaths <- SpaDES.core::getPaths() 
simPaths$packagePath <- pkgPath

setupProject(name = "SpaDES4Dummies_Part2", 
             paths = simPaths,
             modules = c("https://github.com/CeresBarros/SpaDES4Dummies/blob/master/modules/climateData/climateData.R",
                         "https://github.com/CeresBarros/SpaDES4Dummies/blob/master/modules/speciesAbundanceData/speciesAbundanceData.R",
                         "https://github.com/CeresBarros/SpaDES4Dummies/blob/master/modules/projectSpeciesDist/projectSpeciesDist.R"),
             options = list("reproducible.cachePath" = simPaths$cachePath,
                            "reproducible.destinationPath" = simPaths$inputPath,
                            "reproducible.rasterRead" = "terra::rast",
                            "reproducible.useTerra" = TRUE))
Ceres
  • 163
  • 7

1 Answers1

1

There were multiple issues in the code above:

  1. single .R files will not be sufficient as SpaDES modules involve more than one .R script. Instead we need to get the full contents of the CeresBarros/SpaDES4Dummies repository and then keep only the desired modules.
  2. we do not need to create the simulation paths a priori. Passing a single path to the desired project directory is enough.
  3. We also don't need to setup the package directory or pre-install any packages other than SpaDES.project. setupProject deals with this and with copying any necessary packages to the project-level package directory

As a result, the code can be much simplified to achieve the desired output:

## 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")))

library(SpaDES.project)


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"),
                        options = list("reproducible.rasterRead" = "terra::rast",
                                       "reproducible.useTerra" = TRUE))

## 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
Ceres
  • 163
  • 7