Skip to content

Commit

Permalink
First package functions and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
LimaRAF committed Jun 11, 2024
1 parent a2801b6 commit 1351c13
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 68 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ License: GPL (>= 2)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Imports:
dplyr,
stats
Suggests:
knitr,
rmarkdown,
Expand Down
6 changes: 5 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 0 additions & 30 deletions R/fun-demo.R

This file was deleted.

62 changes: 62 additions & 0 deletions R/sts_get_metadata.R
Original file line number Diff line number Diff line change
@@ -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)
}
}
62 changes: 62 additions & 0 deletions R/sts_get_sads.R
Original file line number Diff line number Diff line change
@@ -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)
}
27 changes: 0 additions & 27 deletions man/print_msg.Rd

This file was deleted.

22 changes: 22 additions & 0 deletions man/sts_get_metadata.Rd

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

49 changes: 49 additions & 0 deletions man/sts_get_sads.Rd

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

10 changes: 0 additions & 10 deletions tests/testthat/test-demo.R
Original file line number Diff line number Diff line change
@@ -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.")
})

0 comments on commit 1351c13

Please sign in to comment.