Skip to content

Commit b8399e6

Browse files
committed
Clarifying SAR and SpAR documentation, can suppress messages now
1 parent 166b2f3 commit b8399e6

File tree

9 files changed

+157
-35
lines changed

9 files changed

+157
-35
lines changed

R/create_SAR.R

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
#' Use segmented regression to create a species-area relationship (SAR) plot.
44
#' The X axis represents log(island area) and the Y axis represents log(number
55
#' of species)
6+
#'
7+
#' If the user would prefer to create their own plot of the
8+
#' `ssarp::create_SAR()` output, the `aggDF` element of the returned list
9+
#' includes the raw points from the plot created here. They can be accessed
10+
#' as demonstrated in the Examples section.
611
#' @param occurrences The dataframe output by `ssarp::find_areas()` (or if
712
#' using a custom dataframe, ensure that it has the following columns:
813
#' specificEpithet, areas)
914
#' @param npsi The maximum number of breakpoints to estimate for model
1015
#' selection. Default: 1
1116
#' @param visualize (boolean) Whether the plot should be displayed when the
1217
#' function is called. Default: FALSE
13-
#' @return A list of 3 including: the summary output, the segmented regression
14-
#' object, and the aggregated dataframe used to create the plot
18+
#' @return A list of 4 including: the summary output, the regression
19+
#' object, the aggregated dataframe used to create the plot, and the AIC scores
20+
#' used in model selection
1521
#' @examples
1622
#' # The GBIF key for the Anolis genus is 8782549
1723
#' # Read in example dataset filtered from:
@@ -28,7 +34,9 @@
2834
#' npsi = 1,
2935
#' visualize = FALSE)
3036
#' plot(seg)
31-
#' summary <- seg[1]
37+
#' summary <- seg$summary
38+
#' model_object <- seg$segObj
39+
#' points <- seg$aggDF
3240
#' @export
3341

