From 3cecf17683816cec7e71cc52cd5c66ac7a343c7d Mon Sep 17 00:00:00 2001 From: Aboubacar HEMA Date: Tue, 1 Dec 2026 09:36:28 +0000 Subject: [PATCH] Load example data in one click The Data Upload tab has a "Try with example data" button loading five predictors and a reproducible sample of 1,500 Niakhar occurrences of Faidherbia albida, so the 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. Co-Authored-By: Claude Opus 5.5 --- NEWS.md | 8 +++ README.md | 4 +- inst/shiny/sdmApp/controllers/ui_code.R | 12 ++++- .../shiny/sdmApp/controllers/ui_import_data.R | 53 ++++++++++++++++++- inst/shiny/sdmApp/helpers.R | 46 +++++++++++++++- tests/testthat/test-app-script.R | 41 ++++++++++++++ 6 files changed, 158 insertions(+), 6 deletions(-) diff --git a/NEWS.md b/NEWS.md index 4e76de6..a6bcdc6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. + ## Comparing models * New `sdmApp_compare()` tabulates fitted models: the mean and standard diff --git a/README.md b/README.md index 4a558c2..b0cd4db 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/inst/shiny/sdmApp/controllers/ui_code.R b/inst/shiny/sdmApp/controllers/ui_code.R index 985754e..55a8eeb 100644 --- a/inst/shiny/sdmApp/controllers/ui_code.R +++ b/inst/shiny/sdmApp/controllers/ui_code.R @@ -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 "", diff --git a/inst/shiny/sdmApp/controllers/ui_import_data.R b/inst/shiny/sdmApp/controllers/ui_import_data.R index 6b7b7b1..064bda0 100644 --- a/inst/shiny/sdmApp/controllers/ui_import_data.R +++ b/inst/shiny/sdmApp/controllers/ui_import_data.R @@ -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( @@ -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, diff --git a/inst/shiny/sdmApp/helpers.R b/inst/shiny/sdmApp/helpers.R index 238aa44..43546fd 100644 --- a/inst/shiny/sdmApp/helpers.R +++ b/inst/shiny/sdmApp/helpers.R @@ -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. @@ -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", @@ -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 { diff --git a/tests/testthat/test-app-script.R b/tests/testthat/test-app-script.R index 72088b0..a30b736 100644 --- a/tests/testthat/test-app-script.R +++ b/tests/testthat/test-app-script.R @@ -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 + ) + }) +})