R/prepTaxa.R

Defines functions prepTaxa

Documented in prepTaxa

#' @title prepTaxa: reshapes plant taxa lookup table
#'
#' @importFrom dplyr select
#' @importFrom magrittr %>%
#' @importFrom tidyr pivot_wider
#'
#' @description This function reshapes the plant taxa lookup table to match the tlu_Plants table in the original database
#' to make filtering and summarizing easier. Function is mostly internal and used for quadrat species, seedling and sapling summaries.
#'
#' @return Returns a dataframe with cover class midpoints for each quadrat and includes guild for each species.
#'
#' @examples
#' \dontrun{
#' importData()
#' # reshape the plant taxa lookup to wide
#' taxa_wide <- prepTaxa()
#'}
#'
#'
#' @export
#'

prepTaxa <- function(){

  env <- if(exists("VIEWS_NETN")){VIEWS_NETN} else {.GlobalEnv}

  tryCatch(taxa <- get("Taxa_NETN", envir = env) %>%
             select(TaxonID, TSN, ScientificName, CommonName, Order, Family,
                    Genus, Species, SubSpecies, IsExotic, InvasiveNETN, IsCanopyExclusion, IsFernAlly,
                    TaxonGroupLabel, DeerIndicatorTree, DeerIndicatorHerb), # add FilterMIDN for MIDN
           error = function(e){stop("COMN_Taxa view not found. Please import view.")})

  # Clean up taxa table so easier to work with
  names(taxa)[names(taxa) == "IsFernAlly"] <- "FernAlly"
  names(taxa)[names(taxa) == "IsExotic"] <- "Exotic"
  names(taxa)[names(taxa) == "IsCanopyExclusion"] <- "CanopyExclusion"

  cols <- c("Order", "Family", "Genus", "Species", "SubSpecies")
  taxa[, cols] <- invisible(lapply(taxa[, cols], gsub, pattern = "NOT DETERMINED", replacement = NA))
  taxa$CommonName <- sub(",.*", "", taxa$CommonName)
  taxa$CommonName <- sub("/.*", "", taxa$CommonName)
  taxa$guild <- 1
  taxa$guild_text <- gsub("/", "", taxa$TaxonGroupLabel)

  # reshape guilds to wide
  taxa_wide <- taxa %>% select(-TaxonGroupLabel) %>%
    pivot_wider(names_from = guild_text,
                values_from = guild,
                values_fill = 0) %>%
    select(TaxonID, TSN, ScientificName, CommonName, Order, Family, Genus, Species, SubSpecies,
           Tree, TreeShrub, Shrub, Vine, Herbaceous, Graminoid, FernAlly, MossLichen,
           CanopyExclusion, Exotic, InvasiveNETN, DeerIndicatorTree, DeerIndicatorHerb)

  return(data.frame(taxa_wide))
}
KateMMiller/forestNETN documentation built on April 1, 2024, 11:49 p.m.