Skip to content

Commit

Permalink
User input validation for bar_plot
Browse files Browse the repository at this point in the history
And some documentation improvements
  • Loading branch information
TheOafidian committed Aug 1, 2023
1 parent 09e89f1 commit 871c0cf
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 25 deletions.
12 changes: 12 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,16 @@ importFrom(rlang,as_label)
importFrom(rlang,as_name)
importFrom(rlang,enquo)
importFrom(rlang,enquos)
importFrom(stats,as.formula)
importFrom(stats,cmdscale)
importFrom(stats,cor.test)
importFrom(stats,cutree)
importFrom(stats,fisher.test)
importFrom(stats,hclust)
importFrom(stats,median)
importFrom(stats,na.omit)
importFrom(stats,p.adjust)
importFrom(stats,prcomp)
importFrom(stats,t.test)
importFrom(stats,wilcox.test)
importFrom(tibble,tibble)
4 changes: 3 additions & 1 deletion R/adders_samples.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ add_alphas <- function(ta) {
#' object. This function is extremely useful if one wants to plot similar
#' samples together.
#'
#' @importFrom stats hclust
#' @param ta tidytacos object.
#'
#' @examples
Expand Down Expand Up @@ -235,7 +236,7 @@ add_sample_clustered <- function(ta) {
#' the samples tibble of a tidytacos object named "pcoa1" and "pcoa2". This
#' function will also add relative abundances if not present using
#' \code{\link{add_rel_abundance}}.
#'
#' @importFrom stats cmdscale
#' @param ta tidytacos object.
#'
#' @examples
Expand Down Expand Up @@ -384,6 +385,7 @@ add_spike_ratio <- function(ta, spike_taxon) {
# Adds a variable "cluster" to the samples table
# To do: merge with add_sample_clustered somehow
#
#' @importFrom stats cutree
#' @export
add_cluster <- function(ta, n_clusters) {

Expand Down
10 changes: 9 additions & 1 deletion R/adders_taxa.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ add_total_rel_abundance <- function(ta) {


#' Create sensible names for the taxa and add to taxon table
#' @importFrom stats na.omit
#' @param ta a tidytacos object
#' @param method the method on which to arrange the taxon names.
#' Options: total_rel_abundance, max_rel_abundance
Expand Down Expand Up @@ -261,6 +262,7 @@ add_taxon_name <- function(
#' @param n integer denoting the amount of most abundant taxa to display. Capacity at 12.
#' @param samples optional vector of sample_id's of interest
#' @param taxa optional vector of taxon_id's of interest
#' @param rank optional rank to aggregate taxa on
#' @export
add_taxon_name_color <- function(
ta, method = "total_rel_abundance", n = 12, samples = NULL, taxa = NULL, rank = NULL
Expand Down Expand Up @@ -365,6 +367,7 @@ add_taxon_name_color <- function(
#' of Illumina MiSeq data,” Microbiome, vol. 3, no. 1, Art. no. 1, 2015, doi:
#' 10.1186/s40168-015-0083-8.
#'
#' @importFrom stats cor.test
#' @param ta A tidytacos object.
#' @param dna_conc A variable in the samples table that contains dna
#' concetrations (unquoted).
Expand Down Expand Up @@ -425,7 +428,11 @@ add_jervis_bardy <- function(ta, dna_conc, sample_condition = T, min_pres = 3) {
#'
#' Condition should be a categorical variable present in the samples table.
#' Supply condition as a string.
#'
#' @importFrom stats fisher.test
#' @param ta A tidytacos object
#' @param condition A categorical variable (string)
#' @param relative wether to use relative occurences
#' @param fischer_test wether to perform a fischer test and add the p-values of the test to the taxa table
#' @export
add_occurrences <- function(
ta, condition = NULL, relative = F, fischer_test = F
Expand Down Expand Up @@ -517,6 +524,7 @@ add_occurrences <- function(
#' by setting the `test` argument. Options are NULL (default), "wilcox" or
#' "t-test".
#'
#' @importFrom stats t.test wilcox.test
#' @param ta A tidytacos object
#' @param condition A condition variable (character)
#' @param test Differential abundance test to perform
Expand Down
5 changes: 3 additions & 2 deletions R/codifab.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ add_codifab <- function(ta, condition, conditions = NULL, max_taxa = 30) {
#' Significance of tests is determined by capping the false discovery rate at
#' 10%, using the method of Benjamini and Yekutieli, which is developed for
#' non-independent tests. See [p.adjust].
#'
#' @importFrom stats p.adjust median
#' @param ta A tidytacos object
#' @param diffabun_var The variable with differential abundances in the
#' taxon_pair table
Expand Down Expand Up @@ -207,7 +207,8 @@ codifab_plot <- function(ta, diffabun_var) {
}

#' Add compositional principal components to the sample table
#'
#' @importFrom stats prcomp
#' @param ta a tidytacos object.
#' @export
add_copca <- function(ta) {

Expand Down
2 changes: 1 addition & 1 deletion R/getters.R
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ abundances <- function(ta) ta$abundances
#' and returns the result.
#'
#' Samples where one or more predictors are NA are removed.
#'
#' @importFrom stats as.formula
#' @param ta A tidytacos object.
#' @param predictors A character vector with predictors to include in the model.
#' @param permutations The number of permutations (more permutations takes
Expand Down
15 changes: 15 additions & 0 deletions R/plotters.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ prepare_for_bp <- function(ta, n = 12, extended = TRUE) {
bar_plot <- function(ta, n = 12, x = sample_clustered, geom_bar = T) {
# convert promise to formula
x <- enquo(x)

error_message = paste0("Label \'", quo_name(x),"\' not found in the samples table.")
warning_message = "Sample labels not unique, samples are aggregated."
if (quo_name(x) != "sample_clustered" &&
!is.element(quo_name(x), names(ta$samples))
) {
stop(error_message)
}

if (quo_name(x) != "sample_clustered" &&
length(unique(ta$samples %>% pull(!!x))) < nrow(ta$samples)
) {
warning(warning_message)
}


# make plot and return
plot <- prepare_for_bp(ta, n) %>%
Expand Down
6 changes: 5 additions & 1 deletion R/tidyamplicons.R → R/tidytacos.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@
#' @import vegan
#' @importFrom tibble tibble
#' @importFrom plotly plot_ly layout
globalVariables(c(
"sample_id", "samples",
"taxon_id", "taxa", "taxon", "taxon_name_color",
"abundance", "rel_abundance", "presence"
))
NULL

4 changes: 3 additions & 1 deletion R/varia.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' DEPRECATED, see \code{\link{filter_samples}}
#'
#' Should only be used internally.
#'
#' @noRd
#' @export
process_sample_selection <- function(ta) {

Expand Down Expand Up @@ -76,6 +76,7 @@ rank_names <- function(ta) {
#' Set rank names for a tidytacos object
#' @param ta a tidytacos object
#' @param rank_names a vector containing the names of the ranks used to describe the taxa
#' @return An updated tidytacos object.
#' @export
set_rank_names <- function(ta, rank_names) {

Expand All @@ -86,6 +87,7 @@ set_rank_names <- function(ta, rank_names) {
}

# for internal use, I think
#' @noRd
merge_redundant_taxa <- function(ta) {

# merge taxa in taxon table
Expand Down
3 changes: 3 additions & 0 deletions man/add_copca.Rd

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

9 changes: 9 additions & 0 deletions man/add_occurrences.Rd

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

2 changes: 2 additions & 0 deletions man/add_taxon_name_color.Rd

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

14 changes: 0 additions & 14 deletions man/process_sample_selection.Rd

This file was deleted.

3 changes: 3 additions & 0 deletions man/set_rank_names.Rd

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

2 changes: 1 addition & 1 deletion man/tidytacos.Rd

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

10 changes: 7 additions & 3 deletions tests/testthat/test-plotters.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ test_that("Barplot returns identical plot", {
vdiffr::expect_doppelganger("Default barplot", bp)
})

test_that("Barplot returns identical plot when different arguments are used", {
bp <- ta_test %>% bar_plot(n=5, x=participant)
test_that("Barplot raises warning when aggregating samples", {
expect_warning(bp <- ta_test %>% bar_plot(n=5, x=participant))
vdiffr::expect_doppelganger("Custom barplot", bp)
})
})

test_that("Barplot raises error when providing non-existant label", {
expect_error(ta_test %>% bar_plot(x=imagined))
})

0 comments on commit 871c0cf

Please sign in to comment.