-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First package functions and documentation
- Loading branch information
Showing
9 changed files
with
203 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
}) |