Skip to content

Commit

Permalink
Merge pull request #4 from CeresBarros/achubaty-patch-1
Browse files Browse the repository at this point in the history
try fixing rmd render workflow
  • Loading branch information
Ceres Barros authored Oct 28, 2022
2 parents 8b860ba + 0a009a6 commit 7839175
Show file tree
Hide file tree
Showing 13 changed files with 5,887 additions and 343 deletions.
35 changes: 18 additions & 17 deletions .github/workflows/render-validation-rmd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:

- uses: r-lib/actions/setup-r@v2
with:
r-version: '4.2.1'
use-public-rspm: true

- name: Install additional system dependencies
Expand All @@ -47,36 +48,36 @@ jobs:
libxml2-dev \
python3-gdal
- name: Install remotes, knitr, rmarkdown, bookdown
- name: Set R library
run: |
install.packages("remotes")
remotes::install_cran("rmarkdown")
remotes::install_cran("knitr")
remotes::install_cran("bookdown")
pkgPath <- file.path("packages", version$platform, paste0(version$major, ".", strsplit(version$minor, "[.]")[[1]][1]))
dir.create(dirname(pkgPath), recursive = TRUE)
file.symlink(Sys.getenv("R_LIBS_USER"), pkgPath)
shell: Rscript {0}

- name: Install SpaDES packages
run: |
install.packages("SpaDES", dependencies = TRUE)
remotes::install_github("PredictiveEcology/SpaDES.docs@development")
shell: Rscript {0}
- uses: PredictiveEcology/actions/install-Require@v0
with:
GitTag: '5c44205bf407f613f53546be652a438ef1248147'

- uses: PredictiveEcology/actions/install-Rmd-pkgs@v0

- uses: PredictiveEcology/actions/install-SpaDES@v0

- name: Install module package dependencies
- name: Install additional packages
run: |
mods <- dir("R/SpaDES/m")
pkgs <- sapply(mods, function(m) SpaDES.core::packages(modules = m, paths = "R/SpaDES/m"))
pkgs <- unique(unlist(pkgs))
Require::Require(pkgs)
options(repos = c(CRAN = "https://packagemanager.rstudio.com/all/__linux__/focal/latest", CRAN = "https://cran.rstudio.com"))
Require::Require(c("PredictiveEcology/LandR@development",
"ggspatial", "ggpubr", "cowplot", "DiagrammeR"), require = FALSE)
shell: Rscript {0}

- name: Render validation Rmd
run: |
rmarkdown::render("R/SpaDES/global.Rmd", encoding = "UTF-8", output_dir = "R/SpaDES")
rmarkdown::render("R/SpaDES/global.Rmd", encoding = "UTF-8", output_dir = "R/SpaDES", knit_root_dir = getwd())
shell: Rscript {0}

- name: Commit results
run: |
git config user.email "[email protected]"
git config user.name "GitHub Actions"
git commit Biomass_borealDataPrep.html -m 'Re-build Biomass_borealDataPrep.Rmd' || echo "No changes to commit"
git commit R/SpaDES/global.html -m 'Re-build R/SpaDES/global.Rmd' || echo "No changes to commit"
git push https://${{github.actor}}:${{secrets.GITHUB_TOKEN}}@github.com/${{github.repository}}.git HEAD:${{github.ref}} || echo "No changes to commit"
2 changes: 0 additions & 2 deletions CHECKSUMS.txt

This file was deleted.

Binary file not shown.
24 changes: 12 additions & 12 deletions R/SpaDES/global.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ if (!"remotes" %in% installed.packages()) {
}

