Skip to content

Commit

Permalink
Merge pull request #18 from EvolEcolGroup/warning_sample_pseuabs
Browse files Browse the repository at this point in the history
Warning sample pseuabs
close #17
  • Loading branch information
dramanica authored Oct 4, 2023
2 parents f8c190c + 97a7fd6 commit 4639dca
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 4 deletions.
3 changes: 1 addition & 2 deletions R/sample_pseudoabs.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ sample_pseudoabs <- function (data, raster, n, coords = NULL,
if (length(cell_id)>n){
cell_id <- sample(x= cell_id, size = n)
} else {
warning("There are fewer available cells in the raster than the requested ", n, " points.\n",
"Only ", length(cell_id), " will be returned.")
warning("There are fewer available cells for raster '",time(sampling_raster),"' (",nrow(xy_pres)," presences) than the requested ", n, " pseudoabsences. Only ", length(cell_id), " will be returned.\n")
}
pseudoabsences <- as.data.frame (terra::xyFromCell(sampling_raster, cell_id))
# fix the coordinate names to be the same we started with
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test_sample_pseudoabs.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ test_that("handling of data frames and sf objects", {
locations_sf <- sf::st_as_sf(locations, coords=c("lon","lat")) %>% sf::st_set_crs(4326)
expect_error(sample_pseudoabs(locations_sf, coords = c("x", "y"), raster = grid_raster),
"There are no recognised coordinate columns")
expect_warning(sample_pseudoabs(locations_sf, raster = grid_raster, n = 100),
"There are fewer available cells in the raster than the requested 100 points")
expect_warning(sample_pseudoabs(locations_sf, raster = grid_raster, n = 100), "There are fewer available cells for raster 'NA' (3 presences) than the requested 100 pseudoabsences. Only 52 will be returned.", fixed=TRUE)
locations_sf <- locations_sf %>% dplyr::bind_cols(sf::st_coordinates(.))
expect_no_error(sample_pseudoabs(locations_sf, coords = c("X", "Y"), raster = grid_raster, n = 25))
locations_sf$X <- rep(0, 3)
Expand Down

0 comments on commit 4639dca

Please sign in to comment.