diff --git a/NAMESPACE b/NAMESPACE index 78a24c4f..38f686fc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ export(align_taxa) export(create_species_state_origin_matrix) export(create_taxonomic_update_lookup) export(default_version) +export(get_apc_genus_family_lookup) export(load_taxonomic_resources) export(native_anywhere_in_australia) export(standardise_names) diff --git a/NEWS.md b/NEWS.md index bc7e5cc2..4abf51d8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,11 @@ +# APCalign 1.0.2 + +Minor update to fix + +- Deal with the vignette issues that emerged on CRAN +- Improve "graceful failing", based on issues that have come up on github CI +- minor formatting + # APCalign 1.0.1 First major release of APCalign. A preprint is available at diff --git a/R/state_diversity_counts.R b/R/state_diversity_counts.R index 703dfd14..42cb1396 100644 --- a/R/state_diversity_counts.R +++ b/R/state_diversity_counts.R @@ -1,8 +1,8 @@ #' @title State- and territory-level diversity -#' +#' #' @description -#' For Australian states and territories, use geographic distribution data from -#' the APC to calculate state-level diversity for native, introduced, +#' For Australian states and territories, use geographic distribution data from +#' the APC to calculate state-level diversity for native, introduced, #' and more complicated species origins #' #' @family diversity methods @@ -26,6 +26,7 @@ #' #' @examples #' \donttest{state_diversity_counts(state = "NSW")} + state_diversity_counts <- function(state, resources = load_taxonomic_resources()) { @@ -77,14 +78,45 @@ state_diversity_counts <- function(state, #' @noRd -get_apc_genus_family_lookup <- - function(resources = load_taxonomic_resources()) { - apc_s <- dplyr::filter(resources$APC, - taxon_rank == "species") - dplyr::tibble(genus = word(apc_s$scientific_name, 1, 1), - family = apc_s$family) %>% +create_apc_genus_family_lookup <- + function(resources) { + apc_s <- dplyr::filter(resources$APC, taxon_rank == "species") + dplyr::tibble(genus = word(apc_s$accepted_name_usage, 1, 1), + family = apc_s$family) |> dplyr::distinct() -> lu return(lu) } - +#' @title Lookup Family by Genus from APC +#' +#' @description +#' Retrieve the family name for a given genus using taxonomic data from the +#' Australian Plant Census (APC). +#' +#' @param genus A character vector of genus names for which to retrieve the +#' corresponding family names. +#' @param resources The taxonomic resources required to make the lookup. +#' Loading this can be slow, so call \code{\link{load_taxonomic_resources}} +#' separately to speed up this function and pass the resources in. +#' +#' @return A data frame with two columns: "genus", indicating the genus name, +#' and "family", indicating the corresponding family name from the APC. +#' +#' @seealso \code{\link{load_taxonomic_resources}} +#' +#' @export +#' +#' @examples +#' \donttest{get_apc_genus_family_lookup(genus = c("Acacia", "Eucalyptus"))} +get_apc_genus_family_lookup <- + function(genus, resources = load_taxonomic_resources()) { + if (is.null(resources)) { + message("Not finding taxonomic resources; check internet connection?") + return(NULL) + } + fam_lu <- create_apc_genus_family_lookup(resources = resources) + lu <- dplyr::tibble(genus = genus) %>% + dplyr::left_join(fam_lu, by = "genus") + if (any(is.na(lu$family))) warning("some non-matches with the APC accepted genus list, check the formatting of your genus vector.") + return(lu) + } diff --git a/man/get_apc_genus_family_lookup.Rd b/man/get_apc_genus_family_lookup.Rd new file mode 100644 index 00000000..98f46f2e --- /dev/null +++ b/man/get_apc_genus_family_lookup.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/state_diversity_counts.R +\name{get_apc_genus_family_lookup} +\alias{get_apc_genus_family_lookup} +\title{Lookup Family by Genus from APC} +\usage{ +get_apc_genus_family_lookup(genus, resources = load_taxonomic_resources()) +} +\arguments{ +\item{genus}{A character vector of genus names for which to retrieve the +corresponding family names.} + +\item{resources}{The taxonomic resources required to make the lookup. +Loading this can be slow, so call \code{\link{load_taxonomic_resources}} +separately to speed up this function and pass the resources in.} +} +\value{ +A data frame with two columns: "genus", indicating the genus name, +and "family", indicating the corresponding family name from the APC. +} +\description{ +Retrieve the family name for a given genus using taxonomic data from the +Australian Plant Census (APC). +} +\examples{ + \donttest{get_apc_genus_family_lookup(genus = c("Acacia", "Eucalyptus"))} +} +\seealso{ +\code{\link{load_taxonomic_resources}} +} diff --git a/tests/testthat/benchmarks/family_check.csv b/tests/testthat/benchmarks/family_check.csv new file mode 100644 index 00000000..72a5e300 --- /dev/null +++ b/tests/testthat/benchmarks/family_check.csv @@ -0,0 +1,5 @@ +genus,family +Eucalyptus,Myrtaceae +Pinus,Pinaceae +Brassica,Brassicaceae +not a species,NA diff --git a/tests/testthat/test-state_diversity.R b/tests/testthat/test-state_diversity.R index b207f831..c22afee8 100644 --- a/tests/testthat/test-state_diversity.R +++ b/tests/testthat/test-state_diversity.R @@ -33,3 +33,22 @@ test_that("native_anywhere_in_australia() works", { expect_equal(native_check, previous_check) expect_warning(native_anywhere_in_australia(species = "NOTASPECIES", resources = resources)) }) + + +test_that("get_apc_genus_family_lookup() works", { + expect_warning(family_check <- + get_apc_genus_family_lookup( + c( + "Eucalyptus", + "Pinus", + "Brassica", + "not a species" + ), + resources = resources + )) + # readr::write_csv(family_check,"tests/testthat/benchmarks/family_check.csv") + previous_check <- readr::read_csv("benchmarks/family_check.csv", show_col_types = FALSE) + expect_equal(family_check, previous_check) +}) + +