Skip to content

Commit

Permalink
Make get_aphia_id() a public function
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdesmet committed Dec 21, 2023
1 parent 0ac198c commit 02333a8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 24 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(add_resource)
export(datacite_to_eml)
export(get_aphia_id)
export(get_mvb_term)
export(write_dwc)
importFrom(dplyr,"%>%")
25 changes: 25 additions & 0 deletions R/get_aphia_id.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#' Get AphiaID from a taxonomic name
#'
#' This function wraps [worrms::wm_name2id_()] so that it returns a data frame
#' rather than a list.
#' It also silences "not found" warnings, returning `NA` instead
#'
#' @param x A (vector with) taxonomic name(s).
#' @return Data frame with `name` and `aphia_id`.
#' @family support functions
#' @importFrom dplyr %>%
#' @export
#' @examples
#' get_aphia_id("Mola mola")
#' get_aphia_id(c("Mola mola", "not_a_name"))
get_aphia_id <- function(x) {
result <- suppressWarnings(worrms::wm_name2id_(x))
taxa <- result %>%
purrr::discard(is.list) %>% # Remove x$message: "Not found" for e.g. "?"
dplyr::as_tibble() %>%
tidyr::pivot_longer(cols = dplyr::everything()) %>%
dplyr::rename(aphia_id = value)
# Join resulting taxa (with aphia_id) and input names to get df with all names
taxa %>%
dplyr::full_join(dplyr::as_tibble(x), by = c("name" = "value"))
}
23 changes: 0 additions & 23 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,3 @@ expand_cols <- function(df, colnames) {
df[, cols_to_add] <- NA_character_
return(df)
}

#' Get AphiaID from a taxonomic name
#'
#' This function wraps [worrms::wm_name2id_()] so that it returns a data frame
#' rather than a list.
#' It also silences "not found" warnings, returning `NA` instead
#'
#' @param x A (vector with) taxonomic name(s).
#' @return Data frame with `name` and `aphia_id`.
#' @family helper functions
#' @importFrom dplyr %>%
#' @noRd
get_aphia_id <- function(x) {
result <- suppressWarnings(worrms::wm_name2id_(x))
taxa <- result %>%
purrr::discard(is.list) %>% # Remove x$message: "Not found" for e.g. "?"
dplyr::as_tibble() %>%
tidyr::pivot_longer(cols = dplyr::everything()) %>%
dplyr::rename(aphia_id = value)
# Join resulting taxa (with aphia_id) and input names to get df with all names
taxa %>%
dplyr::full_join(dplyr::as_tibble(x), by = c("name" = "value"))
}
1 change: 1 addition & 0 deletions man/datacite_to_eml.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions man/get_aphia_id.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/get_mvb_term.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 02333a8

Please sign in to comment.