diff --git a/.github/workflows/Deploy-pkgdown.yaml b/.github/workflows/Deploy-pkgdown.yaml index d4a6fdf3..2144110a 100644 --- a/.github/workflows/Deploy-pkgdown.yaml +++ b/.github/workflows/Deploy-pkgdown.yaml @@ -32,7 +32,7 @@ jobs: - uses: r-lib/actions/setup-r-dependencies@v2 with: extra-packages: any::pkgdown, any::ggplot2, any::future, any::future.apply, - any::patchwork, any::rgbif, any::rgdal, any::rnaturalearth, + any::patchwork, any::rgbif, any::rnaturalearth, any::rnaturalearthdata, any::sf, local::. needs: website diff --git a/DESCRIPTION b/DESCRIPTION index 08f0aa92..f3aed639 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: shar Title: Species-Habitat Associations -Version: 2.1 +Version: 2.1.1 Authors@R: c(person("Maximilian H.K.", "Hesselbarth", email = "mhk.hesselbarth@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-1125-9918")), person("Marco", "Sciaini", email = "marco.sciaini@posteo.net", @@ -47,3 +47,4 @@ RoxygenNote: 7.2.3 VignetteBuilder: knitr Encoding: UTF-8 LazyData: true +Config/testthat/edition: 3 diff --git a/NEWS.md b/NEWS.md index 8e35a6b4..aa20ed0f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,6 @@ +# shar 2.1.1 +* Bugfixes (thanks to @baddstats) + # shar 2.1 * Improvements * Remove `comp_fast` argument diff --git a/R/fit_point_process.R b/R/fit_point_process.R index 176206e9..512b06d5 100644 --- a/R/fit_point_process.R +++ b/R/fit_point_process.R @@ -55,12 +55,8 @@ fit_point_process <- function(pattern, pattern <- spatstat.geom::unmark(pattern) - if (verbose) { + if (verbose) message("Unmarking provided input pattern.") - warning("Unmarked provided input pattern.", - call. = FALSE) - - } } if (process == "poisson") { @@ -158,7 +154,7 @@ fit_point_process <- function(pattern, if (simplify) { # not possible if more than one pattern is present - if (n_random > 1 && verbose) { + if (n_random > 1) { warning("'simplify = TRUE' not possible for 'n_random > 1'.", call. = FALSE) @@ -175,7 +171,7 @@ fit_point_process <- function(pattern, } else { # return warning if simply = TRUE because not possible if return_input = TRUE (only verbose = TRUE) - if (simplify && verbose) { + if (simplify) { warning("'simplify = TRUE' not possible for 'return_input = TRUE'.", call. = FALSE) diff --git a/R/plot.rd_ras.R b/R/plot.rd_ras.R index 63a0f7d7..7c4e96e5 100644 --- a/R/plot.rd_ras.R +++ b/R/plot.rd_ras.R @@ -51,14 +51,11 @@ plot.rd_ras <- function(x, n = NULL, col, verbose = TRUE, nrow, ncol, ...) { habitats <- sort(table(terra::values(x$observed), useNA = "no")) # get table of habitats # print warning if more than 10 classes are present - if (verbose) { + if (length(habitats) > 10) { - if (length(habitats) > 10) { + warning("The raster has more than 10 classes. Please make sure discrete classes are provided.", + call. = FALSE) - warning("The raster has more than 10 classes. Please make sure discrete classes are provided.", - call. = FALSE) - - } } # get randomized pattern diff --git a/R/print.rd_mar.R b/R/print.rd_mar.R index 2b169c81..7b975147 100644 --- a/R/print.rd_mar.R +++ b/R/print.rd_mar.R @@ -28,9 +28,7 @@ #' @rdname print.rd_mar #' #' @export -print.rd_mar <- function(x, - digits = 4, - ...) { +print.rd_mar <- function(x, digits = 4, ...) { # set length observed pattern to 0 and # return warning that energy can't be calculated diff --git a/R/randomize_raster.R b/R/randomize_raster.R index 62cbf3bc..715e8406 100644 --- a/R/randomize_raster.R +++ b/R/randomize_raster.R @@ -68,14 +68,10 @@ randomize_raster <- function(raster, habitats <- sort(table(terra::values(raster, mat = FALSE))) # get table of habitats # print warning if more than 10 classes are present - if (verbose) { - - if (length(habitats) > 10) { + if (length(habitats) > 10) { warning("The raster has more than 10 classes. Please make sure discrete classes are provided.", call. = FALSE) - - } } n_cells <- sum(habitats) # number of cells @@ -206,7 +202,7 @@ randomize_raster <- function(raster, if (simplify) { # not possible if more than one raster is present - if (n_random > 1 && verbose) { + if (n_random > 1) { warning("'simplify = TRUE' not possible for 'n_random > 1'.", call. = FALSE) @@ -223,7 +219,7 @@ randomize_raster <- function(raster, } else { # return warning if simply = TRUE because not possible if return_input = TRUE (only verbose = TRUE) - if (simplify && verbose) { + if (simplify) { warning("'simplify = TRUE' not possible for 'return_input = TRUE'.", call. = FALSE) diff --git a/R/reconstruct_algorithm.R b/R/reconstruct_algorithm.R index d17a37c1..20727bf6 100644 --- a/R/reconstruct_algorithm.R +++ b/R/reconstruct_algorithm.R @@ -43,9 +43,7 @@ reconstruct_algorithm <- function(pattern, # check if n_random is >= 1 if (n_random < 1) { - stop("n_random must be >= 1.", call. = FALSE) - } # unmark pattern @@ -53,17 +51,19 @@ reconstruct_algorithm <- function(pattern, pattern <- spatstat.geom::unmark(pattern) - if (verbose) { - warning("Unmarked provided input pattern. For marked pattern, see reconstruct_pattern_marks().", - call. = FALSE) + if (verbose) message("Unmarking provided input pattern. For marked pattern, see reconstruct_pattern_marks().") - } } # grab window and number of points n_points <- pattern$n window <- pattern$window + # check if pattern is emtpy + if (n_points == 0){ + stop("The observed pattern contains no points.", call. = FALSE) + } + # calculate intensity lambda <- n_points / spatstat.geom::area(window) lambda2area <- (n_points * (n_points - 1)) / spatstat.geom::area(window) @@ -169,6 +169,9 @@ reconstruct_algorithm <- function(pattern, } } + # check if simulated is empty + if (simulated$n == 0) stop("The simulated pattern contains no points.", call. = FALSE) + # energy before reconstruction energy <- Inf diff --git a/R/reconstruct_pattern.R b/R/reconstruct_pattern.R index f475e2bc..546dc7e2 100644 --- a/R/reconstruct_pattern.R +++ b/R/reconstruct_pattern.R @@ -122,7 +122,7 @@ reconstruct_pattern <- function(pattern, method = "homo", if (simplify) { # not possible if more than one pattern is present - if (n_random > 1 && verbose) { + if (n_random > 1) { warning("'simplify = TRUE' not possible for 'n_random > 1'.", call. = FALSE) @@ -139,7 +139,7 @@ reconstruct_pattern <- function(pattern, method = "homo", } else { # return warning if simply = TRUE because not possible if return_input = TRUE (only verbose = TRUE) - if (simplify && verbose) { + if (simplify) { warning("'simplify = TRUE' not possible for 'return_input = TRUE'.", call. = FALSE) diff --git a/R/reconstruct_pattern_marks.R b/R/reconstruct_pattern_marks.R index 2bc55026..45af473b 100644 --- a/R/reconstruct_pattern_marks.R +++ b/R/reconstruct_pattern_marks.R @@ -80,23 +80,21 @@ reconstruct_pattern_marks <- function(pattern, # check if n_random is >= 1 if (!n_random >= 1) { - stop("n_random must be >= 1.", call. = FALSE) - } # check if pattern is marked if (spatstat.geom::is.marked(pattern) || !spatstat.geom::is.marked(marked_pattern)) { - stop("'pattern' must be unmarked and 'marked_pattern' marked", call. = FALSE) - } # check if marks are numeric if (!inherits(x = marked_pattern$marks, what = "numeric")) { - stop("marks must be 'numeric'", call. = FALSE) + } + if (pattern$n == 0 || marked_pattern$n == 0){ + stop("At least one of the observed patterns contain no points.", call. = FALSE) } # calculate r from data @@ -297,7 +295,7 @@ reconstruct_pattern_marks <- function(pattern, if (simplify) { # not possible if more than one pattern is present - if (n_random > 1 && verbose) { + if (n_random > 1) { warning("'simplify = TRUE' not possible for 'n_random > 1'.", call. = FALSE) @@ -316,7 +314,7 @@ reconstruct_pattern_marks <- function(pattern, } else { # return warning if simply = TRUE because not possible if return_input = TRUE (only verbose = TRUE) - if (simplify && verbose) { + if (simplify) { warning("'simplify = TRUE' not possible for 'return_input = TRUE'.", call. = FALSE) diff --git a/R/results_habitat_association.R b/R/results_habitat_association.R index bd9b155b..56e658b2 100644 --- a/R/results_habitat_association.R +++ b/R/results_habitat_association.R @@ -67,7 +67,7 @@ results_habitat_association <- function(pattern, raster, significance_level = 0. } - if (significance_level < 0.01 || significance_level > 0.1 && verbose) { + if (significance_level < 0.01 || significance_level > 0.1) { warning("Make sure 'signifcance_level' is meaningful (e.g. 'significance_level = 0.05').", call. = FALSE) @@ -100,24 +100,14 @@ results_habitat_association <- function(pattern, raster, significance_level = 0. same_extent <- terra::ext(raster$observed) == terra::ext(pattern$window$xrange, pattern$window$yrange) - # error if extent is not identical - if (!same_extent) { - - warning("Extent of 'pattern' and 'raster' are not identical.", call. = FALSE) - - } - habitats <- table(terra::values(raster$observed, mat = FALSE), useNA = "no") # get table of habitats # print warning if more than 25 classes are present - if (verbose) { + if (length(habitats) > 25) { - if (length(habitats) > 25) { + warning("The raster has more than 25 classes. You can ignore this warning if your raster data is discrete.", + call. = FALSE) - warning("The raster has more than 25 classes. You can ignore this warning if your raster data is discrete.", - call. = FALSE) - - } } # print quantiles @@ -166,13 +156,6 @@ results_habitat_association <- function(pattern, raster, significance_level = 0. same_extent <- terra::ext(raster) == terra::ext(pattern$observed$window$xrange, pattern$observed$window$yrange) - # error if extent is not identical - if (!same_extent) { - - warning("Extent of 'pattern' and 'raster' are not identical.", call. = FALSE) - - } - # warning if NA are present if (anyNA(terra::values(raster, mat = FALSE))) { @@ -183,14 +166,10 @@ results_habitat_association <- function(pattern, raster, significance_level = 0. habitats <- table(terra::values(raster, mat = FALSE), useNA = "no") # get table of habitats # print warning if more than 25 classes are present - if (verbose) { - - if (length(habitats) > 25) { - - warning("The raster has more than 25 classes. You can ignore this warning if your raster data is discrete.", - call. = FALSE) + if (length(habitats) > 25) { - } + warning("The raster has more than 25 classes. You can ignore this warning if your raster data is discrete.", + call. = FALSE) } # print quantiles @@ -216,6 +195,13 @@ results_habitat_association <- function(pattern, raster, significance_level = 0. }) } + # error if extent is not identical + if (!same_extent) { + + warning("Extent of 'pattern' and 'raster' are not identical.", call. = FALSE) + + } + # repeat each name as often as number of habitats names_obj <- rep(x = names(habitats_count), each = length(habitats)) diff --git a/R/sample_randomized.R b/R/sample_randomized.R index b3d79312..e3580115 100644 --- a/R/sample_randomized.R +++ b/R/sample_randomized.R @@ -52,14 +52,9 @@ sample_randomized <- function(randomized, n = NULL, verbose = TRUE) { # check if less than 3 randomized elements are present n <- ifelse(test = n_randomized < 4, yes = n_randomized, no = 3) - # print message - if (verbose) { - - # return warning - warning("n larger than number of randomize eleements. Setting n = ", n, ".", - call. = FALSE) - - } + # return warning + warning("n larger than number of randomize eleements. Setting n = ", n, ".", + call. = FALSE) } # sample elements @@ -82,11 +77,8 @@ sample_randomized <- function(randomized, n = NULL, verbose = TRUE) { } # return warning that some ids were removed - if (verbose) { + warning("Using only IDs that are present in randomized data.", call. = FALSE) - warning("Using only IDs that are present in randomized data.", call. = FALSE) - - } } # sample elements diff --git a/R/translate_raster.R b/R/translate_raster.R index ddd4d1be..8759ef5c 100644 --- a/R/translate_raster.R +++ b/R/translate_raster.R @@ -55,14 +55,11 @@ translate_raster <- function(raster, steps_x = NULL, steps_y = NULL, habitats <- sort(table(terra::values(raster, mat = FALSE))) # get table of habitats # print warning if more than 10 classes are present - if (verbose) { - - if (length(habitats) > 10) { + if (length(habitats) > 10) { - warning("The raster has more than 10 classes. Please make sure discrete classes are provided.", - call. = FALSE) + warning("The raster has more than 10 classes. Please make sure discrete classes are provided.", + call. = FALSE) - } } # use all possible combinations @@ -171,7 +168,7 @@ translate_raster <- function(raster, steps_x = NULL, steps_y = NULL, if (simplify) { # not possible if more than one raster is present - if (n_random > 1 && verbose) { + if (n_random > 1) { warning("'simplify = TRUE' not possible for 'n_random > 1'.", call. = FALSE) @@ -189,7 +186,7 @@ translate_raster <- function(raster, steps_x = NULL, steps_y = NULL, else { # return warning if simply = TRUE because not possible if return_input = TRUE (only verbose = TRUE) - if (simplify && verbose) { + if (simplify) { warning("'simplify = TRUE' not possible for 'return_input = TRUE'.", call. = FALSE) diff --git a/codemeta.json b/codemeta.json index 09066327..6576112d 100644 --- a/codemeta.json +++ b/codemeta.json @@ -7,7 +7,7 @@ "codeRepository": "https://r-spatialecology.github.io/shar/", "issueTracker": "https://github.com/r-spatialecology/shar/issues/", "license": "https://spdx.org/licenses/GPL-3.0", - "version": "2.1", + "version": "2.1.1", "programmingLanguage": { "@type": "ComputerLanguage", "name": "R", @@ -192,27 +192,27 @@ }, "7": { "@type": "SoftwareApplication", - "identifier": "spatstat.model", - "name": "spatstat.model", + "identifier": "spatstat.geom", + "name": "spatstat.geom", "provider": { "@id": "https://cran.r-project.org", "@type": "Organization", "name": "Comprehensive R Archive Network (CRAN)", "url": "https://cran.r-project.org" }, - "sameAs": "https://CRAN.R-project.org/package=spatstat.model" + "sameAs": "https://CRAN.R-project.org/package=spatstat.geom" }, "8": { "@type": "SoftwareApplication", - "identifier": "spatstat.geom", - "name": "spatstat.geom", + "identifier": "spatstat.model", + "name": "spatstat.model", "provider": { "@id": "https://cran.r-project.org", "@type": "Organization", "name": "Comprehensive R Archive Network (CRAN)", "url": "https://cran.r-project.org" }, - "sameAs": "https://CRAN.R-project.org/package=spatstat.geom" + "sameAs": "https://CRAN.R-project.org/package=spatstat.model" }, "9": { "@type": "SoftwareApplication", @@ -250,7 +250,7 @@ }, "SystemRequirements": null }, - "fileSize": "723.822KB", + "fileSize": "1021.596KB", "citation": [ { "@type": "ScholarlyArticle", diff --git a/cran-comments.md b/cran-comments.md index 17ecf01b..26ae2cd2 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,5 +1,8 @@ For details changes, please see NEWS.md +## shar 2.1.1 +Minor bug fixes + ## shar 2.1 Speed improvements @@ -86,14 +89,3 @@ which is together alreadyv more than 10 min which is the CRAN threshold for a pa running tests for arch 'x64' ... [164s] OK * Renamed package from `SHAR` to `shar` - -## Test environments -* macOS-latest, R: 'release' -* windows-latest, R: 'release' -* ubuntu-latest, R: 'release' - -## R CMD check results -0 errors | 0 warnings | 0 note - -## Reverse dependencies -There are currently no reverse dependencies. diff --git a/tests/testthat.R b/tests/testthat.R index 5c8bff32..2a97cd90 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -1,8 +1,12 @@ -library(dplyr) -library(shar) -library(spatstat.explore) -library(spatstat.model) +# 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/testing-design.html#sec-tests-files-overview +# * https://testthat.r-lib.org/articles/special-files.html + library(testthat) +library(shar) test_check("shar") - diff --git a/tests/testthat/test-calculate_energy.R b/tests/testthat/test-calculate_energy.R index e0333657..8bb4755b 100644 --- a/tests/testthat/test-calculate_energy.R +++ b/tests/testthat/test-calculate_energy.R @@ -1,6 +1,7 @@ -testthat::context("test-calculate_energy") +# testthat::context("test-calculate_energy") -pattern_random_a <- fit_point_process(pattern = species_a, n_random = 3, verbose = FALSE) +pattern_random_a <- fit_point_process(pattern = species_a, n_random = 3, + verbose = FALSE) pattern_random_b <- fit_point_process(pattern = species_b, n_random = 3, return_input = FALSE, verbose = FALSE) @@ -55,13 +56,11 @@ testthat::test_that("calculate_energy returns works for reconstructed marks", { testthat::test_that("calculate_energy returns error if observed not included", { testthat::expect_error(calculate_energy(pattern_random_b, verbose = FALSE), - regexp = "Input must include 'observed' pattern.", - fixed = TRUE) + regexp = "Input must include 'observed' pattern.") }) testthat::test_that("calculate_energy returns error if wrong class ", { testthat::expect_error(calculate_energy(list(species_a, species_b), verbose = FALSE), - regexp = "Class of 'pattern' must be 'rd_pat' or 'rd_mar'.", - fixed = TRUE) + regexp = "Class of 'pattern' must be 'rd_pat' or 'rd_mar'.") }) diff --git a/tests/testthat/test-classify_habitats.R b/tests/testthat/test-classify_habitats.R index d8f0b275..20014e33 100644 --- a/tests/testthat/test-classify_habitats.R +++ b/tests/testthat/test-classify_habitats.R @@ -1,4 +1,4 @@ -testthat::context("test-classify_habitats") +# testthat::context("test-classify_habitats") landscape_classified <- classify_habitats(raster = terra::rast(landscape), n = 5, style = "fisher") diff --git a/tests/testthat/test-classint_to_vector.R b/tests/testthat/test-classint_to_vector.R index 0d0996e8..e1d70619 100644 --- a/tests/testthat/test-classint_to_vector.R +++ b/tests/testthat/test-classint_to_vector.R @@ -1,4 +1,4 @@ -testthat::context("test-classint_to_vector") +# testthat::context("test-classint_to_vector") x <- classInt::classIntervals(var = stats::runif(n = 100), style = "fisher", n = 5) diff --git a/tests/testthat/test-create_neighbourhood.R b/tests/testthat/test-create_neighbourhood.R index 6022db3f..d4c308dc 100644 --- a/tests/testthat/test-create_neighbourhood.R +++ b/tests/testthat/test-create_neighbourhood.R @@ -1,4 +1,4 @@ -testthat::context("test-create_neighbourhood") +# testthat::context("test-create_neighbourhood") mat <- matrix(1, nrow = 10, ncol = 10) @@ -27,6 +27,5 @@ testthat::test_that("create_neighbourhood returns right dimension for directions testthat::test_that("create_neighbourhood returns error", { testthat::expect_error(create_neighbourhood(cell_id, mat, directions = 12), - regexp = "'directions must be 'directions = 4' or 'directions = 8'.", - fixed = TRUE) + regexp = "'directions must be 'directions = 4' or 'directions = 8'.") }) diff --git a/tests/testthat/test-extract_points.R b/tests/testthat/test-extract_points.R index 5667b2e1..02f34220 100644 --- a/tests/testthat/test-extract_points.R +++ b/tests/testthat/test-extract_points.R @@ -1,4 +1,4 @@ -testthat::context("test-extract_points") +# testthat::context("test-extract_points") landscape_classified <- classify_habitats(terra::rast(landscape), n = 5, style = "fisher") diff --git a/tests/testthat/test-fit-point_process.R b/tests/testthat/test-fit-point_process.R index be3d7368..15233e7e 100644 --- a/tests/testthat/test-fit-point_process.R +++ b/tests/testthat/test-fit-point_process.R @@ -1,4 +1,4 @@ -testthat::context("test-fit_point_process") +# testthat::context("test-fit_point_process") # normal pattern_random <- fit_point_process(pattern = species_b, n_random = 3, @@ -62,32 +62,29 @@ testthat::test_that("Input pattern can not be returned for fit_point_process", { testthat::test_that("simplify works for fit_point_process", { - testthat::expect_is(pattern_random_simple, "ppp") + testthat::expect_s3_class(pattern_random_simple, "ppp") }) testthat::test_that("fit_point_process returns errors", { testthat::expect_error(fit_point_process(pattern = species_b, n_random = -10, verbose = FALSE), - regexp = "n_random must be >= 1.", - fixed = TRUE) + regexp = "n_random must be >= 1.") - testthat::expect_error(fit_point_process(pattern = species_b, n_random = 19, - process = "not_valid", verbose = FALSE), - regexp = "Please select either 'poisson' or 'cluster'.", - fixed = TRUE) + testthat::expect_error(fit_point_process(pattern = species_b, + n_random = 19, process = "not_valid", + verbose = FALSE), + regexp = "Please select either 'poisson' or 'cluster'.") }) testthat::test_that("fit_point_process returns warnings", { - testthat::expect_warning(fit_point_process(pattern = species_a, n_random = 3, - return_input = FALSE, simplify = TRUE), - regexp = "'simplify = TRUE' not possible for 'n_random > 1'.", - fixed = TRUE) + testthat::expect_warning(fit_point_process(pattern = species_a, + n_random = 3, return_input = FALSE, + simplify = TRUE, verbose = FALSE), + regexp = "'simplify = TRUE' not possible for 'n_random > 1'.") - testthat::expect_warning(fit_point_process(pattern = species_a, n_random = 1, - simplify = TRUE), - regexp = "'simplify = TRUE' not possible for 'return_input = TRUE'.", - fixed = TRUE) + testthat::expect_warning(fit_point_process(pattern = species_a, + n_random = 1, simplify = TRUE, verbose = FALSE), + regexp = "'simplify = TRUE' not possible for 'return_input = TRUE'.") }) - diff --git a/tests/testthat/test-list_to_randomized.R b/tests/testthat/test-list_to_randomized.R index 6e048ab0..f14ac20a 100644 --- a/tests/testthat/test-list_to_randomized.R +++ b/tests/testthat/test-list_to_randomized.R @@ -1,8 +1,10 @@ -testthat::context("test-list_to_randomized") +# testthat::context("test-list_to_randomized") pattern_random <- lapply(X = 1:3, function(i) { fit_point_process(pattern = species_b, n_random = 1, - return_input = FALSE, simplify = TRUE, verbose = FALSE)}) + return_input = FALSE, simplify = TRUE, + verbose = FALSE) + }) pattern_conv <- list_to_randomized(list = pattern_random, observed = species_b) diff --git a/tests/testthat/test-pack-unpack.R b/tests/testthat/test-pack-unpack.R index 86b605bf..0379b2fa 100644 --- a/tests/testthat/test-pack-unpack.R +++ b/tests/testthat/test-pack-unpack.R @@ -1,4 +1,4 @@ -testthat::context("test-pack_randomized") +# testthat::context("test-pack_randomized") landscape_classified <- classify_habitats(terra::rast(landscape), n = 5, style = "fisher") landscape_classified[terra::values(landscape_classified) != 1] <- 2 @@ -20,7 +20,7 @@ testthat::test_that("pack_randomized wraps raster", { }) -testthat::context("test-pack_randomized") +# testthat::context("test-pack_randomized") y <- unpack_randomized(raster = x) y_ni <- unpack_randomized(raster = x_ni) diff --git a/tests/testthat/test-plot_energy.R b/tests/testthat/test-plot_energy.R index b1d8b099..e2aa5a7b 100644 --- a/tests/testthat/test-plot_energy.R +++ b/tests/testthat/test-plot_energy.R @@ -1,10 +1,11 @@ -testthat::context("test-plot_energy") +# testthat::context("test-plot_energy") pattern_reconstruction <- reconstruct_pattern(pattern = species_a, n_random = 3, max_runs = 100, verbose = FALSE) -pattern_fitted <- fit_point_process(pattern = species_a, n_random = 3, verbose = FALSE) +pattern_fitted <- fit_point_process(pattern = species_a, n_random = 3, + verbose = FALSE) ################################################################################ @@ -22,10 +23,8 @@ testthat::test_that("plot_energy uses colours", { testthat::test_that("plot_energy returns error", { testthat::expect_error(plot_energy(pattern = 1:10), - regexp = "Class of 'pattern' must be 'rd_pat' or 'rd_mar'.", - fixed = TRUE) + regexp = "Class of 'pattern' must be 'rd_pat' or 'rd_mar'.") testthat::expect_error(plot_energy(pattern_fitted), - regexp = "There is no 'energy_df' slot. Please use pattern reconstruction for valid input data.", - fixed = TRUE) + regexp = "There is no 'energy_df' slot. Please use pattern reconstruction for valid input data.") }) diff --git a/tests/testthat/test-plot_rd_pat.R b/tests/testthat/test-plot_rd_pat.R index e9fa8d8b..5955c101 100644 --- a/tests/testthat/test-plot_rd_pat.R +++ b/tests/testthat/test-plot_rd_pat.R @@ -1,4 +1,4 @@ -testthat::context("test-plot_rd_pat") +# testthat::context("test-plot_rd_pat") pattern_random <- fit_point_process(species_a, n_random = 3, verbose = FALSE) @@ -26,8 +26,7 @@ testthat::test_that("plot returns plot", { testthat::test_that("plot returns error if observed is missing", { testthat::expect_error(plot(pattern_random_ni, verbose = FALSE), - regexp = "Input must include 'observed' pattern.", - fixed = TRUE) + regexp = "Input must include 'observed' pattern.") }) testthat::test_that("plot works for reconstructed marks", { @@ -39,6 +38,5 @@ testthat::test_that("plot works for reconstructed marks", { testthat::test_that("plot returns error if what is wrong", { testthat::expect_error(plot(pattern_random, what = "wrong", verbose = FALSE), - regexp = "Please select either what = 'sf' or what = 'pp'.", - fixed = TRUE) + regexp = "Please select either what = 'sf' or what = 'pp'.") }) diff --git a/tests/testthat/test-plot_rd_ras.R b/tests/testthat/test-plot_rd_ras.R index 59705fcd..5145eb3a 100644 --- a/tests/testthat/test-plot_rd_ras.R +++ b/tests/testthat/test-plot_rd_ras.R @@ -1,6 +1,7 @@ -testthat::context("test-plot_rd_ras") +# testthat::context("test-plot_rd_ras") -landscape_classified <- classify_habitats(raster = terra::rast(landscape), n = 5, style = "fisher") +landscape_classified <- classify_habitats(raster = terra::rast(landscape), n = 5, + style = "fisher") raster_random <- translate_raster(raster = landscape_classified, steps_x = 1:2, steps_y = 1:1, verbose = FALSE) @@ -36,20 +37,17 @@ testthat::test_that("plot returns plot if n vector is specified", { testthat::test_that("plot returns error if observed is missing", { testthat::expect_error(plot(raster_random_ni, verbose = FALSE), - regexp = "Input must include 'observed' raster.", - fixed = TRUE) + regexp = "Input must include 'observed' raster.") }) testthat::test_that("plot returns error if wrong id are selected ", { testthat::expect_error(plot(raster_random, n = c(100, 101, 102), verbose = FALSE), - regexp = "Please provide at least on valid ID for n.", - fixed = TRUE) + regexp = "Please provide at least on valid ID for n.") }) testthat::test_that("plot returns warning if more than 10 classes are present", { testthat::expect_warning(plot(raster_random_cont), - regexp = "The raster has more than 10 classes. Please make sure discrete classes are provided.", - fixed = TRUE) + regexp = "The raster has more than 10 classes. Please make sure discrete classes are provided.") }) diff --git a/tests/testthat/test-print.R b/tests/testthat/test-print.R index 37fb90b5..38161ab8 100644 --- a/tests/testthat/test-print.R +++ b/tests/testthat/test-print.R @@ -1,4 +1,4 @@ -testthat::context("test-print") +# testthat::context("test-print") pattern_reconstruction <- reconstruct_pattern(pattern = species_a, n_random = 3, max_runs = 1, verbose = FALSE) diff --git a/tests/testthat/test-randomize_raster.R b/tests/testthat/test-randomize_raster.R index ce3c0e8e..520eebcf 100644 --- a/tests/testthat/test-randomize_raster.R +++ b/tests/testthat/test-randomize_raster.R @@ -1,4 +1,4 @@ -testthat::context("test-randomize_raster") +# testthat::context("test-randomize_raster") landscape_classified <- classify_habitats(raster = terra::rast(landscape), n = 5, style = "fisher") @@ -21,7 +21,7 @@ landscape_wrong[1:50] <- NA testthat::test_that("Output is as long as n_random for randomize_raster", { - testthat::expect_is(landscape_random, class = "rd_ras") + testthat::expect_s3_class(landscape_random, class = "rd_ras") testthat::expect_length(landscape_random$randomized, n = 1) }) @@ -46,32 +46,27 @@ testthat::test_that("Input raster can not be returned for randomize_raster", { testthat::test_that("simplify works for randomize_raster", { - testthat::expect_is(raster_random_simple, "SpatRaster") + testthat::expect_s4_class(raster_random_simple, "SpatRaster") }) testthat::test_that("randomize_raster returns error of n_random < 1", { testthat::expect_error(randomize_raster(raster = landscape_classified, n_random = 0, - verbose = FALSE), - regexp = "n_random must be >= 1.", - fixed = TRUE) + verbose = FALSE), regexp = "n_random must be >= 1.") }) testthat::test_that("randomize_raster returns all warnings", { testthat::expect_warning(randomize_raster(raster = landscape_classified, n_random = 1, simplify = TRUE), - regexp = "'simplify = TRUE' not possible for 'return_input = TRUE'.", - fixed = TRUE) + regexp = "'simplify = TRUE' not possible for 'return_input = TRUE'.") testthat::expect_warning(randomize_raster(raster = landscape_classified, n_random = 2, simplify = TRUE, return_input = FALSE), - regexp = "'simplify = TRUE' not possible for 'n_random > 1'.", - fixed = TRUE) + regexp = "'simplify = TRUE' not possible for 'n_random > 1'.") testthat::expect_warning(randomize_raster(raster = terra::rast(landscape), n_random = 1), - regexp = "The raster has more than 10 classes. Please make sure discrete classes are provided.", - fixed = TRUE) + regexp = "The raster has more than 10 classes. Please make sure discrete classes are provided.") }) testthat::test_that("Warning if NA are present", { diff --git a/tests/testthat/test-reconstruct_pattern.R b/tests/testthat/test-reconstruct_pattern.R index d4302c24..6dd1bfea 100644 --- a/tests/testthat/test-reconstruct_pattern.R +++ b/tests/testthat/test-reconstruct_pattern.R @@ -1,4 +1,4 @@ -testthat::context("test-reconstruct_pattern") +# testthat::context("test-reconstruct_pattern") # normal reconstruction pattern_recon_homo <- reconstruct_pattern(pattern = species_a, n_random = 3, @@ -24,15 +24,17 @@ pattern_recon_simple <- reconstruct_pattern(pattern = species_a, n_random = 1, max_runs = 1, simplify = TRUE, return_input = FALSE, verbose = FALSE) +pattern_empty <- spatstat.geom::ppp() + ################################################################################ testthat::test_that("reconstruct_pattern returns correct class", { - testthat::expect_is(pattern_recon_homo, class = "rd_pat") + testthat::expect_s3_class(pattern_recon_homo, class = "rd_pat") - testthat::expect_is(pattern_recon_cluster, class = "rd_pat") + testthat::expect_s3_class(pattern_recon_cluster, class = "rd_pat") - testthat::expect_is(pattern_recon_hetero, class = "rd_pat") + testthat::expect_s3_class(pattern_recon_hetero, class = "rd_pat") }) @@ -77,25 +79,30 @@ testthat::test_that("Reconstruction stops if e_threshold is reached", { testthat::test_that("simplify works for reconstruct_pattern", { - testthat::expect_is(pattern_recon_simple, "ppp") + testthat::expect_s3_class(pattern_recon_simple, "ppp") }) -testthat::test_that("reconstruct_pattern returns error if n_random < 1", { +testthat::test_that("reconstruct_pattern returns errors", { testthat::expect_error(reconstruct_pattern(pattern = species_a, n_random = -5, verbose = FALSE), regexp = "n_random must be >= 1.") + + testthat::expect_error(reconstruct_pattern(pattern = pattern_empty, n_random = 199), + regexp = "The observed pattern contains no points.") + }) testthat::test_that("reconstruct_pattern returns warnings", { - testthat::expect_warning(reconstruct_pattern(pattern = species_a, - n_random = 2, max_runs = 1, - return_input = FALSE, simplify = TRUE), + testthat::expect_warning(reconstruct_pattern(pattern = species_a, n_random = 2, + max_runs = 1, return_input = FALSE, + simplify = TRUE, verbose = FALSE), regexp = "'simplify = TRUE' not possible for 'n_random > 1'.") - testthat::expect_warning(reconstruct_pattern(pattern = species_a, - n_random = 1, max_runs = 1, - simplify = TRUE), + testthat::expect_warning(reconstruct_pattern(pattern = species_a, n_random = 1, + max_runs = 1, simplify = TRUE, + verbose = FALSE), regexp = "'simplify = TRUE' not possible for 'return_input = TRUE'.") }) + diff --git a/tests/testthat/test-reconstruct_pattern_marks.R b/tests/testthat/test-reconstruct_pattern_marks.R index b1e926f6..2407d352 100644 --- a/tests/testthat/test-reconstruct_pattern_marks.R +++ b/tests/testthat/test-reconstruct_pattern_marks.R @@ -1,4 +1,4 @@ -testthat::context("test-reconstruct_pattern_marks") +# testthat::context("test-reconstruct_pattern_marks") pattern_recon <- reconstruct_pattern(species_a, n_random = 1, return_input = FALSE, simplify = TRUE, max_runs = 1, @@ -25,11 +25,13 @@ marks_recon_energy <- reconstruct_pattern_marks(pattern = pattern_recon, marked_ n_random = 3, e_threshold = 0.1, verbose = FALSE) +pattern_recon_empty <- pattern_recon[-c(1:pattern_recon$n)] + ################################################################################ testthat::test_that("Output is a long as n_random for reconstruct_pattern_marks", { - testthat::expect_is(marks_recon, class = "rd_mar") + testthat::expect_s3_class(marks_recon, class = "rd_mar") testthat::expect_type(marks_recon$randomized, type = "list") @@ -52,7 +54,7 @@ testthat::test_that("Input pattern can not be returned for reconstruct_pattern_m testthat::test_that("Only pattern can be returned for simplify = TRUE", { - testthat::expect_is(marks_recon_simple, "ppp") + testthat::expect_s3_class(marks_recon_simple, "ppp") }) testthat::test_that("Reconstruction stops if e_threshold is reached", { @@ -70,29 +72,31 @@ testthat::test_that("All errors are returned for reconstruct_pattern_marks", { marked_pattern = marks_sub, n_random = -5, max_runs = 1, verbose = FALSE), - regexp = "n_random must be >= 1.", - fixed = TRUE) + regexp = "n_random must be >= 1.") testthat::expect_error(reconstruct_pattern_marks(pattern = pattern_recon, marked_pattern = pattern_recon, n_random = 3, max_runs = 1, verbose = FALSE), - regexp = "'pattern' must be unmarked and 'marked_pattern' marked", - fixed = TRUE) + regexp = "'pattern' must be unmarked and 'marked_pattern' marked") testthat::expect_error(reconstruct_pattern_marks(pattern = marks_sub, marked_pattern = marks_sub, n_random = 3, max_runs = 1, verbose = FALSE), - regexp = "'pattern' must be unmarked and 'marked_pattern' marked", - fixed = TRUE) + regexp = "'pattern' must be unmarked and 'marked_pattern' marked") testthat::expect_error(reconstruct_pattern_marks(pattern = pattern_recon, marked_pattern = spatstat.geom::subset.ppp(species_a, select = status), n_random = 3, max_runs = 1), - regexp = "marks must be 'numeric'", - fixed = TRUE) + regexp = "marks must be 'numeric'") + + testthat::expect_error(reconstruct_pattern_marks(pattern = pattern_recon_empty, + marked_pattern = marks_sub, + verbose = FALSE), + regexp = "At least one of the observed patterns contain no points.") + }) testthat::test_that("All warnings are returned for reconstruct_pattern_marks", { @@ -102,13 +106,11 @@ testthat::test_that("All warnings are returned for reconstruct_pattern_marks", { n_random = 2, max_runs = 1, return_input = FALSE, simplify = TRUE), - regexp = "'simplify = TRUE' not possible for 'n_random > 1'", - fixed = TRUE) + regexp = "'simplify = TRUE' not possible for 'n_random > 1'") testthat::expect_warning(reconstruct_pattern_marks(pattern = pattern_recon, marked_pattern = marks_sub, n_random = 1, max_runs = 1, simplify = TRUE), - regexp = "'simplify = TRUE' not possible for 'return_input = TRUE'", - fixed = TRUE) + regexp = "'simplify = TRUE' not possible for 'return_input = TRUE'") }) diff --git a/tests/testthat/test-results_habitat_association.R b/tests/testthat/test-results_habitat_association.R index 0eee78b1..ccec218c 100644 --- a/tests/testthat/test-results_habitat_association.R +++ b/tests/testthat/test-results_habitat_association.R @@ -1,4 +1,4 @@ -testthat::context("test-results_habitat_association") +# testthat::context("test-results_habitat_association") set.seed(42) @@ -80,20 +80,20 @@ testthat::test_that("results_habitat_association returns breaks", { testthat::test_that("results_habitat_association returns warning if significance_threshold is not meaningful", { testthat::expect_warning(results_habitat_association(raster = landscape_classified$raster, - pattern = random_a, - significance_level = 0.75), + pattern = random_a, significance_level = 0.75, + verbose = FALSE), regexp = "Make sure 'signifcance_level' is meaningful (e.g. 'significance_level = 0.05').", fixed = TRUE) }) testthat::test_that("results_habitat_association returns warning if more than 25 classes are present", { - testthat::expect_warning(results_habitat_association(raster = terra::rast(landscape), - pattern = random_a), + testthat::expect_warning(results_habitat_association(raster = terra::rast(landscape), pattern = random_a, + verbose = FALSE), regexp = "The raster has more than 25 classes. You can ignore this warning if your raster data is discrete.") - testthat::expect_warning(results_habitat_association(raster = raster_random_cont, - pattern = species_a), + testthat::expect_warning(results_habitat_association(raster = raster_random_cont, pattern = species_a, + verbose = FALSE), regexp = "The raster has more than 25 classes. You can ignore this warning if your raster data is discrete.") }) diff --git a/tests/testthat/test-sample_randomized.R b/tests/testthat/test-sample_randomized.R index 85f849e0..523d2121 100644 --- a/tests/testthat/test-sample_randomized.R +++ b/tests/testthat/test-sample_randomized.R @@ -1,4 +1,4 @@ -testthat::context("test-plot_rd_pat") +# testthat::context("test-plot_rd_pat") pattern_random <- fit_point_process(species_a, n_random = 9, verbose = FALSE) diff --git a/tests/testthat/test-translate_raster.R b/tests/testthat/test-translate_raster.R index dbe8b64e..6a78c588 100644 --- a/tests/testthat/test-translate_raster.R +++ b/tests/testthat/test-translate_raster.R @@ -1,4 +1,4 @@ -testthat::context("test-translate_raster") +# testthat::context("test-translate_raster") # n_random <- (terra::nrow(terra::rast(landscape)) + 1) * # terra::ncol(terra::rast(landscape)) + 1) - 4 @@ -63,8 +63,7 @@ testthat::test_that("Providing steps is working for translate_raster", { testthat::test_that("simplify is working for translate_raster", { - testthat::expect_is(landscape_random_simple, - class = "SpatRaster") + testthat::expect_s4_class(landscape_random_simple, class = "SpatRaster") }) testthat::test_that("Warning if more than 10 classes are present for translate_raster", {