Commit d5bbe3e0 authored by Lars Dalby's avatar Lars Dalby
Browse files

Merge branch 'development' into 'main'

Development

See merge request !20
parents 7bada8ee d6391e9b
Loading
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
Package: camalienr
Title: Functions Used in the CamAlien Biodiversa+ Project
Version: 0.4.0
Title: Functions Used in the Plant Module of the Biodiversa+ Pilot on Invasive Alien Species
Version: 0.4.2
Authors@R: 
    person("Lars", "Dalby", , "lars.dalby@ecos.au.dk", role = c("aut", "cre"),
           comment = c(ORCID = "0000-0002-7270-6999"))
Description: What the package does (one paragraph).
Description: Tools used in the work with the vehicle mounted CamAlien system for monitoring plants. The package provides functions to interact with the CamAlien database.  
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
+8 −0
Original line number Diff line number Diff line
# camalienr 0.4.2

* Update the `ias` dataset with names from [GBIF](http://gbif.org)

# camalienr 0.4.1

* Update desc.

# camalienr 0.4.0

* Version bump after publication on r-universe.dev.
+8 −2
Original line number Diff line number Diff line
@@ -2,14 +2,20 @@
#'
#' Each partner has their own list of invasive alien species
#' which they targeted during the field campaign. The same species
#' can be on the list of multiple partners.
#' can be on the list of multiple partners. Target species can be
#' an entire genus. The scientific and the canonical name are from
#' a lookup on [GBIF](http://gbif.org).
#'
#' @format Data.frame with gbifid and name
#' \describe{
#'   \item{partner}{chr, The partner name}
#'   \item{canonical_name}{chr, The canonical name from GBIF with the modification that for
#' genera we add .sp}
#'   \item{scientificname}{chr, The scientific taxon name}
#'   \item{gbifid}{int, The gbifid used by Plantnet and in the camalien database}
#'   \item{pnid}{int, The ID used by Plantnet (hence pnid) and in the camalien database}
#'   \item{scientificname}{chr, The scientific binomial species name}
#'   \item{taxon_name_key}{chr, The GBIF key for the taxon name}
#'   \item{taxon_rank}{chr, The taxon rank, either species or genus. Derived from GBIF rankMarker}
#' }
"ias"

+65 −6
Original line number Diff line number Diff line
@@ -2,19 +2,70 @@
library(dplyr, warn.conflicts = FALSE)
library(camalienr)
library(googledrive)
library(rgbif)
library(purrr)

tmp <- tempfile()
drive_get("Updated list of species") |>
  drive_download(path = tmp)
readxl::read_xlsx(fs::path(glue::glue("{tmp}.xlsx")),
                  col_names = c("partner_acronym", "type", "scientificname", "gbifid", "comments"),
                  skip = 1) |>
readxl::read_xlsx(
  fs::path(glue::glue("{tmp}.xlsx")),
  col_names = c(
    "partner_acronym",
    "type",
    "scientificname",
    "gbifid",
    "comments"
  ),
  skip = 1
) |>
  filter(type == "Plant") |>
  select(-any_of(c("type", "comments"))) |>
  select(-any_of(c("type", "comments", "scientificname"))) |>
  mutate(gbifid = as.integer(gbifid)) -> ias_sp

# Get the accepted name from GBIF ----------------------------------------

map_dfr(unique(ias_sp$gbifid), function(id) {
  res <- name_usage(id, data = "name")$data
  if (!is.null(res)) {
    tibble(
      gbifid = id,
      taxon_name_key = res$key,
      scientificname = res$scientificName,
      canonical_name = res$canonicalName,
      rankMarker = res$rankMarker
    )
  } else {
    tibble(
      gbifid = id,
      taxon_name_key = NA,
      scientificname = NA,
      canonical_name = NA,
      rankMarker = NA
    )
  }
}) |>
  dplyr::mutate(
    taxonomic_rank = if_else(rankMarker == "gen.", "genus", "species"),
    canonical_name = if_else(
      rankMarker == "gen.",
      glue::glue("{canonical_name} sp."),
      canonical_name
    ),
    .keep = "unused"
  ) |>
  left_join(x = ias_sp, y = _, by = "gbifid") -> ias_sp

inner_join(partners, ias_sp, by = "partner_acronym") |>
  select(partner, scientificname, gbifid) -> ias
  select(
    partner,
    canonical_name,
    scientificname,
    gbifid,
    taxon_name_key,
    taxonomic_rank
  ) |>
  mutate(canonical_name = as.character(canonical_name)) -> ias

ids <- ias$gbifid

@@ -25,7 +76,15 @@ con |>
  filter(gbifid %in% ids) |>
  collect() |>
  right_join(ias, by = "gbifid", relationship = "many-to-many") |>
  select(partner, scientificname, gbifid, pnid) |>
  select(
    partner,
    canonical_name,
    scientificname,
    gbifid,
    pnid,
    taxon_name_key,
    taxonomic_rank
  ) |>
  arrange(partner, scientificname) |>
  # We need a hack to avoid saving as int64 (will gives issue when loading later)
  mutate(across(where(is.integer), as.double)) |>
+1.9 KiB (4.86 KiB)

File changed.

No diff preview for this file type.

Loading