diff --git a/DESCRIPTION b/DESCRIPTION index 96a69de..6e38233 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,14 +23,15 @@ Imports: sf (>= 1.0) RoxygenNote: 7.2.1 Suggests: - terra (>= 1.6-41), - ggplot2 (>= 3.3.6), - dismo, - cowplot, - automap (>= 1.0-16), - shiny (>= 1.7), - knitr, - rmarkdown, - testthat, - covr + terra (>= 1.6-41), + ggplot2 (>= 3.3.6), + dismo, + cowplot, + automap (>= 1.0-16), + shiny (>= 1.7), + knitr, + rmarkdown, + testthat (>= 3.0.0), + covr VignetteBuilder: knitr +Config/testthat/edition: 3 diff --git a/R/spatialAutoRange.R b/R/spatialAutoRange.R index 3d59c9f..d6daab7 100644 --- a/R/spatialAutoRange.R +++ b/R/spatialAutoRange.R @@ -33,6 +33,10 @@ #' } #' #' @export +#' +#' @examples +#' \donttest{ +#' } spatialAutoRange <- function(rasterLayer, sampleNumber = 5000L, border = NULL, diff --git a/blockCV.Rproj b/blockCV.Rproj index e5c3f7c..a2c9639 100644 --- a/blockCV.Rproj +++ b/blockCV.Rproj @@ -1,8 +1,8 @@ Version: 1.0 -RestoreWorkspace: Default -SaveWorkspace: Default -AlwaysSaveHistory: Default +RestoreWorkspace: No +SaveWorkspace: No +AlwaysSaveHistory: No EnableCodeIndexing: Yes UseSpacesForTab: Yes diff --git a/codecov.yml b/codecov.yml index 872906c..07634f5 100644 --- a/codecov.yml +++ b/codecov.yml @@ -1,3 +1,15 @@ comment: false -codecov: - token: 668a6472-722f-434d-9e77-0187bcb61a88 \ No newline at end of file + +coverage: + token: 668a6472-722f-434d-9e77-0187bcb61a88 + status: + project: + default: + target: auto + threshold: 1% + informational: true + patch: + default: + target: auto + threshold: 1% + informational: true diff --git a/man/spatialAutoRange.Rd b/man/spatialAutoRange.Rd index f17308f..af95c3e 100644 --- a/man/spatialAutoRange.Rd +++ b/man/spatialAutoRange.Rd @@ -60,6 +60,10 @@ An object of class S3. A list object including: \description{ This function is deprecated and will be removed in future updates! Please use \code{\link{cv_spatial_autocor}} instead! } +\examples{ +\donttest{ +} +} \seealso{ \code{\link{cv_spatial_autocor}} } diff --git a/tests/testthat.R b/tests/testthat.R index 609803e..39e0c75 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -1,3 +1,11 @@ +# This file is part of the standard setup for testthat. +# It is recommended that you do not modify it. +# +# Where should you do additional test configuration? +# Learn more about the roles of various files in: +# * https://r-pkgs.org/tests.html +# * https://testthat.r-lib.org/reference/test_package.html#special-files + library(testthat) library(blockCV) diff --git a/tests/testthat/Rplots.pdf b/tests/testthat/Rplots.pdf new file mode 100644 index 0000000..e5aeae2 Binary files /dev/null and b/tests/testthat/Rplots.pdf differ diff --git a/tests/testthat/testBuffering.R b/tests/testthat/oldtestBuffering.R similarity index 100% rename from tests/testthat/testBuffering.R rename to tests/testthat/oldtestBuffering.R diff --git a/tests/testthat/testEnvBlock.R b/tests/testthat/oldtestEnvBlock.R similarity index 100% rename from tests/testthat/testEnvBlock.R rename to tests/testthat/oldtestEnvBlock.R diff --git a/tests/testthat/testSpatialAutoRange.R b/tests/testthat/oldtestSpatialAutoRange.R similarity index 78% rename from tests/testthat/testSpatialAutoRange.R rename to tests/testthat/oldtestSpatialAutoRange.R index 33b5ce4..88ccd53 100644 --- a/tests/testthat/testSpatialAutoRange.R +++ b/tests/testthat/oldtestSpatialAutoRange.R @@ -1,76 +1,3 @@ -library(blockCV) -library(automap) - -context("Functions of v2") - -awt <- system.file("extdata/au/", package = "blockCV") |> - list.files(full.names = TRUE) |> - terra::rast() -nl <- terra::nlyr(awt) - -pa_data <- read.csv(system.file("extdata/", "species.csv", package = "blockCV")) |> - sf::st_as_sf(coords = c("x", "y"), crs = 7845) - -# spatialAutoRange -------------------------------------------------------- -expect_names <- c("range", - "rangeTable", - "plots", - "sampleNumber", - "variograms") - - -test_that("test spatialAutoRange function works", { - skip_on_cran() - - range1 <- spatialAutoRange(rasterLayer = awt, - sampleNumber = 1000, - showPlots = TRUE) - - expect_true(exists("range1")) - expect_is(range1, "SpatialAutoRange") - expect_equal(names(range1), expect_names) - expect_equal(nrow(range1$rangeTable), nl) - expect_equal(length(range1$variograms), nl) - expect_equal(dim(range1$rangeTable), c(nl, 3)) - expect_is(range1$plots[[1]], "ggplot") - expect_is(range1$variograms[[1]], "autofitVariogram") - expect_is(range1$variograms, "list") - expect_is(range1$rangeTable, "data.frame") - expect_is(range1$sampleNumber, "numeric") - expect_is(range1$range, "numeric") - expect_true(range1$range >= 0) - expect_true(!is.null(range1$variograms)) - expect_true( - all(names(awt) %in% range1$rangeTable$layers) - ) - - expect_equal(print.SpatialAutoRange(range1), "SpatialAutoRange") - expect_silent(plot.SpatialAutoRange(range1)) - expect_output(summary.SpatialAutoRange(range1)) - -}) - -test_that("test spatialAutoRange function with x", { - skip_on_cran() - - range3 <- spatialAutoRange(rasterLayer = awt, - speciesData = pa_data, - progress = FALSE, - showPlots = TRUE) - - expect_true(exists("range3")) - expect_is(range3, "SpatialAutoRange") - expect_is(range3$plots[[1]], "ggplot") - expect_is(range3$variograms[[1]], "autofitVariogram") - expect_is(range3$range, "numeric") - expect_true(range3$range >= 0) - expect_true(!is.null(range3$variograms)) - - expect_equal(print.SpatialAutoRange(range3), "SpatialAutoRange") - expect_silent(plot.SpatialAutoRange(range3)) - -}) - # library(blockCV) # # context("Spatial Autocorrelation Finder") diff --git a/tests/testthat/test-blocking.R b/tests/testthat/test-blocking.R new file mode 100644 index 0000000..8849056 --- /dev/null +++ b/tests/testthat/test-blocking.R @@ -0,0 +1,3 @@ +test_that("multiplication works", { + expect_equal(2 * 2, 4) +}) diff --git a/tests/testthat/test-buffering.R b/tests/testthat/test-buffering.R new file mode 100644 index 0000000..8849056 --- /dev/null +++ b/tests/testthat/test-buffering.R @@ -0,0 +1,3 @@ +test_that("multiplication works", { + expect_equal(2 * 2, 4) +}) diff --git a/tests/testthat/test-cv_spatial.R b/tests/testthat/test-cv_spatial.R new file mode 100644 index 0000000..cce9887 --- /dev/null +++ b/tests/testthat/test-cv_spatial.R @@ -0,0 +1,239 @@ +library(blockCV) + +expect_names <- c("folds_list", + "folds_ids", + "biomod_table", + "k", + "size", + "column", + "blocks", + "records") + +aus <- system.file("extdata/au/", package = "blockCV") |> + list.files(full.names = TRUE) |> + terra::rast() + +pa_data <- read.csv(system.file("extdata/", "species.csv", package = "blockCV")) |> + sf::st_as_sf(coords = c("x", "y"), crs = 7845) + + +test_that("test cv_spatial function with random assingment and raster file", { + + set.seed(1000) + scv <- cv_spatial(x = pa_data, + column = "occ", + r = aus, + size = 450000, + k = 5, + selection = "random", + iteration = 5, + biomod2 = TRUE, + offset = c(0.3, 0.2), + plot = FALSE, + progress = TRUE) + + expect_true(exists("scv")) + expect_s3_class(scv, "cv_spatial") + expect_equal(names(scv), expect_names) + expect_equal(length(scv$folds_list), 5) + expect_type(scv$folds_list, "list") + expect_type(scv$biomod_table, "logical") + expect_equal(dim(scv$biomod_table), c(nrow(pa_data), 5)) + expect_equal(scv$k, 5) + expect_s3_class(scv$blocks, "sf") + expect_type(scv$column, "character") + expect_type(scv$size, "double") + expect_equal(dim(scv$records), c(5, 4)) + expect_true( + !all(scv$records == 0) + ) + +}) + + +test_that("test cv_spatial function with systematic assingment and no raster file", { + + scv <- cv_spatial(x = sf::as_Spatial(pa_data), + rows_cols = c(10, 10), + k = 5, + selection = "systematic", + biomod2 = FALSE, + plot = TRUE) + + expect_true(exists("scv")) + expect_s3_class(scv, "cv_spatial") + expect_equal(names(scv), expect_names) + expect_equal(length(scv$folds_list), 5) + expect_type(scv$folds_list, "list") + expect_null(scv$biomod_table, "matrix") + expect_equal(scv$k, 5) + expect_s3_class(scv$blocks, "sf") + expect_null(scv$column) + expect_null(scv$size) + expect_equal(dim(scv$records), c(5, 2)) + expect_true( + !all(scv$records == 0) + ) + + expect_equal(print.cv_spatial(scv), "cv_spatial") + expect_message(plot.cv_spatial(scv)) + expect_output(summary.cv_spatial(scv)) + +}) + + +test_that("test cv_spatial function with non-numeric iteration", { + + scv <- cv_spatial(x = pa_data, + hexagon = FALSE, + k = 5, + selection = "random", + iteration = "2", # not numeric + biomod2 = FALSE, + plot = FALSE) + + expect_true(exists("scv")) + expect_s3_class(scv, "cv_spatial") + expect_equal(names(scv), expect_names) + expect_equal(length(scv$folds_list), 5) + expect_type(scv$folds_list, "list") + expect_null(scv$biomod_table, "matrix") + expect_equal(scv$k, 5) + expect_s3_class(scv$blocks, "sf") + expect_null(scv$column) + expect_null(scv$size) + expect_equal(dim(scv$records), c(5, 2)) + expect_true( + !all(scv$records == 0) + ) + + expect_equal(print.cv_spatial(scv), "cv_spatial") + expect_message(plot.cv_spatial(scv)) + expect_output(summary.cv_spatial(scv)) + +}) + +test_that("test cv_spatial with checkerboard assingment and only row blocks", { + + scv <- cv_spatial(x = sf::as_Spatial(pa_data), + hexagon = FALSE, + rows_cols = c(5, 1), + selection = "checkerboard", + biomod2 = FALSE, + plot = TRUE) + + expect_true(exists("scv")) + expect_s3_class(scv, "cv_spatial") + expect_equal(names(scv), expect_names) + expect_equal(length(scv$folds_list), 2) + expect_type(scv$folds_list, "list") + expect_null(scv$biomod_table) + expect_equal(scv$k, 2) + expect_s3_class(scv$blocks, "sf") + expect_null(scv$column) + expect_null(scv$size) + expect_equal(dim(scv$records), c(2, 2)) + expect_true( + !all(scv$records == 0) + ) + + expect_equal(print.cv_spatial(scv), "cv_spatial") + expect_message(plot.cv_spatial(scv)) + expect_output(summary.cv_spatial(scv)) + +}) + + +test_that("test cv_spatial with user-defined blocks", { + + sb <- cv_spatial(x = pa_data, + size = 450000, + selection = "random", + iteration = 1, + biomod2 = FALSE, + plot = FALSE) + + scv <- cv_spatial(x = pa_data, + blocks = sb$blocks, + selection = "random", + iteration = 5, + biomod2 = FALSE, + progress = FALSE, + plot = FALSE) + + expect_true(exists("scv")) + expect_s3_class(scv, "cv_spatial") + expect_equal(names(scv), expect_names) + expect_equal(length(scv$folds_list), 5) + expect_type(scv$folds_list, "list") + expect_null(scv$biomod_table) + expect_equal(scv$k, 5) + expect_s3_class(scv$blocks, "sf") + expect_null(scv$column) + expect_null(scv$size) + expect_equal(dim(scv$records), c(5, 2)) + expect_true( + !all(scv$records == 0) + ) + + expect_equal(print.cv_spatial(scv), "cv_spatial") + expect_message(plot.cv_spatial(scv)) + expect_output(summary.cv_spatial(scv)) + +}) + + +test_that("test cv_spatial failur: number of blocks, wrong selection", { + + expect_error(cv_spatial(x = pa_data, + rows_cols = c(2, 2), + hexagon = FALSE, + k = 15, # very high k + selection = "random")) + + expect_error(cv_spatial(x = pa_data, + k = 1, # very low k + selection = "random")) + + expect_error(cv_spatial(x = pa_data, + k = 5, + selection = "rand")) # wrong selection + +}) + + +test_that("test cv_spatial failur: wrong user-defined blocks", { + + expect_error( + cv_spatial(x = pa_data, # wrong user-defined blocks + column = "occ", + r = aus, + user_blocks = data.frame(a = 1), + size = 450000, + k = 5) + ) + +}) + + +test_that("test cv_spatial with no speceis column match", { + + expect_warning( + scv <- cv_spatial(x = pa_data, + column = "response", # wrong response + r = aus, + size = 450000, + k = 5) + ) + + expect_true(exists("scv")) + expect_s3_class(scv, "cv_spatial") + expect_equal(names(scv), expect_names) + expect_equal(length(scv$folds_list), 5) + expect_equal(scv$k, 5) + expect_equal(dim(scv$records), c(5, 2)) + expect_true( + !all(scv$records == 0) + ) + +}) diff --git a/tests/testthat/testData.R b/tests/testthat/test-data.R similarity index 80% rename from tests/testthat/testData.R rename to tests/testthat/test-data.R index 1346790..15a8d5b 100644 --- a/tests/testthat/testData.R +++ b/tests/testthat/test-data.R @@ -1,6 +1,5 @@ library(blockCV) -context("Test the availablbe data in the package") test_that("test that the data exist", { @@ -20,10 +19,9 @@ test_that("test that the data is corrrect", { expect_true(exists("pa")) expect_true(nrow(pa) > 100) - # expect_equal(dim(pa), c(254, 3)) - expect_is(pa, "data.frame") + expect_s3_class(pa, "data.frame") expect_true(exists("r")) expect_equal(terra::nlyr(r), 4) - expect_is(r, "SpatRaster") + expect_s4_class(r, "SpatRaster") }) diff --git a/tests/testthat/test-environBlock.R b/tests/testthat/test-environBlock.R new file mode 100644 index 0000000..8849056 --- /dev/null +++ b/tests/testthat/test-environBlock.R @@ -0,0 +1,3 @@ +test_that("multiplication works", { + expect_equal(2 * 2, 4) +}) diff --git a/tests/testthat/v2_functions.R b/tests/testthat/test-v2-functions.R similarity index 73% rename from tests/testthat/v2_functions.R rename to tests/testthat/test-v2-functions.R index a5de240..f474203 100644 --- a/tests/testthat/v2_functions.R +++ b/tests/testthat/test-v2-functions.R @@ -1,16 +1,16 @@ library(blockCV) library(automap) -context("Functions of v2") -awt <- system.file("extdata/au/", package = "blockCV") |> +aus <- system.file("extdata/au/", package = "blockCV") |> list.files(full.names = TRUE) |> terra::rast() -nl <- terra::nlyr(awt) +nl <- terra::nlyr(aus) pa_data <- read.csv(system.file("extdata/", "species.csv", package = "blockCV")) |> sf::st_as_sf(coords = c("x", "y"), crs = 7845) + # spatialAutoRange -------------------------------------------------------- expect_names <- c("range", "rangeTable", @@ -20,28 +20,28 @@ expect_names <- c("range", test_that("test spatialAutoRange function works", { - skip_on_cran() + # skip_on_cran() - range1 <- spatialAutoRange(rasterLayer = awt, + range1 <- spatialAutoRange(rasterLayer = aus, sampleNumber = 1000, showPlots = TRUE) expect_true(exists("range1")) - expect_is(range1, "SpatialAutoRange") + expect_s3_class(range1, "SpatialAutoRange") expect_equal(names(range1), expect_names) expect_equal(nrow(range1$rangeTable), nl) expect_equal(length(range1$variograms), nl) expect_equal(dim(range1$rangeTable), c(nl, 3)) - expect_is(range1$plots[[1]], "ggplot") - expect_is(range1$variograms[[1]], "autofitVariogram") - expect_is(range1$variograms, "list") - expect_is(range1$rangeTable, "data.frame") - expect_is(range1$sampleNumber, "numeric") - expect_is(range1$range, "numeric") + expect_s3_class(range1$plots[[1]], "ggplot") + expect_s3_class(range1$variograms[[1]], "autofitVariogram") + expect_s3_class(range1$rangeTable, "data.frame") + expect_type(range1$variograms, "list") + expect_type(range1$sampleNumber, "double") + expect_type(range1$range, "double") expect_true(range1$range >= 0) expect_true(!is.null(range1$variograms)) expect_true( - all(names(awt) %in% range1$rangeTable$layers) + all(names(aus) %in% range1$rangeTable$layers) ) expect_equal(print.SpatialAutoRange(range1), "SpatialAutoRange") @@ -51,18 +51,18 @@ test_that("test spatialAutoRange function works", { }) test_that("test spatialAutoRange function with x", { - skip_on_cran() + # skip_on_cran() - range3 <- spatialAutoRange(rasterLayer = awt, + range3 <- spatialAutoRange(rasterLayer = aus, speciesData = pa_data, progress = FALSE, showPlots = TRUE) expect_true(exists("range3")) - expect_is(range3, "SpatialAutoRange") - expect_is(range3$plots[[1]], "ggplot") - expect_is(range3$variograms[[1]], "autofitVariogram") - expect_is(range3$range, "numeric") + expect_s3_class(range3, "SpatialAutoRange") + expect_s3_class(range3$plots[[1]], "ggplot") + expect_s3_class(range3$variograms[[1]], "autofitVariogram") + expect_type(range3$range, "double") expect_true(range3$range >= 0) expect_true(!is.null(range3$variograms)) @@ -85,12 +85,12 @@ expect_names <- c("folds", test_that("test spatiaBlock function works", { - skip_on_cran() + # skip_on_cran() set.seed(1000) sb1 <- spatialBlock(speciesData = pa_data, species = "occ", - rasterLayer = awt, + rasterLayer = aus, theRange = 450000, k = 5, selection = "random", @@ -100,16 +100,16 @@ test_that("test spatiaBlock function works", { progress = TRUE) expect_true(exists("sb1")) - expect_is(sb1, "SpatialBlock") + expect_s3_class(sb1, "SpatialBlock") expect_equal(names(sb1), expect_names) expect_equal(length(sb1$folds), 5) - expect_is(sb1$folds, "list") - expect_is(sb1$biomodTable, "matrix") + expect_type(sb1$folds, "list") + expect_type(sb1$biomodTable, "logical") expect_equal(dim(sb1$biomodTable), c(nrow(pa_data), 5)) expect_equal(sb1$k, 5) - expect_is(sb1$blocks, "SpatialPolygonsDataFrame") - expect_is(sb1$species, "character") - expect_is(sb1$range, "numeric") + expect_s4_class(sb1$blocks, "SpatialPolygonsDataFrame") + expect_type(sb1$species, "character") + expect_type(sb1$range, "double") expect_equal(dim(sb1$records), c(5, 4)) expect_true( !all(sb1$records == 0) @@ -127,7 +127,7 @@ expect_names <- c("folds", "records") test_that("test buffering function works", { - skip_on_cran() + # skip_on_cran() # buffering with presence-absence data bf1 <- buffering(speciesData= pa_data, @@ -137,13 +137,13 @@ test_that("test buffering function works", { progress = TRUE) expect_true(exists("bf1")) - expect_is(bf1, "BufferedBlock") + expect_s3_class(bf1, "BufferedBlock") expect_equal(names(bf1), expect_names) expect_equal(length(bf1$folds), nrow(pa_data)) - expect_is(bf1$folds, "list") - expect_is(bf1$k, "integer") - expect_is(bf1$species, "character") - expect_is(bf1$range, "numeric") + expect_type(bf1$folds, "list") + expect_type(bf1$k, "integer") + expect_type(bf1$species, "character") + expect_type(bf1$range, "double") expect_equal(bf1$dataType, "PA") expect_equal(dim(bf1$records), c(nrow(pa_data), 4)) expect_true( @@ -162,10 +162,10 @@ expect_names <- c("folds", "records") test_that("test that environmental blocking function works", { - skip_on_cran() + # skip_on_cran() # environmental clustering - eb <- envBlock(rasterLayer = awt, + eb <- envBlock(rasterLayer = aus, speciesData = pa_data, species = "occ", # name of the column with species data k = 5, @@ -173,12 +173,12 @@ test_that("test that environmental blocking function works", { rasterBlock = FALSE) expect_true(exists("eb")) - expect_is(eb, "EnvironmentalBlock") + expect_s3_class(eb, "EnvironmentalBlock") expect_equal(names(eb), expect_names) expect_equal(length(eb$folds), 5) - expect_is(eb$folds, "list") + expect_type(eb$folds, "list") expect_equal(eb$k, 5) - expect_is(eb$species, "character") + expect_type(eb$species, "character") expect_equal(dim(eb$records), c(5, 4)) expect_true( !all(eb$records == 0) diff --git a/tests/testthat/testHelper_function.R b/tests/testthat/testHelper_function.R deleted file mode 100644 index b007b16..0000000 --- a/tests/testthat/testHelper_function.R +++ /dev/null @@ -1,51 +0,0 @@ -# library(blockCV) -# -# context("external helper functions fully") -# -# awt <- raster::brick(system.file("extdata", "awt.grd", package = "blockCV")) -# PA <- read.csv(system.file("extdata", "PA.csv", package = "blockCV")) -# pa_data <- sf::st_as_sf(PA, coords = c("x", "y"), crs = raster::projection(awt)) -# -# # test_that("helper function with no CRS", { -# # skip_on_cran() -# # -# # suppressWarnings(awt_wgs <- raster::projectRaster(from = awt, crs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")) -# # raster::projection(awt_wgs) <- NA -# # -# # expect_warning( -# # net <- blockCV:::rasterNet(x = awt_wgs, resolution = 70000, mask = TRUE) -# # ) -# # -# # expect_true(exists("net")) -# # expect_is(net, "sf") -# # -# # }) -# -# test_that("helper function with no species data", { -# -# net <- blockCV:::rasterNet(x = pa_data, resolution = 70000, mask = TRUE) -# -# expect_true(exists("net")) -# expect_is(net, "sf") -# -# }) -# -# test_that("helper function error with no block size", { -# -# expect_error( -# blockCV:::rasterNet(x = awt, mask = TRUE) -# ) -# -# }) -# -# test_that("helper function error with wrong ofset", { -# -# expect_error( -# blockCV:::rasterNet(x = pa_data, xOffset = 3) -# ) -# -# expect_error( -# blockCV:::rasterNet(x = pa_data, yOffset = 3) -# ) -# -# }) diff --git a/tests/testthat/testSpatialBlock.R b/tests/testthat/testSpatialBlock.R deleted file mode 100644 index e085e7c..0000000 --- a/tests/testthat/testSpatialBlock.R +++ /dev/null @@ -1,323 +0,0 @@ -# library(blockCV) -# -# context("spatialBlock function") -# -# expect_names <- c("folds", -# "foldID", -# "biomodTable", -# "k", -# "blocks", -# "species", -# "range", -# "plots", -# "records") -# -# awt <- raster::brick(system.file("extdata", "awt.grd", package = "blockCV")) -# PA <- read.csv(system.file("extdata", "PA.csv", package = "blockCV")) -# pa_data <- sf::st_as_sf(PA, coords = c("x", "y"), crs = raster::projection(awt)) -# -# # r <- awt[[1]] -# # r[r > 0] <- 1 -# # bound <- raster::rasterToPolygons(r, dissolve = TRUE) -# -# test_that("test spatiaBlock function with random assingment and raster file", { -# -# set.seed(1000) -# # expect_warning( -# sb1 <- spatialBlock(speciesData = pa_data, -# species = "Species", -# rasterLayer = awt, -# theRange = 70000, -# k = 5, -# selection = "random", -# # border = bound, -# iteration = 5, -# numLimit = 0, -# biomod2Format = TRUE, -# xOffset = 0.3, -# yOffset = 0.2, -# showBlocks = FALSE, -# progress = TRUE) -# # ) -# -# expect_true(exists("sb1")) -# expect_is(sb1, "SpatialBlock") -# expect_equal(names(sb1), expect_names) -# expect_equal(length(sb1$folds), 5) -# expect_is(sb1$folds, "list") -# expect_is(sb1$biomodTable, "matrix") -# expect_equal(dim(sb1$biomodTable), c(nrow(pa_data), 5)) -# expect_equal(sb1$k, 5) -# expect_is(sb1$blocks, "SpatialPolygonsDataFrame") -# expect_is(sb1$species, "character") -# expect_is(sb1$range, "numeric") -# expect_null(sb1$plots) -# expect_equal(dim(sb1$records), c(5, 4)) -# expect_true( -# !all(sb1$records == 0) -# ) -# -# }) -# -# -# test_that("test spatiaBlock function with systematic assingment and no raster file", { -# -# sb2 <- spatialBlock(speciesData = sf::as_Spatial(pa_data), -# rows = 5, -# cols = 8, -# k = 5, -# selection = "systematic", -# biomod2Format = FALSE, -# showBlocks = TRUE) -# -# expect_true(exists("sb2")) -# expect_is(sb2, "SpatialBlock") -# expect_equal(names(sb2), expect_names) -# expect_equal(length(sb2$folds), 5) -# expect_is(sb2$folds, "list") -# expect_null(sb2$biomodTable, "matrix") -# expect_equal(sb2$k, 5) -# expect_is(sb2$blocks, "SpatialPolygonsDataFrame") -# expect_null(sb2$species) -# expect_null(sb2$range) -# expect_is(sb2$plots, "ggplot") -# expect_equal(dim(sb2$records), c(5, 2)) -# expect_true( -# !all(sb2$records == 0) -# ) -# -# expect_equal(print.SpatialBlock(sb2), "SpatialBlock") -# expect_message(plot.SpatialBlock(sb2)) -# expect_output(summary.SpatialBlock(sb2)) -# -# }) -# -# -# test_that("test spatiaBlock function with non-numeric iteration", { -# -# sb2 <- spatialBlock(speciesData = pa_data, -# rows = 5, -# cols = 8, -# k = 5, -# selection = "random", -# iteration = "12", # not numeric -# # numLimit = 2, -# biomod2Format = FALSE, -# showBlocks = TRUE) -# -# expect_true(exists("sb2")) -# expect_is(sb2, "SpatialBlock") -# expect_equal(names(sb2), expect_names) -# expect_equal(length(sb2$folds), 5) -# expect_is(sb2$folds, "list") -# expect_null(sb2$biomodTable, "matrix") -# expect_equal(sb2$k, 5) -# expect_is(sb2$blocks, "SpatialPolygonsDataFrame") -# expect_null(sb2$species) -# expect_null(sb2$range) -# expect_is(sb2$plots, "ggplot") -# expect_equal(dim(sb2$records), c(5, 2)) -# expect_true( -# !all(sb2$records == 0) -# ) -# -# expect_equal(print.SpatialBlock(sb2), "SpatialBlock") -# expect_message(plot.SpatialBlock(sb2)) -# expect_output(summary.SpatialBlock(sb2)) -# -# }) -# -# test_that("test spatiaBlock with checkerboard assingment and only row blocks", { -# -# sb3 <- spatialBlock(speciesData = sf::as_Spatial(pa_data), -# rows = 5, -# selection = "checkerboard", -# biomod2Format = FALSE, -# showBlocks = TRUE) -# -# expect_true(exists("sb3")) -# expect_is(sb3, "SpatialBlock") -# expect_equal(names(sb3), expect_names) -# expect_equal(length(sb3$folds), 2) -# expect_is(sb3$folds, "list") -# expect_null(sb3$biomodTable) -# expect_equal(sb3$k, 2) -# expect_is(sb3$blocks, "SpatialPolygonsDataFrame") -# expect_null(sb3$species) -# expect_null(sb3$range) -# expect_is(sb3$plots, "ggplot") -# expect_equal(dim(sb3$records), c(2, 2)) -# expect_true( -# !all(sb3$records == 0) -# ) -# -# expect_equal(print.SpatialBlock(sb3), "SpatialBlock") -# expect_message(plot.SpatialBlock(sb3)) -# expect_output(summary.SpatialBlock(sb3)) -# -# }) -# -# -# test_that("test spatiaBlock with user-defined blocks", { -# -# sb <- spatialBlock(speciesData = pa_data, -# theRange = 70000, -# selection = "random", -# iteration = 1, -# biomod2Format = FALSE, -# showBlocks = FALSE) -# -# sb4 <- spatialBlock(speciesData = pa_data, -# blocks = sb$blocks[,-2], -# selection = "checkerboard", -# biomod2Format = FALSE, -# showBlocks = FALSE) -# -# expect_true(exists("sb4")) -# expect_is(sb4, "SpatialBlock") -# expect_equal(names(sb4), expect_names) -# expect_equal(length(sb4$folds), 2) -# expect_is(sb4$folds, "list") -# expect_null(sb4$biomodTable) -# expect_equal(sb4$k, 2) -# expect_is(sb4$blocks, "SpatialPolygonsDataFrame") -# expect_null(sb4$species) -# expect_null(sb4$range) -# expect_null(sb4$plots) -# expect_equal(dim(sb4$records), c(2, 2)) -# expect_true( -# !all(sb4$records == 0) -# ) -# -# expect_equal(print.SpatialBlock(sb4), "SpatialBlock") -# expect_message(plot.SpatialBlock(sb4)) -# expect_output(summary.SpatialBlock(sb4)) -# -# }) -# -# test_that("test spatiaBlock function with NULL numLimit", { -# -# sb2 <- spatialBlock(speciesData = pa_data, -# species = "Species", -# rows = 5, -# cols = 8, -# k = 5, -# selection = "random", -# iteration = 5, -# numLimit = NULL, -# biomod2Format = FALSE, -# showBlocks = FALSE) -# -# expect_true(exists("sb2")) -# expect_is(sb2, "SpatialBlock") -# expect_equal(names(sb2), expect_names) -# expect_equal(dim(sb2$records), c(5, 4)) -# expect_true( -# !all(sb2$records == 0) -# ) -# -# }) -# -# test_that("test spatialBlock failur: number of blocks, wrong selection", { -# -# expect_error(spatialBlock(speciesData = pa_data, -# cols = 5, -# k = 15, # very high k -# selection = "random")) -# -# expect_error(spatialBlock(speciesData = pa_data, -# cols = 5, -# rows = 8, -# k = 1, # very low k -# selection = "random")) -# -# expect_error(spatialBlock(speciesData = pa_data, -# cols = 5, -# k = 5, -# selection = "rand")) # wrong selection -# -# }) -# -# test_that("test spatialBlock failur: wrong species data", { -# -# expect_error( -# spatialBlock(speciesData = awt, # wrong speceis data -# species = "Species", -# rasterLayer = awt, -# theRange = 70000, -# k = 5) -# ) -# -# }) -# -# test_that("test spatialBlock failur: wrong user-defined blocks", { -# -# expect_error( -# spatialBlock(speciesData = pa_data, # wrong user-defined blocks -# species = "Species", -# rasterLayer = awt, -# blocks = r, -# theRange = 70000, -# k = 5) -# ) -# -# }) -# -# -# test_that("test spatialBlock with no speceis column match", { -# -# expect_warning( -# sb4 <- spatialBlock(speciesData = pa_data, -# species = "response", # wrong response -# rasterLayer = awt, -# maskBySpecies = FALSE, -# theRange = 70000, -# k = 5) -# ) -# -# expect_true(exists("sb4")) -# expect_is(sb4, "SpatialBlock") -# expect_equal(names(sb4), expect_names) -# expect_equal(length(sb4$folds), 5) -# expect_equal(sb4$k, 5) -# expect_equal(dim(sb4$records), c(5, 2)) -# expect_true( -# !all(sb4$records == 0) -# ) -# -# }) -# -# # skip these on CRAN ------------------------------------------------------ -# -# test_that("test spatialBlock failur: wrong user-defined border", { -# skip_on_cran() -# -# expect_error( -# spatialBlock(speciesData = pa_data, # wrong speceis data -# species = "Species", -# rasterLayer = awt, -# border = r, -# theRange = 70000, -# k = 5) -# ) -# -# }) -# -# test_that("test spatialBlock with no smaller mask raster", { -# skip_on_cran() -# -# ext <- c(xmin = 206961.5, -# xmax = 481078.8, -# ymin = 8038112, -# ymax = 8308669 ) -# awt2 <- raster::crop(awt, ext) -# expect_warning( -# sb4 <- spatialBlock(speciesData = pa_data, -# species = "response", # wrong response -# rasterLayer = awt2, -# theRange = 70000, -# k = 5) -# ) -# expect_true(exists("sb4")) -# -# })