From 1351c13eb5effb77403ac07c7368e92f193e843f Mon Sep 17 00:00:00 2001 From: Renato Lima Date: Tue, 11 Jun 2024 11:40:37 -0300 Subject: [PATCH] First package functions and documentation --- DESCRIPTION | 3 ++ NAMESPACE | 6 +++- R/fun-demo.R | 30 ------------------ R/sts_get_metadata.R | 62 ++++++++++++++++++++++++++++++++++++++ R/sts_get_sads.R | 62 ++++++++++++++++++++++++++++++++++++++ man/print_msg.Rd | 27 ----------------- man/sts_get_metadata.Rd | 22 ++++++++++++++ man/sts_get_sads.Rd | 49 ++++++++++++++++++++++++++++++ tests/testthat/test-demo.R | 10 ------ 9 files changed, 203 insertions(+), 68 deletions(-) delete mode 100644 R/fun-demo.R create mode 100644 R/sts_get_metadata.R create mode 100644 R/sts_get_sads.R delete mode 100644 man/print_msg.Rd create mode 100644 man/sts_get_metadata.Rd create mode 100644 man/sts_get_sads.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 131f08a..25c9814 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,6 +16,9 @@ License: GPL (>= 2) Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.1 +Imports: + dplyr, + stats Suggests: knitr, rmarkdown, diff --git a/NAMESPACE b/NAMESPACE index ba08b14..c4b5daf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,3 +1,7 @@ # Generated by roxygen2: do not edit by hand -export(print_msg) +export(sts_get_metadata) +export(sts_get_sads) +importFrom(dplyr,left_join) +importFrom(stats,aggregate) +importFrom(stats,reshape) diff --git a/R/fun-demo.R b/R/fun-demo.R deleted file mode 100644 index 8f1fc75..0000000 --- a/R/fun-demo.R +++ /dev/null @@ -1,30 +0,0 @@ -#' Print a message -#' -#' @description -#' This function prints a simple message. This is a demo function to show good -#' practices in writing and documenting R function. If you delete this function -#' do not forget to delete the corresponding test file -#' `tests/testthat/test-demo.R` if you used `new_package(test = TRUE)`. -#' -#' @param x a character of length 1. Default is `'Hello world'`. -#' -#' @return The value of `x` (only if the result is assigned to a variable). -#' -#' @export -#' -#' @examples -#' print_msg() -#' print_msg("Bonjour le monde") -#' -#' x <- print_msg("Bonjour le monde") -#' x - -print_msg <- function(x = "Hello world") { - - if (!is.character(x) || length(x) != 1) - stop("Argument 'x' must be a character of length 1.") - - cat(x, "\n") - - invisible(x) -} diff --git a/R/sts_get_metadata.R b/R/sts_get_metadata.R new file mode 100644 index 0000000..8bba4bc --- /dev/null +++ b/R/sts_get_metadata.R @@ -0,0 +1,62 @@ +#' @title Prepare Data For Analyses +#' +#' @description Simple function to get species abundances and plot +#' metadata together for analyses +#' +#' @param x the SynTreeSys database +#' @param output character. Should the plot metadat be returned (i.e. +#' 'Plots') or the the species table (i.e. 'SpeciesObservations') +#' with the plot metadata? Defaults to 'SpeciesObservations'. +#' +#' @return A data frame +#' +#' @importFrom dplyr left_join +#' +#' @export +#' +sts_get_metadata <- function(x, output = "SpeciesObservations") { + + ## Isolating the tables + plots <- x$Plots + comms <- x$CommunityObservations + spps <- x$SpeciesObservations + + ## Checking table contents + plots_plots <- unique(plots$uniquePlotID) + plots_comms <- unique(comms$uniquePlotID) + plots_spps <- unique(spps$uniquePlotID) + + check1 <- setdiff(plots_plots, plots_comms) + if (length(check1) > 0L) { + warning("One or more plots in the CommunityObservations are not in the Plots table. Removing them from the CommunityObservations table") + comms <- comms[!comms$uniquePlotID %in% check1, ] + } + + check2 <- setdiff(plots_plots, plots_spps) + if (length(check2) > 0L) { + warning("One or more plots in the SpeciesObservations are not in the Plots table. Removing them from the Plots table") + plots <- plots[!plots$uniquePlotID %in% check2, ] + } + + ## Checking for duplicated entries + dup_comms_plot <- comms$uniquePlotID[duplicated(comms$uniquePlotID)] + if (length(dup_comms_plot) > 0L){ + warning("One or more plots in CommunityObservations are duplicated. Removing: ", + paste(dup_comms_plot,collapse=", ")) + comms <- comms[!duplicated(comms$uniquePlotID),] + } + + ## Getting the necessary descriptions for the species observations + dados1 <- dplyr::left_join(plots, comms, + by = "uniquePlotID") + + if (output == "Plots") { + return(dados1) + } + + if (output == "SpeciesObservations") { + spps1 <- dplyr::left_join(spps, dados1, + by = "uniquePlotID") + return(spps1) + } +} \ No newline at end of file diff --git a/R/sts_get_sads.R b/R/sts_get_sads.R new file mode 100644 index 0000000..70c464d --- /dev/null +++ b/R/sts_get_sads.R @@ -0,0 +1,62 @@ +#' @title Get the Species Abundances Distributions per Group +#' +#' @param x the SynTreeSys database +#' @param minN numerical. The minimum number of individuals per plot. +#' Defaults to zero. +#' @param maxN numerical. The maximum number of individuals per plot. +#' Defaults to Inf. +#' @param minCutOff numerical. The minimum size criteria cutoff of the +#' plot (in mm). Defaults to zero. +#' @param group.name character. The STS column name with the categorie +#' used for grouping abundances. +#' @param spp.name character. The STS column name with the +#' (morpho)species name. Defaults to "organismNameCurated". +#' @param remove.indet logical. Should morphospecies not identified at +#' the species level be removed? Defaults to TRUE +#' @param abund.metric character. The STS column name with the +#' abundance metric to be used to build the SAD. Defaults to +#' "countsMeasurement" +#' @param strata.names vector of characters. The names of the strata +#' the should be kept in the analyses. +#' +#' +#' @importFrom stats aggregate reshape +#' +#' +#' @export +#' +sts_get_sads <- function(x, + minN = 0, + maxN = Inf, + minCutOff = 0, + group.name = NULL, + spp.name = "organismNameCurated", + remove.indet = TRUE, + abund.metric = "countsMeasurement", + strata.names = NULL) { + + ids2keep <- x$individuals >= minN & x$sizeCutoffMin >= minCutOff + plot2keep <- x$uniquePlotID[ids2keep] + x1 <- x[x$uniquePlotID %in% plot2keep,] + + if (remove.indet) + x1 <- x1[x1$organismNameTaxonRank %in% "species", ] + + if(!is.null(strata.names)){ + ## TO BE FINSHED ## + } + + sad.per.group <- stats::aggregate(x1[[abund.metric]], + list(x1[[spp.name]], x1[[group.name]]), + sum, na.rm = TRUE) + names(sad.per.group) <- c(spp.name, group.name, abund.metric) + sad.per.group1 <- stats::reshape(sad.per.group, direction = "wide", + idvar = spp.name, + timevar = group.name, + v.names = abund.metric) + colnames(sad.per.group1) <- gsub("countsMeasurement.", "", + colnames(sad.per.group1)) + sad.per.group1[is.na(sad.per.group1)] <- 0 + + return(sad.per.group1) +} \ No newline at end of file diff --git a/man/print_msg.Rd b/man/print_msg.Rd deleted file mode 100644 index e2ebf01..0000000 --- a/man/print_msg.Rd +++ /dev/null @@ -1,27 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/fun-demo.R -\name{print_msg} -\alias{print_msg} -\title{Print a message} -\usage{ -print_msg(x = "Hello world") -} -\arguments{ -\item{x}{a character of length 1. Default is \code{'Hello world'}.} -} -\value{ -The value of \code{x} (only if the result is assigned to a variable). -} -\description{ -This function prints a simple message. This is a demo function to show good -practices in writing and documenting R function. If you delete this function -do not forget to delete the corresponding test file -\code{tests/testthat/test-demo.R} if you used \code{new_package(test = TRUE)}. -} -\examples{ -print_msg() -print_msg("Bonjour le monde") - -x <- print_msg("Bonjour le monde") -x -} diff --git a/man/sts_get_metadata.Rd b/man/sts_get_metadata.Rd new file mode 100644 index 0000000..fef4bb0 --- /dev/null +++ b/man/sts_get_metadata.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/sts_get_metadata.R +\name{sts_get_metadata} +\alias{sts_get_metadata} +\title{Prepare Data For Analyses} +\usage{ +sts_get_metadata(x, output = "SpeciesObservations") +} +\arguments{ +\item{x}{the SynTreeSys database} + +\item{output}{character. Should the plot metadat be returned (i.e. +'Plots') or the the species table (i.e. 'SpeciesObservations') +with the plot metadata? Defaults to 'SpeciesObservations'.} +} +\value{ +A data frame +} +\description{ +Simple function to get species abundances and plot +metadata together for analyses +} diff --git a/man/sts_get_sads.Rd b/man/sts_get_sads.Rd new file mode 100644 index 0000000..f23faa0 --- /dev/null +++ b/man/sts_get_sads.Rd @@ -0,0 +1,49 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/sts_get_sads.R +\name{sts_get_sads} +\alias{sts_get_sads} +\title{Get the Species Abundances Distributions per Group} +\usage{ +sts_get_sads( + x, + minN = 0, + maxN = Inf, + minCutOff = 0, + group.name = NULL, + spp.name = "organismNameCurated", + remove.indet = TRUE, + abund.metric = "countsMeasurement", + strata.names = NULL +) +} +\arguments{ +\item{x}{the SynTreeSys database} + +\item{minN}{numerical. The minimum number of individuals per plot. +Defaults to zero.} + +\item{maxN}{numerical. The maximum number of individuals per plot. +Defaults to Inf.} + +\item{minCutOff}{numerical. The minimum size criteria cutoff of the +plot (in mm). Defaults to zero.} + +\item{group.name}{character. The STS column name with the categorie +used for grouping abundances.} + +\item{spp.name}{character. The STS column name with the +(morpho)species name. Defaults to "organismNameCurated".} + +\item{remove.indet}{logical. Should morphospecies not identified at +the species level be removed? Defaults to TRUE} + +\item{abund.metric}{character. The STS column name with the +abundance metric to be used to build the SAD. Defaults to +"countsMeasurement"} + +\item{strata.names}{vector of characters. The names of the strata +the should be kept in the analyses.} +} +\description{ +Get the Species Abundances Distributions per Group +} diff --git a/tests/testthat/test-demo.R b/tests/testthat/test-demo.R index b09f267..c34dc6c 100644 --- a/tests/testthat/test-demo.R +++ b/tests/testthat/test-demo.R @@ -1,14 +1,4 @@ ## Test demo ---- test_that("Test demo", { - expect_invisible(print_msg()) - - x <- print_msg() - expect_equal(x, "Hello world") - - x <- print_msg("Bonjour le monde") - expect_equal(x, "Bonjour le monde") - - expect_error(print_msg(c("Hello", "world"))) - expect_error(print_msg(1), "Argument 'x' must be a character of length 1.") })