Skip to content
Merged
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
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# sdmApp (development version)

## Example data in one click

* The Data Upload tab has a "Try with example data" button loading five
predictors and a sample of 1,500 Niakhar occurrences of *Faidherbia
albida*, so the whole workflow can be explored without one's own
files. The R-Code tab rebuilds them with `system.file()`, so the script
runs on any machine.

## Spatial sorting bias

* New `sdmApp_ssb()` measures the spatial sorting bias of each
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ separated and uses a comma as decimal mark, so read it with
## The graphical interface

The five tabs are `Help/About`, `Data Upload`, `Spatial Analysis`,
`Modeling` and `R-Code`, meant to be visited in order. The screenshots
`Modeling` and `R-Code`, meant to be visited in order. To explore the app
before using your own data, click "Try with example data" in the Data
Upload tab. The screenshots
below follow a session on *Faidherbia albida* in Senegal, with occurrences
from GBIF and climate layers from WorldClim.

Expand Down
12 changes: 10 additions & 2 deletions inst/shiny/sdmApp/controllers/ui_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,16 @@ steps_done <- reactive({
length(s$projections) > 0 && !is.null(s$future)
),
Details = c(
if (identical(s$env$type, "worldclim")) "WorldClim" else "files",
if (identical(s$occ$type, "gbif")) "GBIF" else "file",
switch(s$env$type %||% "files",
worldclim = "WorldClim",
example = "example data",
"files"
),
switch(s$occ$type %||% "file",
gbif = "GBIF",
example = "example data",
"file"
),
paste(s$thin, collapse = ", then "),
if (!is.null(s$future)) s$future$type else "",
if (!is.null(s$vif)) paste("VIF threshold", s$vif) else "",
Expand Down
53 changes: 51 additions & 2 deletions inst/shiny/sdmApp/controllers/ui_import_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,40 @@ aesthetics_panel <- tabsetPanel(
)
)

# Example data ---------------------------------------------------------------

observeEvent(input$load_example, {
example <- withProgress(
message = "Loading the example data",
example_data()
)
env_state$env <- example$env
env_state$paths <- NULL
env_state$source <- list(type = "example", layers = example_layers())
env_state$future <- NULL
env_state$future_source <- NULL
occ_state$table <- example$occurrences
occ_state$path <- NULL
occ_state$source <- list(type = "example", n = example_points())
occ_state$thin <- NULL
occ_state$lon <- "X_WGS84"
occ_state$lat <- "Y_WGS84"
occ_state$species <- "Faidherbia.albida"
spatial_state$autocor <- NULL
spatial_state$blocks <- NULL
updateSelectInput(session, "layer", choices = names(example$env))
showNotification(
sprintf(
paste(
"Loaded the example: %d layers and %d occurrences of Faidherbia",
"albida in Niakhar (Senegal). Go to Spatial Analysis or Modeling."
),
terra::nlyr(example$env), nrow(example$occurrences)
),
type = "message", duration = 10
)
})

output$ui_import_data <- renderUI({
tagList(
fluidRow(
Expand All @@ -254,10 +288,25 @@ output$ui_import_data <- renderUI({
),
column(12, p(
"First load the raster files (environmental variables), then the",
"species occurrence file. Example data are available under",
"'Examples' in the file choosers."
"species occurrence file, or download them below. To explore the",
"app first, try it with the example data."
), class = "wb-header-hint")
),
fluidRow(column(
12,
wellPanel(
actionButton("load_example", "Try with example data",
icon = icon("seedling"), class = "btn-success"
),
span(
style = "margin-left: 10px;",
"Faidherbia albida in Niakhar, Senegal: five predictors",
"(temperature, rainfall, elevation) and",
sprintf("%d presence/absence points,", example_points()),
"to explore the whole workflow at once."
)
)
)),
fluidRow(
sidebarPanel(
width = 3,
Expand Down
46 changes: 45 additions & 1 deletion inst/shiny/sdmApp/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@ app_cores <- function(method, n_tasks) {
as.integer(getOption("sdmApp.cores", default))
}

# Example data ---------------------------------------------------------------

example_layers <- function() {
c("bio1.tif", "bio4.tif", "bio12.tif", "bio15.tif", "Elev.tif")
}

# A sample keeps the example fast: all 9,258 points make some methods slow.
example_points <- function() 1500L

# The example data set: predictors and a reproducible sample of the
# Niakhar occurrences (the R-Code tab writes the same steps).
example_data <- function() {
env <- sdmApp::sdmApp_read_layers(
system.file("extdata", example_layers(), package = "sdmApp")
)
occ <- utils::read.csv2(
system.file("extdata", "Niakhar.csv", package = "sdmApp")
)
set.seed(1)
occ <- occ[sort(sample(nrow(occ), example_points())), , drop = FALSE]
list(env = env, occurrences = occ)
}

# Spatial thinning of the presences of an occurrence table (absences and
# rows without coordinates are kept), with sdmApp::sdmApp_thin(). Returns
# the kept rows; the attribute "thinned" gives the number removed.
Expand Down Expand Up @@ -592,7 +615,15 @@ script_env <- function(env) {
}
c(
r_section("Environmental variables"),
if (identical(env$type, "worldclim")) {
if (identical(env$type, "example")) {
c(
"# The example data shipped with sdmApp.",
r_call("sdmApp_read_layers", list(r_code(paste0(
"system.file(\"extdata\", ", r_value(env$layers),
", package = \"sdmApp\")"
))), assign = "env")
)
} else if (identical(env$type, "worldclim")) {
c(
"# WorldClim 2.1 bioclimatic variables (downloaded once, then cached).",
r_call("sdmApp_worldclim",
Expand Down Expand Up @@ -633,6 +664,19 @@ script_occ <- function(occ, thin) {
if (!is.null(occ$buffer)) list(buffer = occ$buffer)
), assign = "occ_table")
)
} else if (identical(occ$type, "example")) {
c(
"# The example occurrences shipped with sdmApp, a random sample.",
paste0(
"occ_table <- utils::read.csv2(system.file(\"extdata\", ",
"\"Niakhar.csv\", package = \"sdmApp\"))"
),
"set.seed(1)",
paste0(
"occ_table <- occ_table[sort(sample(nrow(occ_table), ",
r_value(occ$n), ")), ]"
)
)
} else if (is.null(occ$path)) {
"# occ_table: the occurrence table used in the interface."
} else {
Expand Down
41 changes: 41 additions & 0 deletions tests/testthat/test-app-script.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,44 @@ test_that("environmental folds are created and reproduced by the script", {
)
})
})

test_that("the example data load in one click and the script rebuilds them", {
skip_on_cran()
ok <- tryCatch(
{
sdmApp:::.sdmApp_check_deps()
TRUE
},
error = function(e) FALSE
)
skip_if_not(ok, "Packages needed by the interface are not installed.")
app <- sdmApp(shiny.server = TRUE)
shiny::testServer(app, {
expect_false(has_data())
session$setInputs(load_example = 1)
expect_true(has_data())
expect_equal(terra::nlyr(env()), 5)
expect_equal(nrow(occ_state$table), 1500)
expect_match(output$ui_import_data$html, "Try with example data")

session$setInputs(
`glm-predictors` = c("bio1", "bio12"), `glm-cv` = "random",
`glm-k` = 3
)
session$setInputs(`glm-run` = 1)
code <- current_code()
expect_true(any(grepl("system.file(\"extdata\"", code, fixed = TRUE)))
expect_false(any(grepl(.libPaths()[1], code, fixed = TRUE)))
steps <- steps_done()
expect_identical(steps$Details[1], "example data")

script <- new.env(parent = globalenv())
eval(parse(text = code[code != "library(sdmApp)"]), envir = script)
expect_equal(
sf::st_coordinates(script$occ), sf::st_coordinates(occ_sf())
)
expect_equal(
script$fit_glm$fold_metrics, model_fits$glm()$fold_metrics
)
})
})
Loading