Skip to content

Commit 4ccc6d2

Browse files
committed
CRAN compliance
- Updated documentation - Updated package framework - Package structure and verbose updated
1 parent c39d037 commit 4ccc6d2

102 files changed

Lines changed: 1087 additions & 1053 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

DESCRIPTION

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Package: gbif.range
22
Version: 1.6.3
3-
Title: Tools for GBIF Retrieval and Ecoregion-Based Species Range Mapping
3+
Title: Species Range Mapping from GBIF Using Ecoregion Constraints
44
Depends: R (>= 4.0.0)
55
Imports: terra, rgbif, CoordinateCleaner, sf, ClusterR, FNN, geometry,
6-
cluster, rnaturalearth, mclust, methods, utils, zip, class,
7-
NMOF
6+
cluster, mclust, methods, utils, zip, class, NMOF, stats, tools,
7+
graphics, grDevices
88
Suggests:
99
data.table,
1010
knitr,
@@ -25,8 +25,9 @@ Description: Provides a workflow to retrieve, filter, and analyze 'GBIF'
2525
et al. 2007 <doi:10.1641/B570707>), and freshwater (Abell et al. 2008
2626
<doi:10.1641/B580507>) realms. The 'GBIF' API is accessed via the
2727
'rgbif' package, and coordinate cleaning uses 'CoordinateCleaner'
28-
(Zizka et al. 2019 <doi:10.1111/2041-210X.13152>).
29-
License: GPL (>= 3) | file LICENCE
28+
(Zizka et al. 2019 <doi:10.1111/2041-210X.13152>). The 'GBIF' API is
29+
described at <https://www.gbif.org/developer/summary>.
30+
License: GPL (>= 3)
3031
URL: https://github.com/8Ginette8/gbif.range,
3132
https://8ginette8.github.io/gbif.range/
3233
BugReports: https://github.com/8Ginette8/gbif.range/issues

LICENCE

Lines changed: 0 additions & 21 deletions
This file was deleted.

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
S3method(plot,gbifPackedSpatRaster)
44
S3method(plot,gbifPackedSpatVector)
55
export(check_and_get_ecoreg)
6+
export(cscl)
67
export(cv_range)
78
export(ecoreg_list)
89
export(evaluate_range)
10+
export(fig_label)
11+
export(getGBIF)
912
export(get_doi)
1013
export(get_ecoreg)
1114
export(get_gbif)
@@ -20,6 +23,7 @@ export(read_ecoreg)
2023
export(read_range_rds)
2124
export(species_csvs_to_ranges)
2225
export(split_gbif_by_species)
26+
exportClasses(getRange)
2327
importFrom(ClusterR,KMeans_rcpp)
2428
importFrom(CoordinateCleaner,cd_ddmm)
2529
importFrom(CoordinateCleaner,cd_round)