if (!"Require" %in% installed.packages(lib.loc = pkgPath) ||
packageVersion("Require", lib.loc = pkgPath) < "0.1.6.9005") {
remotes::install_github("PredictiveEcology/Require@8ac756d66bc145f226f39e703e3787d3aed159f2",
packageVersion("Require", lib.loc = pkgPath) < "0.1.6.9014") {
remotes::install_github("PredictiveEcology/Require@5c44205bf407f613f53546be652a438ef1248147",
upgrade = FALSE, force = TRUE)
}
## use binary linux packages if on Ubuntu
Expand All @@ -33,29 +33,30 @@ Require::Require("PredictiveEcology/SpaDES.project@6d7de6ee12fc967c7c60de44f1aa3

modulePath <- "R/SpaDES/m"
SpaDES.project::getModule(modulePath = modulePath,
c("PredictiveEcology/Biomass_speciesData@505fa065399da93e817424373ee1160e46703ce3",
"PredictiveEcology/Biomass_borealDataPrep@6cd0c1242cdb95f7432d37becfb2f8dd03642e76",
"PredictiveEcology/Biomass_core@5f7691af755a651579408f37bf292a7274b7678f",
"PredictiveEcology/Biomass_validationKNN@ec9b5362aa0d07eee844fcdb74e33abeeda89b4b",
"CeresBarros/Biomass_speciesParameters@31b66f22f915cd8b5774e5b56359dc2406691895"))
c("PredictiveEcology/Biomass_speciesData@1dc71ebc6181b6eb9b0813db2ca4184488312197",
"PredictiveEcology/Biomass_borealDataPrep@4f9858f767a2c19d2bceacf80047dd5eac661e73",
"PredictiveEcology/Biomass_core@d36a902c23283ddd1c38d6e59ce86047ccdf14d8",
"PredictiveEcology/Biomass_validationKNN@30829e0a9465513169a21c0477cfffd5f9a5c936",
"PredictiveEcology/Biomass_speciesParameters@c7970580c1a5e1817d8cfeb4675076748eb50917"))

pkgSnapshotFile <- file.path("packages",
paste0("pkgSnapshot_",
paste0(version$major, "_", strsplit(version$minor, "[.]")[[1]][1]),
".txt"))

if (file.exists(pkgSnapshotFile)) {
Require::Require(packageVersionFile = pkgSnapshotFile,
require = FALSE, standAlone = TRUE)
Require::Require(packageVersionFile = pkgSnapshotFile, require = FALSE, standAlone = TRUE)
} else {
outs <- SpaDES.project::packagesInModules(modulePath = modulePath)
Require::Require(c(unname(unlist(outs)),
"PredictiveEcology/SpaDES.experiment@development",
"PredictiveEcology/SpaDES.experiment@91bfad98d67ea2b7fcee3ea0115f8746e47534ad",
"devtools", "ggspatial", "ggpubr", "cowplot"),
require = FALSE, standAlone = TRUE)

## the next line is commented to prevent accidental execution, but can be run to generate a missing pkg snapshot
# Require::pkgSnapshot(pkgSnapshotFile, libPaths = pkgPath, standAlone = TRUE)
Require::pkgSnapshot(pkgSnapshotFile, libPaths = pkgPath,
standAlone = TRUE, exact = TRUE, purge = TRUE,
includeBase = FALSE)
}

## load packages
Expand Down Expand Up @@ -112,7 +113,6 @@ GAemail <- NULL
googledrive::drive_auth(email = GAemail)

## Get necessary objects like the study area.
x11() ## open a new plotting window - avoids errors if the current one is too small.
devtools::source_url(paste0("https://raw.githubusercontent.com/CeresBarros/",
"LandRBiomass_publication/repPkgInstall/R/SpaDES/",
"1_simObjects.R?raw=TRUE"))
Expand Down
83 changes: 42 additions & 41 deletions R/SpaDES/global.Rmd
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
---
title: "LandR Publication Simulations"
author: "Ceres Barros"
date: "22/01/2021"
date: "Last updated: `r Sys.Date()`"
output:
html_document:
bookdown::html_document2:
toc: true
toc_float: true
toc_depth: 4
theme: sandstone
number_sections: false
df_print: paged
keep_md: yes
editor_options:
chunk_output_type: console
markdown:
wrap: 80
always_allow_html: true
---

# A simplified example of publication runs
For full code used in publication see `global.R`
This provides a simplified example of publication runs. For full code used in
publication see `global.R`

```{r setup, include = FALSE}
isInteractive <- interactive()
knitr::opts_chunk$set(echo = TRUE, eval = isInteractive, results = "hold",
cache = 2)
pkgPath <- file.path("packages", version$platform,
paste0(version$major, ".", strsplit(version$minor, "[.]")[[1]][1]))
dir.create(pkgPath, recursive = TRUE)
.libPaths(pkgPath, include.site = FALSE)
modulePath <- "R/SpaDES/m"
```

## Package installation
```{pkgInstall}
# Package installation and setup
```{r pkgInstall}
## set CRAN repo
options(repos = c(CRAN = "https://cloud.r-project.org"))
Expand All @@ -38,8 +48,8 @@ if (!"remotes" %in% installed.packages()) {
}
if (!"Require" %in% installed.packages(lib.loc = pkgPath) ||
packageVersion("Require", lib.loc = pkgPath) < "0.1.6.9005") {
remotes::install_github("PredictiveEcology/Require@8ac756d66bc145f226f39e703e3787d3aed159f2",
packageVersion("Require", lib.loc = pkgPath) < "0.1.6.9014") {
remotes::install_github("PredictiveEcology/Require@5c44205bf407f613f53546be652a438ef1248147",
upgrade = FALSE, force = TRUE)
}
Expand All @@ -62,14 +72,13 @@ pkgSnapshotFile <- file.path("packages",
paste0(version$major, "_", strsplit(version$minor, "[.]")[[1]][1]),
".txt"))
Require::Require(packageVersionFile = pkgSnapshotFile,
require = FALSE, standAlone = TRUE)
Require::Require(packageVersionFile = pkgSnapshotFile, require = FALSE, standAlone = TRUE)
```

## Package setup
```{r init, eval = TRUE, results = FALSE}
## Package loading and options, folder directories setup
```{r init, eval = TRUE, results = FALSE, messages = FALSE, warnings = FALSE}
Require::Require(c("raster", "terra", "dplyr", "data.table", "future",
"SpaDES.core", "LandR", "reproducible",
"SpaDES.core", "LandR", "reproducible", "ggplot2",
"ggspatial", "ggpubr", "cowplot"),
upgrade = FALSE, install = FALSE)
Expand All @@ -96,9 +105,9 @@ simPaths <- list(cachePath = Require::normPath(file.path("R/SpaDES/cache", simPa
, outputPath = Require::normPath(file.path("R/SpaDES/outputs", simPathName, runName)))
```

## Simulation setup - part 1
# Simulation setup

### Get study area and other necessary objects
## Part 1 - Get study area and other necessary objects

```{r simulationSetup}
## Get necessary objects -----------------------
Expand All @@ -107,7 +116,7 @@ devtools::source_url(paste0("https://raw.githubusercontent.com/CeresBarros/",
"1_simObjects.R?raw=TRUE"))
```

## Simulation setup - part 2 - species layers
## Part 2 - Species layers

```{r simulationSetup2}
## Run Biomass_speciesData to get species layers
Expand Down Expand Up @@ -143,16 +152,9 @@ rstLCC2005 <- Cache(prepInputs,

```{r loadSppLayers, include=FALSE, eval = !isInteractive}
simOutSpeciesLayers <- loadSimList(file.path("GitHubActionsSims", simPathName, runName, paste0("simList_speciesLayers", runName, ".qs")))
## subset sppEquivalencies
sppEquivalencies_CA <- sppEquivalencies_CA[Boreal %in% names(simOutSpeciesLayers$speciesLayers)]
sppColorVect <- sppColorVect[c(names(simOutSpeciesLayers$speciesLayers), "Mixed")]
## Get land-cover raster now that we have a rasterToMatchLarge
rstLCC2005 <- raster(file.path("GitHubActionsSims", simPathName, runName, "rstLCC_966512e152b53d9b.tif"))
```

## Simulation setup - part 3 - module parameters and outputs
## Part 3 - Module parameters and outputs

* `vegLeadingProportion` indicates what proportion the stand must be in one species group for it to be leading.
If 0, then there is never a notion of "mixed" vegetation types and a species is a leading species if it has the highest relative biomass in the pixel.
Expand Down Expand Up @@ -260,7 +262,7 @@ simOutputs <- data.frame(
saveTime = unique(seq(simTimes$start, simTimes$end, by = 1)),
eventPriority = 1,
stringsAsFactors = FALSE)
)
)
simOutputs <- rbind(simOutputs,
data.frame(objectName = "pixelGroupMap",
saveTime = unique(seq(simTimes$start, simTimes$end, by = 1)),
Expand Down Expand Up @@ -300,7 +302,7 @@ LandRBiomass_simInit <- loadSimList(file.path("GitHubActionsSims", simPathName,
objectDiagram(LandRBiomass_simInit, width = 1000, height = 2500)
```

## Run simulation
# Run simulation

Here we run just one repetition

Expand All @@ -310,11 +312,6 @@ LandRBiomass_sim <- spades(LandRBiomass_simInit, .plotInitialTime = simTimes$sta
saveSimList(LandRBiomass_sim, file.path(simPaths$outputPath, paste0("simList_LandRBiomass_sim_1rep_", runName, ".qs")))
```

```{r loadSimulation, include=FALSE, eval = !isInteractive}
## this has been commented because the simLists obj is not used at the moment.
# LandRBiomass_sim <- qs::qread(file.path("GitHubActionsSims", simPathName, runName, paste0("simList_LandRBiomass_sim_", runName, ".qs")))
```

If we were to run several repetitions, this would be how:

```{r runExperiment}
Expand Down Expand Up @@ -369,31 +366,31 @@ Plot(LandRBiomass_simInit$rawBiomassMap,
## species % cover
clearPlot(force = TRUE)
Plot(simOutSpeciesLayers$speciesLayers,
title = names(simOutSpeciesLayers$speciesLayers))
title = paste(names(simOutSpeciesLayers$speciesLayers), collapse = ", "))
```

```{r plots2, fig.show = 'asis', eval = !isInteractive, echo = FALSE}
plot(plotStudyAreas, main = " ")
## input stand biomass and age, ecological zonation (ecodistricts) and land-cover (LCC 2005)
plotEcodist <- ggplot() +
layer_spatial(LandRBiomass_simInit$ecoregionLayer, aes(fill = as.factor(ECODISTRIC))) +
layer_spatial(LandRBiomass_simInit$ecoregionLayer, aes(fill = as.factor(ECODISTRIC))) +
labs(fill = "") +
theme_void()
plot(stack(LandRBiomass_simInit$rawBiomassMap,
LandRBiomass_simInit$standAgeMap,
LandRBiomass_simInit$rstLCC),
main = c("kNN stand biomass - kNN stand age - land-cover"))
plot(stack(file.path("GitHubActionsSims", simPathName, runName, "rawBiomassMap_966512e152b53d9b.tif"),
LandRBiomass_simInit$standAgeMap,
file.path("GitHubActionsSims", simPathName, runName, "rstLCC_966512e152b53d9b.tif")),
main = "")
text(1.2, labels = "kNN stand biomass - kNN stand age - land-cover", xpd = NA, cex = 1, adj = 1.2)
plot(plotEcodist, main = "ecolocations")
## species % cover
plot(simOutSpeciesLayers$speciesLayers,
main = paste(names(simOutSpeciesLayers$speciesLayers), collapse = ", "))
plot(simOutSpeciesLayers$speciesLayers)
```



Similarly, we can have a look at the species traits values used in the simulation directly from the `simList` object (although we also chose to save them).

```{r speciesTraits, eval=FALSE}
Expand All @@ -413,6 +410,8 @@ knitr::kable(LandRBiomass_simInit$species, caption = "Invariant species traits")

Here we run the validation on the outputs of just one repetition.

## Validation setup

We begin by preparing all the inputs necessary for the `Biomass_validationKNN` module.

```{r validationPrep}
Expand Down Expand Up @@ -479,6 +478,8 @@ validationOutputs <- rbind(validationOutputs, data.frame(objectName = "speciesLa
eventPriority = 1))
```

## Validation runs

We can now run the validation:

```{r validationRun}
Expand Down
5,650 changes: 5,483 additions & 167 deletions R/SpaDES/global.html

Large diffs are not rendered by default.

Loading

0 comments on commit 7839175

Please sign in to comment.