3442
create_SAR <- function(occurrences, npsi = 1, visualize = FALSE) {

R/create_SpAR.R

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
#' Use segmented regression to create a speciation-area relationship plot. The
44
#' X axis represents log(island area) and the Y axis represents
55
#' log(speciation rate)
6+
#'
7+
#' If the user would prefer to create their own plot of the
8+
#' `ssarp::create_SpAR()` output, the `aggDF` element of the returned list
9+
#' includes the raw points from the plot created here. They can be accessed
10+
#' as demonstrated in the Examples section.
611
#' @param occurrences The dataframe output by one of ssarp's speciation
712
#' methods (`ssarp::estimate_BAMM()`, `ssarp::estimate_DR()`,
813
#' `ssarp::estimate_MS()`), or if using a custom dataframe, ensure that it
@@ -11,8 +16,9 @@
1116
#' selection. Default: 1
1217
#' @param visualize (boolean) Whether the plot should be displayed when the
1318
#' function is called. Default: FALSE
14-
#' @return A list of 3 including: the summary output, the segmented regression
15-
#' object, and the aggregated dataframe used to create the plot
19+
#' @return A list of 4 including: the summary output, the regression
20+
#' object, the aggregated dataframe used to create the plot, and the AIC scores
21+
#' used in model selection
1622
#' @examples
1723
#' # The GBIF key for the Anolis genus is 8782549
1824
#' # Read in example dataset filtered from:
@@ -38,7 +44,9 @@
3844
#' npsi = 1,
3945
#' visualize = FALSE)
4046
#' plot(seg)
41-
#' summary <- seg[1]
47+
#' summary <- seg$summary
48+
#' model_object <- seg$segObj
49+
#' points <- seg$aggDF
4250
#' @export
4351

4452
create_SpAR <- function(occurrences, npsi = 1, visualize = FALSE) {

R/find_areas.R

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ find_areas <- function(occs, area_custom = NULL,
9393
minus <- rep(NA, nrow(occs))
9494
# Loop through dataframe
9595
for (i in seq_len(nrow(occs))) {
96-
if (nrow(occs) == 0) {
97-
cli::cli_alert_warning("No data in occurrence record dataframe")
96+
if (nrow(occs) == 0) {
97+
if(!getOption("ssarp.silent", FALSE)){
98+
cli::cli_alert_warning("No data in occurrence record dataframe")
99+
}
98100
break
99101
}
100102
if (
@@ -128,10 +130,14 @@ find_areas <- function(occs, area_custom = NULL,
128130
# Next, go through the occs dataframe and see if the Third column has a name.
129131
# If yes, add to the island list. If NA, go to the Second column.
130132
# If Second column is NA, go to the First column.
131-
cli::cli_alert_info("Recording island names...")
133+
if(!getOption("ssarp.silent", FALSE)){
134+
cli::cli_alert_info("Recording island names...")
135+
}
132136
for (i in seq_len(nrow(occs))) {
133137
if (nrow(occs) == 0) {
134-
cli::cli_alert_warning("No data in occurrence record dataframe")
138+
if(!getOption("ssarp.silent", FALSE)){
139+
cli::cli_alert_warning("No data in occurrence record dataframe")
140+
}
135141
break
136142
}
137143
if (!is.na(occs[i, "Third"])) {
@@ -156,7 +162,9 @@ find_areas <- function(occs, area_custom = NULL,
156162
}
157163

158164
# Look through the island area file and find the names in uniq_islands list
159-
cli::cli_alert_info("Assembling island dictionary...")
165+
if(!getOption("ssarp.silent", FALSE)){
166+
cli::cli_alert_info("Assembling island dictionary...")
167+
}
160168
# Initialize vector of island names from island area dataset with
161169
# "Island" appended
162170
area_file_append <- paste0(area_file$Name, " Island")
@@ -197,7 +205,9 @@ find_areas <- function(occs, area_custom = NULL,
197205
}
198206

199207
# Use the dictionary to add the areas to the final dataframe
200-
cli::cli_alert_info("Adding areas to final dataframe...")
208+
if(!getOption("ssarp.silent", FALSE)){
209+
cli::cli_alert_info("Adding areas to final dataframe...")
210+
}
201211
areas <- rep(0, times = nrow(occs))
202212

203213
for (i in seq_len(nrow(occs))) {
@@ -227,8 +237,10 @@ find_areas <- function(occs, area_custom = NULL,
227237
if(!is.null(names)){
228238
polygons <- terra::subset(shapefile, shapefile$name %in% names)
229239
} else {
230-
cli::cli_alert_info(
240+
if(!getOption("ssarp.silent", FALSE)){
241+
cli::cli_alert_info(
231242
"Using all names in the shapefile, this might extend processing time")
243+
}
232244
# If the user did not input a "names" vector, use
233245
# the full list of polygon names
234246
# If there are any NAs in shapefile$name, remove them

R/find_land.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ find_land <- function(occurrences, fillgaps = FALSE) {
6464
# Check if there is anything in the given occurrences
6565
# If not, return NULL to stop the function call
6666
if (nrow(occurrences) == 0) {
67-
cli::cli_alert_warning("Occurrence record dataframe has no entries")
67+
if(!getOption("ssarp.silent", FALSE)){
68+
cli::cli_alert_warning("Occurrence record dataframe has no entries")
69+
}
6870
return(NULL)
6971
}
7072

@@ -119,10 +121,14 @@ find_land <- function(occurrences, fillgaps = FALSE) {
119121
if (fillgaps == TRUE) {
120122
# There might still be a lot of NA entries, so use Photon to try to
121123
# fill in gaps
122-
cli::cli_alert_info("Filling gaps using Photon...")
124+
if(!getOption("ssarp.silent", FALSE)){
125+
cli::cli_alert_info("Filling gaps using Photon...")
126+
}
123127
for (i in seq_len(nrow(occs))) {
124128
if (nrow(occs) == 0) {
125-
cli::cli_alert_warning("Occurrence record dataframe has no entries")
129+
if(!getOption("ssarp.silent", FALSE)){
130+
cli::cli_alert_warning("Occurrence record dataframe has no entries")
131+
}
126132
break
127133
}
128134
if (is.na(occs[i, "First"])) {

R/get_sources.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ get_sources <- function(occs) {
4343

4444
return(count_df)
4545
} else {
46-
cli::cli_alert_warning("datasetKey column not found.")
46+
if(!getOption("ssarp.silent", FALSE)){
47+
cli::cli_alert_warning("datasetKey column not found.")
48+
}
4749
return(NULL)
4850
}
4951
}

README.Rmd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ library(devtools)
3939
install_github("kmartinet/ssarp")
4040
```
4141

42+
## Suppressing Messages
43+
44+
To suppress all messages output by *ssarp*, run
45+
46+
``` r
47+
options(ssarp.silent = TRUE)
48+
```
49+
50+
before using any of *ssarp*'s functions.
51+
4252
## Example: Creating a Species-Area Relationship
4353

4454
A species-area relationship (SAR) visualizes the relationship between species richness (the number of species) and the area of the land mass on which the species live. This brief example covers the *ssarp* workflow for creating a SAR, and more detailed explanations of the code and methods can be found [in the Articles on the ssarp pkgdown website](https://kmartinet.github.io/ssarp/index.html).

README.md

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,23 +261,81 @@ statistical information about the model.
261261

262262
### Workflow Summary for using data from GBIF to create a species-area relationship plot
263263

264-
1. Use `rgbif` to gather occurrence records, or input your own dataframe of occurrence records.
265-
2. Use `find_land(occ, fillgaps)` with the dataframe obtained in Step 1 to figure out the names of landmasses using the occurrence record GPS points and the [*maps* R package](https://cran.r-project.org/web/packages/maps/index.html). Setting the "fillgaps" parameter to `TRUE` will enable the use of [Photon API](https://photon.komoot.io/) to fill in any missing landmass names left by the *maps* R package.
266-
3. Use `find_areas(occ, area_custom)` with the dataframe obtained in Step 2 to match the landmass names to a dataset that includes names of most islands on the planet and their areas. If the user would like to use a custom island area dataset instead of the built-in one, the "area_custom" parameter can be set to the name of the custom island area dataframe.
267-
3a. If you'd like to only include occurrence records from islands, you can remove continental records by using `remove_continents(occ)` with the dataframe returned by `find_areas()`
268-
5. Use `create_SAR(occ, npsi)` with the dataframe obtained in Step 3 to create a species-area relationship plot that reports information important to the associated regression. The "npsi" parameter indicates the maximum number of breakpoints the user would like to compare for model selection. The returned model and plot correspond with the best-fit model.
264+
1. Use `rgbif` to gather occurrence records, or input your own dataframe of
265+
occurrence records.
266+
2. Use `find_land(occ, fillgaps)` with the dataframe obtained in Step 1 to
267+
figure out the names of landmasses using the occurrence record GPS points and
268+
the [*maps* R package](https://cran.r-project.org/web/packages/maps/index.html).
269+
Setting the "fillgaps" parameter to `TRUE` will enable the use of
270+
[Photon API](https://photon.komoot.io/) to fill in any missing landmass names
271+
left by the *maps* R package.
272+
3. Use `find_areas(occ, area_custom)` with the dataframe obtained in Step 2 to
273+
match the landmass names to a dataset that includes names of most islands on
274+
the planet and their areas. If the user would like to use a custom island area
275+
dataset instead of the built-in one, the "area_custom" parameter can be set to
276+
the name of the custom island area dataframe.
277+
3a. If you'd like to only include occurrence records from islands, you can
278+
remove continental records by using `remove_continents(occ)` with the dataframe
279+
returned by `find_areas()`
280+
4. Use `create_SAR(occ, npsi)` with the dataframe obtained in Step 3 to create
281+
a species-area relationship plot that reports information important to the
282+
associated regression. The "npsi" parameter indicates the maximum number of
283+
breakpoints the user would like to compare for model selection. The returned
284+
model and plot correspond with the best-fit model.
269285

270286
### Workflow summary for using data from GBIF and a user-provided phylogenetic tree to create a speciation-area relationship plot
271287

272-
1. Use `rgbif` to gather occurrence records, or input your own dataframe of occurrence records.
273-
2. Use `find_land(occ, fillgaps)` with the dataframe obtained in Step 1 to figure out the names of landmasses using the occurrence record GPS points and the [*maps* R package](https://cran.r-project.org/web/packages/maps/index.html). Setting the "fillgaps" parameter to `TRUE` will enable the use of [Photon API](https://photon.komoot.io/) to fill in any missing landmass names left by the *maps* R package.
274-
3. Use `find_areas(occ, area_custom)` with the dataframe obtained in Step 2 to match the landmass names to a dataset that includes names of most islands on the planet and their areas. If the user would like to use a custom island area dataset instead of the built-in one, the "area_custom" parameter can be set to the name of the custom island area dataframe.
275-
3a. If you'd like to only include occurrence records from islands, you can remove continental records by using `remove_continents(occ)` with the dataframe returned by `find_areas()`
276-
4. Use either `estimate_DR(tree, label_type, occ)` or `estimate_MS(tree, label_type, occ)` with your own phylogenetic tree that corresponds with the taxa signified in previous steps, a classifier that describes your tip labels (whether the tip labels are simply species epithets or full scientific names), and the dataframe obtained in Step 3 to add tip speciation rates using the DR statistic (Jetz et al. 2012) or the lambda calculation for crown groups from Magallόn and Sanderson (2001) respectively to the occurrence dataframe. The user may also choose to estimate tip speciation rates from a BAMM analysis (Rabosky 2014) by using `estimate_BAMM(label_type, occ, edata)` with a classifier that describes your tip labels (whether the tip labels are simply species epithets or full scientific names), the occurrence record dataframe obtained in Step 3, and a bammdata object generated by reading the event data file from a BAMM analysis with the *BAMMtools* package (Rabosky et al. 2014).
277-
5. Use `create_SpAR(occ, npsi)` with the dataframe obtained in Step 4 to create a speciation-area relationship plot that reports information important to the associated regression. The "npsi" parameter indicates the maximum number of breakpoints the user would like to compare for model selection. The returned model and plot correspond with the best-fit model.
288+
1. Use `rgbif` to gather occurrence records, or input your own dataframe of
289+
occurrence records.
290+
2. Use `find_land(occ, fillgaps)` with the dataframe obtained in Step 1 to
291+
figure out the names of landmasses using the occurrence record GPS points and
292+
the [*maps* R package](https://cran.r-project.org/web/packages/maps/index.html).
293+
Setting the "fillgaps" parameter to `TRUE` will enable the use of
294+
[Photon API](https://photon.komoot.io/) to fill in any missing landmass names
295+
left by the *maps* R package.
296+
3. Use `find_areas(occ, area_custom)` with the dataframe obtained in Step 2 to
297+
match the landmass names to a dataset that includes names of most islands on
298+
the planet and their areas. If the user would like to use a custom island area
299+
dataset instead of the built-in one, the "area_custom" parameter can be set to
300+
the name of the custom island area dataframe.
301+
3a. If you'd like to only include occurrence records from islands, you can
302+
remove continental records by using `remove_continents(occ)` with the dataframe
303+
returned by `find_areas()`
304+
4. Use either `estimate_DR(tree, label_type, occ)` or
305+
`estimate_MS(tree, label_type, occ)` with your own phylogenetic tree that
306+
corresponds with the taxa signified in previous steps, a classifier that
307+
describes your tip labels (whether the tip labels are simply species epithets
308+
or full scientific names), and the dataframe obtained in Step 3 to add tip
309+
speciation rates using the DR statistic (Jetz et al. 2012) or the lambda
310+
calculation for crown groups from Magallόn and Sanderson (2001) respectively
311+
to the occurrence dataframe. The user may also choose to estimate tip
312+
speciation rates from a BAMM analysis (Rabosky 2014) by using
313+
`estimate_BAMM(label_type, occ, edata)` with a classifier that describes your
314+
tip labels (whether the tip labels are simply species epithets or full
315+
scientific names), the occurrence record dataframe obtained in Step 3, and a
316+
bammdata object generated by reading the event data file from a BAMM analysis
317+
with the *BAMMtools* package (Rabosky et al. 2014).
318+
5. Use `create_SpAR(occ, npsi)` with the dataframe obtained in Step 4 to create
319+
a speciation-area relationship plot that reports information important to the
320+
associated regression. The "npsi" parameter indicates the maximum number of
321+
breakpoints the user would like to compare for model selection. The returned
322+
model and plot correspond with the best-fit model.
278323

279324
### Some helpful notes about well-known text (WKT) representation of geometry
280-
When running `getData()`, the user can specify a well-known text (WKT) representation of geometry to restrict the geographic location of the returned occurrence records. The rgbif::occ_search function that `getData()` calls requires a counter-clockwise winding order for WKT. I find it helpful to think about WKT polygons in this way: imagine a square around your geographic area of interest and pick one of the corners as a starting point. The order of points in WKT format should follow counter-clockwise from the corner you picked first, and the final entry in the WKT string needs to be the same as the first entry. Additionally, while GPS points are typically represented in "latitude, longitude" format, WKT expects them in "longitude latitude" format with commas separating the points rather than individual longitude and latitude values. WKT polygons can have more specified points than included in this simple square example, and even include polygons nested within others or polygons with holes in the middle.
325+
When running `getData()`, the user can specify a well-known text (WKT)
326+
representation of geometry to restrict the geographic location of the returned
327+
occurrence records. The rgbif::occ_search function that `getData()` calls
328+
requires a counter-clockwise winding order for WKT. I find it helpful to think
329+
about WKT polygons in this way: imagine a square around your geographic area of
330+
interest and pick one of the corners as a starting point. The order of points
331+
in WKT format should follow counter-clockwise from the corner you picked first,
332+
and the final entry in the WKT string needs to be the same as the first entry.
333+
Additionally, while GPS points are typically represented in "latitude,
334+
longitude" format, WKT expects them in "longitude latitude" format with commas
335+
separating the points rather than individual longitude and latitude values.
336+
WKT polygons can have more specified points than included in this simple square
337+
example, and even include polygons nested within others or polygons with holes
338+
in the middle.
281339

282340
#### Literature Cited
283341

man/create_SAR.Rd

Lines changed: 12 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)