R/classes.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
#' Stores the original arguments used to build a range map and the resulting
77
#' spatial output.
88
#'
9-
#' @keywords internal
9+
#' @return A generator object of reference class \code{"getRange"}, used to
10+
#' instantiate objects with two fields: \code{init.args} (the original
11+
#' arguments used to build the range map) and \code{rangeOutput} (the
12+
#' resulting spatial output).
13+
#' @export
1014
getRange <- setRefClass("getRange",
1115
fields = list(
1216
init.args = "ANY",
@@ -23,8 +27,8 @@ getRange <- setRefClass("getRange",
2327
#' in the package.
2428
#'
2529
#' @param df A data frame containing GBIF occurrence records.
26-
#' @return An object of class \code{getGBIF}.
27-
#' @keywords internal
30+
#' @return An object of class \code{c("getGBIF", "data.frame")}.
31+
#' @export
2832
getGBIF <- function(df) {
2933
if (!is.data.frame(df)) {
3034
stop("Input must be a data.frame")

R/conv_function.R

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
#' @param temp_dir Character string giving the temporary directory used for
1616
#' intermediate convex-hull files.
1717
#' @param g Optional ecoregion identifier used in status messages.
18-
#' @keywords internal
18+
#' @param verbose Logical. Should status messages be printed?
1919
#' @importFrom terra crds buffer aggregate vect crs
2020
#' @importFrom sf st_polygon
21+
#' @noRd
2122
conv_function <- function (sp_coord,
2223
bwp,
2324
bipl,
2425
bwpo,
2526
temp_dir,
26-
g = NULL){
27+
g = NULL,
28+
verbose = TRUE) {
2729

2830
# Preps and convert degrees to meters
2931
x <- terra::crds(sp_coord)
@@ -55,12 +57,14 @@ conv_function <- function (sp_coord,
5557
if (all(abs(is.line) == 0)) {
5658

5759
# Print
58-
cat(
59-
'\n[ecoreg =',g,nrow(x),
60-
'points lying on one line. Using buffer width of ',
61-
bwp.bipl.m/1000,
62-
'km]\n'
63-
)
60+
if (isTRUE(verbose)) {
61+
message(
62+
"[ecoreg = ", g, "] ", nrow(x),
63+
" points lying on one line. Using buffer width of ",
64+
bwp.bipl.m / 1000,
65+
" km"
66+
)
67+
}
6468

6569
# Buffer
6670
rtn <- terra::buffer(

R/cv_range.R

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#' the total number of spatial blocks as \code{nfolds * nblocks}.
1717
#' @param backpoints Numeric. Number of regularly spaced background points used
1818
#' as pseudo-absences. Default is \code{10000}.
19+
#' @param verbose Logical. Should fold progress messages be printed?
1920
#' @details The function rebuilds the range map \code{nfolds} times. In each
2021
#' iteration, one fold is reserved for evaluation and the remaining folds are
2122
#' used for training.
@@ -31,23 +32,26 @@
3132
#' columns \code{TP}, \code{FA}, \code{TA}, \code{FP}, \code{Precision},
3233
#' \code{Sensitivity}, \code{Specificity}, and \code{TSS}.
3334
#' @references
34-
#' Roberts, D. R., Bahn, V., Ciuti, S., Boyce, M. S., Elith, J., Guillera
35-
#' Arroita, G., ... & Dormann, C. F. (2017). Crossvalidation strategies
35+
#' Roberts, D. R., Bahn, V., Ciuti, S., Boyce, M. S., Elith, J., Guillera-
36+
#' Arroita, G., ... & Dormann, C. F. (2017). Cross-validation strategies
3637
#' for data with temporal, spatial, hierarchical, or phylogenetic structure.
37-
#' Ecography, 40(8), 913-929.
38-
#'
38+
#' Ecography, 40(8), 913-929. \doi{10.1111/ecog.02881}
39+
#'
3940
#' Chauvier, Y., Zimmermann, N. E., Poggiato, G., Bystrova, D., Brun, P.,
4041
#' & Thuiller, W. (2021). Novel methods to correct for observer and sampling
41-
#' bias in presence‐only species distribution models. Global Ecology and
42-
#' Biogeography, 30(11), 2312-2325.
42+
#' bias in presence-only species distribution models. Global Ecology and
43+
#' Biogeography, 30(11), 2312-2325. \doi{10.1111/geb.13383}
44+
#' @seealso \code{\link{get_range}}() to build the range map being evaluated,
45+
#' and \code{\link{make_blocks}}() for the underlying fold-assignment logic.
4346
#' @example inst/examples/cv_range_help.R
4447
#' @importFrom terra ext extract
4548
#' @export
4649
cv_range <- function(range_object = NULL,
4750
cv = 'random-cv',
4851
nfolds = 5,
4952
nblocks = 2,
50-
backpoints = 1e4){
53+
backpoints = 1e4,
54+
verbose = TRUE) {
5155

5256
######################################################
5357
### Stop messages
@@ -64,6 +68,7 @@ cv_range <- function(range_object = NULL,
6468
check_numeric(nfolds, "nfolds")
6569
check_numeric(nblocks, "nblocks")
6670
check_numeric(backpoints, "backpoints")
71+
check_logical(verbose, "verbose")
6772

6873

6974
######################################################
@@ -132,7 +137,7 @@ cv_range <- function(range_object = NULL,
132137
# Run nfolds time the get_range function + evaluation
133138
for (i in 1:nfolds)
134139
{
135-
cat("...fold", i, sep="")
140+
if (isTRUE(verbose)) message("...fold", i, appendLF = FALSE)
136141

137142
# Extract all but %nfolds
138143
xy.fit <- all.xy[!cv.strat %in% i,]
@@ -182,7 +187,7 @@ cv_range <- function(range_object = NULL,
182187
cv.df[i, "Sensitivity"] + cv.df[i, "Specificity"] - 1
183188
}
184189

185-
cat("","\n")
190+
if (isTRUE(verbose)) message("")
186191

187192
# Finalize average
188193
cv.df[nfolds+1, ] <- apply(cv.df[1:nfolds, ], 2, mean, na.rm = TRUE)

R/data.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
#' @source Calculated with the \code{gbif.range} package.
1515
#' @docType data
1616
#' @name area_data
17-
NULL
17+
NULL

R/evaluate_range.R

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@
3434
#' per-species evaluation statistics, and \code{overlay_list}, a list of raster
3535
#' overlays used for plotting and inspection.
3636
#' @references
37-
#' Pinkert, S., Sica, Y. V., Winner, K., & Jetz, W. (2023). The potential of
38-
#' ecoregional range maps for boosting taxonomic coverage in ecology and
39-
#' conservation. Ecography, 12, e06794.
37+
#' Pinkert, S., Sica, Y. V., Winner, K., & Jetz, W. (2023). The potential of
38+
#' ecoregional range maps for boosting taxonomic coverage in ecology and
39+
#' conservation. Ecography, 12, e06794. \doi{10.1111/ecog.06794}
40+
#' @seealso \code{\link{get_range}}() to generate the range maps being
41+
#' evaluated, and \code{\link{cv_range}}() for cross-validation against the
42+
#' original occurrence data instead of external validation data.
4043
#' @example inst/examples/evaluate_range_help.R
4144
#' @importFrom terra rast ext crs project aggregate rasterize crop extend resample values classify ncol nrow plot
4245
#' @importFrom sf st_read st_as_sf st_union st_drop_geometry st_transform
4346
#' @importFrom grDevices dev.off pdf
44-
#' @importFrom graphics legend par
47+
#' @importFrom graphics legend
4548
#' @export
4649
evaluate_range <- function(root_dir = NULL,
4750
valData_dir = NULL,
@@ -143,35 +146,33 @@ evaluate_range <- function(root_dir = NULL,
143146
))
144147
}
145148

146-
cat("-Note-", "\n")
147149
if (valData_type == "TIFF") {
148150
f.list.matches <- intersect(f.list.eco, basename(f.list.valRM))
149-
cat(
150-
sprintf(
151-
paste(
152-
"%.2f%% (%d) of the species names of ecoregions",
153-
"match with names of the validation data files"
154-
),
155-
100 * length(f.list.matches) / length(f.list.valRM),
156-
length(f.list.matches)
157-
)
151+
match_msg <- sprintf(
152+
paste(
153+
"%.2f%% (%d) of the species names of ecoregions",
154+
"match with names of the validation data files"
155+
),
156+
100 * length(f.list.matches) / length(f.list.valRM),
157+
length(f.list.matches)
158158
)
159-
160159
} else {
161160
f.list.matches <- intersect(f.list.eco, f.list.valRM)
162-
cat(
163-
sprintf(
164-
paste(
165-
"%.2f%% (%d) of the species names of ecoregions",
166-
"match with those in the validation data column 'sci_name'"
167-
),
168-
100 * length(f.list.matches) / length(f.list.valRM),
169-
length(f.list.matches)
170-
)
161+
match_msg <- sprintf(
162+
paste(
163+
"%.2f%% (%d) of the species names of ecoregions",
164+
"match with those in the validation data column 'sci_name'"
165+
),
166+
100 * length(f.list.matches) / length(f.list.valRM),
167+
length(f.list.matches)
171168
)
172169
}
173170

174-
cat("------", "\n")
171+
if (isTRUE(verbose)) {
172+
message("-Note-")
173+
message(match_msg)
174+
message("------")
175+
}
175176

176177
df.eval <- data.frame(
177178
species = f.list.matches,
@@ -188,7 +189,7 @@ evaluate_range <- function(root_dir = NULL,
188189

189190
# Process each species
190191
for (i in seq_along(f.list.matches)) {
191-
if (verbose) cat(i, " Species: ", f.list.matches[i], "\n")
192+
if (isTRUE(verbose)) message(i, " Species: ", f.list.matches[i])
192193

193194
if (is.null(mask)) {
194195
domain.raster <- NULL
@@ -321,7 +322,7 @@ evaluate_range <- function(root_dir = NULL,
321322
df.eval$Sen_ecoRM[i] + df.eval$Spec_ecoRM[i] - 1
322323

323324
# Plot the overlay raster
324-
if (verbose && print_map) {
325+
if (isTRUE(verbose) && print_map) {
325326

326327
if (!dir.exists(file.path(root_dir, "eval_output"))) {
327328
dir.create(file.path(root_dir, "eval_output"))
@@ -331,11 +332,10 @@ evaluate_range <- function(root_dir = NULL,
331332
colors <- c("gray", "red", "blue", "purple")
332333
breaks <- c(-0.5, 0.5, 1.5, 2.5, 3.5)
333334

334-
pdf(file = file.path(root_dir,"eval_output",
335+
grDevices::pdf(file = file.path(root_dir,"eval_output",
335336
paste0("Evaluation_map_", f.list.matches[i], ".pdf")),
336337
height = (11 * aspect.r)+3, width = 11
337338
)
338-
par(mfrow = c(1, 1))
339339
terra::plot(
340340
overlay.raster,
341341
col = colors,
@@ -352,7 +352,7 @@ evaluate_range <- function(root_dir = NULL,
352352
)
353353

354354
# Adding a legend
355-
legend("bottomright",
355+
graphics::legend("bottomright",
356356
legend = c(
357357
"Abs in both (TA)",
358358
"Pres in ecoRM only (FP)",
@@ -364,22 +364,22 @@ evaluate_range <- function(root_dir = NULL,
364364
box.col = NA,
365365
inset = c(0,0.1)
366366
)
367-
dev.off()
367+
grDevices::dev.off()
368368
}
369369
}
370370

371-
if (verbose) {
372-
cat("Cross-species mean Prec & Sensitivity:",
371+
if (isTRUE(verbose)) {
372+
message(
373+
"Cross-species mean Prec & Sensitivity: ",
373374
round(
374-
mean(rowMeans(df.eval[, c("Prec_ecoRM", "Sen_ecoRM")],na.rm = TRUE)),
375-
digits = 2),
376-
"\n"
375+
mean(rowMeans(df.eval[, c("Prec_ecoRM", "Sen_ecoRM")], na.rm = TRUE)),
376+
digits = 2)
377377
)
378378
}
379379

380-
if (print_map) {
381-
cat("### Maps have been saved to:",
382-
file.path(root_dir, "eval_output"), "###\n")
380+
if (isTRUE(verbose) && print_map) {
381+
message("### Maps have been saved to: ",
382+
file.path(root_dir, "eval_output"), " ###")
383383
}
384384

385385
output <- list(df_eval = df.eval, overlay_list = overlay.list)

R/gbif.range-package.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
#' @docType package
2424
#' @example inst/examples/gbif.range-package_help.R
2525
#' @keywords package
26-
"_PACKAGE"
26+
"_PACKAGE"

0 commit comments

Comments
 (0)