Skip to content

Commit

Permalink
Merge branch 'main' into remove-base-declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
damianooldoni committed Jul 17, 2024
2 parents 886230d + 5e293f2 commit 1d354a9
Show file tree
Hide file tree
Showing 26 changed files with 146 additions and 125 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: trias
Title: Process Data for the Project Tracking Invasive Alien Species
(TrIAS)
Version: 2.3.0
Version: 2.3.1
Authors@R: c(
person("Damiano", "Oldoni", email = "damiano.oldoni@inbo.be",
role = c("aut", "cre"), comment = c(ORCID = "0000-0003-3445-7562")),
Expand Down Expand Up @@ -68,4 +68,4 @@ Encoding: UTF-8
LazyData: true
LazyDataCompression: bzip2
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
13 changes: 5 additions & 8 deletions R/apply_decision_rules.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,17 @@
#' - (`dr_1` is `TRUE` and `dr_3` is `FALSE`) or (`dr_1`, `dr_2` and `dr_3` are
#' `FALSE`): `em = 1` (unclear)
#' @examples
#' \dontrun{
#' df <- dplyr::tibble(
#' taxonID = c(rep(1008955, 10), rep(2493598, 3)),
#' y = c(seq(2009, 2018), seq(2016, 2018)),
#' obs = c(1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 3, 0)
#' )
#' apply_decision_rules(df,
#' eval_year = c(2016, 2017, 2018),
#' eval_year = 2016,
#' y_var = "obs",
#' taxonKey = "taxonID",
#' year = y
#' year = "y"
#' )
#' }
#'
apply_decision_rules <- function(df,
y_var = "ncells",
eval_year,
Expand Down Expand Up @@ -173,7 +170,7 @@ apply_decision_rules <- function(df,
# Get all taxa in df
taxon_keys <-
df %>%
distinct(!!rlang::sym(taxonKey)) %>%
dplyr::distinct(!!rlang::sym(taxonKey)) %>%
dplyr::pull()

# Find taxa whose timeseries don't contain eval_year, remove them and throw a
Expand Down Expand Up @@ -214,7 +211,7 @@ apply_decision_rules <- function(df,
dplyr::filter(!!rlang::sym(y_var) > 0) %>%
dplyr::add_tally(wt = NULL) %>%
dplyr::mutate(dr_1 = n == 1) %>%
distinct(!!rlang::sym(taxonKey), dr_1)
dplyr::distinct(!!rlang::sym(taxonKey), dr_1)

# Rule 2: last value (at eval_year) above median value?
dr_2 <-
Expand Down Expand Up @@ -300,7 +297,7 @@ apply_decision_rules <- function(df,

em_dr <-
em_dr %>%
bind_rows(taxa_without_em)
dplyr::bind_rows(taxa_without_em)

return(em_dr)
}
4 changes: 2 additions & 2 deletions R/apply_gam.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#' @param x_label character. x-axis label of output plot. Default: `"year"`.
#' @param y_label character. y-axis label of output plot. Default: `"number of
#' observations"`.
#' @param saveplot logical. If `TRUE` the plots are authomatically saved.
#' @param saveplot logical. If `TRUE` the plots are automatically saved.
#' Default: `FALSE`.
#' @param dir_name character. Path of directory where saving plots. If path
#' doesn't exists, directory will be created. Example: "./output/graphs/". If
Expand Down Expand Up @@ -92,7 +92,7 @@
#'
#' \item `first_derivative`: df. Data.frame with details of first derivatives.
#' It contains the following columns:
#' - `smooth`: smoooth identifier. Example: `s(year)`.
#' - `smooth`: smooth identifier. Example: `s(year)`.
#' - `derivative`: numeric. Value of first derivative.
#' - `se`: numeric. Standard error of `derivative`.
#' - `crit`: numeric. Critical value required such that
Expand Down
2 changes: 1 addition & 1 deletion R/climate_match.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#' zones in the target nation or region
#' - `spatial` a sf object containing the observations used
#' in the analysis
#' - `current_map` a leaflet object displaying the degree of wordlwide
#' - `current_map` a leaflet object displaying the degree of worldwide
#' climate match with the climate from 1980 till 2016
#' - `future_maps` a list of leaflet objects for each future climate
#' scenario, displaying the degree of climate match
Expand Down
6 changes: 3 additions & 3 deletions R/data-legends.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#'
#' Each data.frame contains two columns:
#' - `GRIDCODE`: (numeric) grid value corresponding to a climate zone
#' - `Classification`: (character) Koppen-{G}eiger climate classification value
#' - `Description`: (character) verbose description of the Koppen-{G}eiger
#' - `Classification`: (character) Koppen-Geiger climate classification value
#' - `Description`: (character) verbose description of the Koppen-Geiger
#' climate zone, e.g. "Tropical rainforest climate"
#' - `Group`: (character) group the Koppen-{G}eiger climate zone belongs to,
#' - `Group`: (character) group the Koppen-Geiger climate zone belongs to,
#' e.g. "Tropical"
#' - `Precipitation Type`: (character) Type of precipitations associated to
#' the climate zone, e.g. "Rainforest"
Expand Down
2 changes: 1 addition & 1 deletion R/gbif_has_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @param taxon_key (single numeric or character) a single taxon key.
#' @param ... one or more GBIF distribution properties and related values.
#' Up to now it supports the following properties:
#' country (and its synoynym: countryCode), status (and its synonym:
#' country (and its synonym: countryCode), status (and its synonym:
#' occurrenceStatus) and establishmentMeans.
#' @return a logical, TRUE or FALSE.
#' @export
Expand Down
42 changes: 20 additions & 22 deletions R/gbif_verify_keys.R
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
#' Check keys against GBIF Backbone Taxonomy
#'
#' This function performs three checks:
#' \itemize{
#' \item{\code{keys} are valid GBIF taxon keys. That means that adding a key
#' - `keys` are valid GBIF taxon keys. That means that adding a key
#' at the end of the URL https://www.gbif.org/species/ returns a GBIF page
#' related to a taxa.}
#' \item{\code{keys} are taxon keys of the GBIF Backbone Taxonomy checklist.
#' related to a taxa.
#' - `keys` are taxon keys of the GBIF Backbone Taxonomy checklist.
#' That means that adding a key at the end of the URL
#' https://www.gbif.org/species/ returns a GBIF page related to a taxa of the
#' GBIF Backbone.)}
#' \item{\code{keys} are synonyms of other taxa (taxonomicStauts neither
#' \code{ACCEPTED} nor \code{DOUBTFUL}).}
#' }.
#' GBIF Backbone.)
#' - `keys` are synonyms of other taxa (taxonomicStatus neither
#' `ACCEPTED` nor `DOUBTFUL`).
#' @param keys (character or numeric) a vector, a list, or a data.frame
#' containing the keys to verify.
#' @param col_keys (character) name of column containing keys in case
#' \code{keys} is a data.frame.
#' `keys` is a data.frame.
#'
#' @return a data.frame with the following columns:
#' \itemize{
#' \item{\code{key}}{:(numeric) keys as input keys.}
#' \item{\code{is_taxonKey}} {: (logical) is the key a valid GBIF taxon
#' key?}
#' \item{\code{is_from_gbif_backbone}} {:(logical) is the key a valid taxon
#' key from GBIF Backbone Taxonomy checklist?}
#' \item{\code{is_synonym}} {: (logical) is the key related to a synonym (not
#' \code{ACCEPTED} or \code{DOUBTFUL})?}
#' }
#' If a key didn't pass the first check (\code{is_taxonKey = FALSE}) then
#' \code{NA} for other two columns. If a key didn't pass the second check
#' (\code{is_from_gbif_backbone = FALSE}) then \code{is_synonym} = \code{NA}.
#' - `key`: (numeric) keys as input keys.
#' - `is_taxonKey`: (logical) is the key a valid GBIF taxon key?
#' - `is_from_gbif_backbone`: (logical) is the key a valid taxon key from
#' GBIF Backbone Taxonomy checklist?
#' - `is_synonym`: (logical) is the key related to a synonym (not
#' `ACCEPTED` or `DOUBTFUL`)?
#'
#' If a key didn't pass the first check (`is_taxonKey` = `FALSE`) then
#' `NA` for other two columns. If a key didn't pass the second check
#' (`is_from_gbif_backbone` = `FALSE`) then `is_synonym` = `NA`.
#'
#' @export
#' @importFrom dplyr .data %>%
#' @importFrom rlang !!
#' @examples
#' \dontrun{
#' # input is a vector
#' keys1 <- c(
#' "12323785387253", # invalid GBIF taxonKey
Expand All @@ -50,7 +48,6 @@
#' )
#' # input is a named list
#' keys3 <- keys1
#' library(purrr)
#' names(keys3) <- purrr::map_chr(
#' c(1:length(keys3)),
#' ~ paste(sample(c(0:9, letters, LETTERS), 3),
Expand All @@ -64,6 +61,7 @@
#' gbif_verify_keys(keys2, col_keys = "keys")
#' gbif_verify_keys(keys3)
#' gbif_verify_keys(keys4)
#' }
gbif_verify_keys <- function(keys, col_keys = "key") {
assertthat::assert_that(is.data.frame(keys) | is.vector(keys),
msg = "keys should be a vector, a named list or a data.frame."
Expand Down
28 changes: 16 additions & 12 deletions R/indicator_native_range_year.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' [countYearProvince](https://github.com/inbo/reporting-rshiny-grofwildjacht/blob/exoten/reporting-grofwild/R/countYearProvince.R)
#' plot from reporting - rshiny - grofwildjacht
#' @param df input data.frame.
#' @param years (numeric) vector years we are interested to. If \code{NULL}
#' @param years (numeric) vector years we are interested to. If `NULL`
#' (default) all years from minimum and maximum of years of first observation
#' are taken into account.
#' @param type character, native_range level of interest should be one of
Expand All @@ -16,22 +16,26 @@
#' species".
#' @param relative (logical) if TRUE (default), each bar is standardised before
#' stacking.
#' @param taxon_key_col character. Name of the column of \code{df} containing
#' taxon IDs. Default: \code{"key"}.
#' @param taxon_key_col character. Name of the column of `df` containing
#' taxon IDs. Default: `"key"`.
#' @param first_observed (character) Name of the column in `data`
#' containing temporal information about introduction of the alien species.
#' Expressed as years.
#' @return list with: \itemize{ \item{'static_plot': }{ggplot object, for a
#' @return list with:
#' - `static_plot`: ggplot object, for a
#' given species the observed number per year and per native range is plotted
#' in a stacked bar chart} \item{'interactive_plot': }{plotly object, for a
#' in a stacked bar chart.
#' - `interactive_plot`: plotly object, for a
#' given species the observed number per year and per native range is plotted
#' in a stacked bar chart} \item{'data': }{data displayed in the plot, as
#' data.frame with: \itemize{ \item{'year': }{year at which the species were
#' introduced} \item{'native_range': }{native range of the introduced species}
#' \item{'n': }{number of species introduced from the native range for a given
#' year} \item{'total': }{total number of species, from all around the world,
#' introduced during a given year} \item{'perc': }{percentage of species
#' introduced from the native range for a given year. (n/total)*100} } } }
#' in a stacked bar chart.
#' - `data`: data displayed in the plot, as a data.frame with:
#' - `year`: year at which the species were introduced.
#' - `native_range`: native range of the introduced species.
#' - `n`: number of species introduced from the native range for a given year.
#' - `total`: total number of species, from all around the world, introduced.
#' during a given year.
#' - `perc`: percentage of species introduced from the native range for a
#' given year, `n`/`total`*100.
#' @export
#' @importFrom dplyr %>% .data
#' @examples
Expand Down
2 changes: 1 addition & 1 deletion R/pathways_cbd.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Pathways of introdcution as defined by CBD
#' Pathways of introduction as defined by CBD
#'
#' Function to get all CBD pathays of introdution at level 1 (`pathway_level1`)
#' and level 2 (`pathway_level2`). Added pathway `unknown` at level 1 and level
Expand Down
2 changes: 1 addition & 1 deletion R/update_download_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' This function opens a (tab-separated) text file containing all occurrence
#' downloads from GBIF and updates the status of all downloads with status
#' `RUNNING` or `PREPARING`. If the specifid download is not present it will be add.
#' `RUNNING` or `PREPARING`. If the specified download is not present it will be add.
#'
#' If a download key is passed which is not present in the file it will be added
#' as a new line.
Expand Down
8 changes: 4 additions & 4 deletions R/verify_taxa.R
Original file line number Diff line number Diff line change
Expand Up @@ -666,12 +666,12 @@ verify_taxa <- function(taxa,
assertthat::assert_that(
taxa %>%
dplyr::filter(is.na(.data$bb_key)) %>%
dplyr::filter_at(dplyr::vars(starts_with("bb_")),
dplyr::filter_at(dplyr::vars(dplyr::starts_with("bb_")),
dplyr::all_vars(is.na(.))) %>%
nrow() ==
taxa %>%
dplyr::filter(is.na(.data$bb_key)) %>%
dplyr::filter_at(dplyr::vars(starts_with("bb_")),
dplyr::filter_at(dplyr::vars(dplyr::starts_with("bb_")),
dplyr::any_vars(is.na(.))) %>%
nrow(),
msg = "Columns with GBIF Backbone info should be empty for unmatched taxa."
Expand Down Expand Up @@ -1212,7 +1212,7 @@ verify_taxa <- function(taxa,
.data$bb_scientificName,
.data$n
) %>%
dplyr::arrange(desc(.data$n)) %>%
dplyr::arrange(dplyr::desc(.data$n)) %>%
dplyr::ungroup()
} else {
duplicates <- dplyr::tibble(
Expand All @@ -1237,7 +1237,7 @@ verify_taxa <- function(taxa,
# Order verification by outdated and dateAdded
verification <-
verification %>%
dplyr::arrange(.data$outdated, desc(.data$dateAdded))
dplyr::arrange(.data$outdated, dplyr::desc(.data$dateAdded))

# Add not outdated taxa from verification to not_to_verify_taxa
taxa <-
Expand Down
14 changes: 9 additions & 5 deletions R/visualize_pathways_level1.R
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,22 @@ visualize_pathways_level1 <- function(df,
# rename to default column name
df <-
df %>%
dplyr::rename_at(vars(tidyselect::all_of(kingdom_names)), ~"group") %>%
dplyr::rename_at(vars(tidyselect::all_of(taxon_names)), ~"taxonKey") %>%
dplyr::rename_at(vars(tidyselect::all_of(first_observed)), ~"first_observed") %>%
dplyr::rename_at(vars(tidyselect::all_of(pathway_level1_names)), ~"pathway_level1")
dplyr::rename_at(dplyr::vars(tidyselect::all_of(kingdom_names)),
~"group") %>%
dplyr::rename_at(dplyr::vars(tidyselect::all_of(taxon_names)),
~"taxonKey") %>%
dplyr::rename_at(dplyr::vars(tidyselect::all_of(first_observed)),
~"first_observed") %>%
dplyr::rename_at(dplyr::vars(tidyselect::all_of(pathway_level1_names)),
~"pathway_level1")
# handle asymmetric category system (Chordata, Not Chordta are not kingdoms)
if (!is.null(category)) {
if (!category %in% c("Chordata", "Not Chordata")) {
df <- df %>% dplyr::filter(.data$group == category)
} else {
df <-
df %>%
dplyr::rename_at(vars(phylum_names), ~"phylum_group")
dplyr::rename_at(dplyr::vars(phylum_names), ~"phylum_group")
if (category == "Chordata") {
df <- df %>% dplyr::filter(.data$phylum_group == category)
} else {
Expand Down
17 changes: 11 additions & 6 deletions R/visualize_pathways_level2.R
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,16 @@ visualize_pathways_level2 <- function(df,
# rename to default column name
df <-
df %>%
dplyr::rename_at(vars(tidyselect::all_of(kingdom_names)), ~"group") %>%
dplyr::rename_at(vars(tidyselect::all_of(taxon_names)), ~"taxonKey") %>%
dplyr::rename_at(vars(tidyselect::all_of(first_observed)), ~"first_observed") %>%
dplyr::rename_at(vars(tidyselect::all_of(pathway_level1_names)), ~"pathway_level1") %>%
dplyr::rename_at(vars(tidyselect::all_of(pathway_level2_names)), ~"pathway_level2")
dplyr::rename_at(dplyr::vars(tidyselect::all_of(kingdom_names)),
~"group") %>%
dplyr::rename_at(dplyr::vars(tidyselect::all_of(taxon_names)),
~"taxonKey") %>%
dplyr::rename_at(dplyr::vars(tidyselect::all_of(first_observed)),
~"first_observed") %>%
dplyr::rename_at(dplyr::vars(tidyselect::all_of(pathway_level1_names)),
~"pathway_level1") %>%
dplyr::rename_at(dplyr::vars(tidyselect::all_of(pathway_level2_names)),
~"pathway_level2")
# Select data with the chosen pathway level 1
df <-
df %>%
Expand All @@ -343,7 +348,7 @@ visualize_pathways_level2 <- function(df,
} else {
df <-
df %>%
dplyr::rename_at(vars(phylum_names), ~"phylum_group")
dplyr::rename_at(dplyr::vars(phylum_names), ~"phylum_group")
if (category == "Chordata") {
df <- df %>% dplyr::filter(.data$phylum_group == category)
} else {
Expand Down
12 changes: 7 additions & 5 deletions R/visualize_pathways_year_level1.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,12 @@ visualize_pathways_year_level1 <- function(
# rename to default column name
df <-
df %>%
dplyr::rename_at(vars(tidyselect::all_of(kingdom_names)), ~"group") %>%
dplyr::rename_at(vars(tidyselect::all_of(taxon_names)), ~"taxonKey") %>%
dplyr::rename_at(vars(tidyselect::all_of(first_observed)), ~"first_observed") %>%
dplyr::rename_at(vars(tidyselect::all_of(pathway_level1_names)), ~"pathway_level1")
dplyr::rename_at(dplyr::vars(dplyr::all_of(kingdom_names)), ~"group") %>%
dplyr::rename_at(dplyr::vars(dplyr::all_of(taxon_names)), ~"taxonKey") %>%
dplyr::rename_at(dplyr::vars(dplyr::all_of(first_observed)),
~"first_observed") %>%
dplyr::rename_at(dplyr::vars(dplyr::all_of(pathway_level1_names)),
~"pathway_level1")

# handle asymmetric category system (Chordata, Not Chordata are not kingdoms)
if (!is.null(category)) {
Expand All @@ -297,7 +299,7 @@ visualize_pathways_year_level1 <- function(
} else {
df <-
df %>%
dplyr::rename_at(vars(phylum_names), ~"phylum_group")
dplyr::rename_at(dplyr::vars(phylum_names), ~"phylum_group")
if (category == "Chordata") {
df <- df %>% dplyr::filter(.data$phylum_group == category)
} else {
Expand Down
Loading

0 comments on commit 1d354a9

Please sign in to comment.