diff --git a/R/tax_agg.R b/R/tax_agg.R index 92d1f4f7..e4592d19 100644 --- a/R/tax_agg.R +++ b/R/tax_agg.R @@ -65,10 +65,10 @@ tax_agg <- function(x, rank, db = 'ncbi', verbose=FALSE, ...) { - if(is.matrix(x)) - { - if(is.null(colnames(x))) stop("The community data matrix must have named columns") - x <- data.frame(x, check.names=FALSE) + if (is.matrix(x)) { + if (is.null(colnames(x))) + stop("The community data matrix must have named columns") + x <- data.frame(x, check.names = FALSE) } # bring to long format x$rownames <- rownames(x) @@ -76,7 +76,7 @@ tax_agg <- function(x, rank, db = 'ncbi', verbose=FALSE, ...) # aggregate to family level (by querying NCBI for taxonomic classification) uniq_tax <- as.character(unique(df_m$variable)) - agg <- tax_name(uniq_tax, get = rank, db = db, verbose=verbose, ...) + agg <- tax_name(uniq_tax, get = rank, db = db, verbose = verbose, ...) lookup <- data.frame(variable = uniq_tax, agg = agg[ , 3], stringsAsFactors = FALSE) # merge lookup with orig. @@ -87,7 +87,7 @@ tax_agg <- function(x, rank, db = 'ncbi', verbose=FALSE, ...) # bring back to long format and aggregate df_l <- dcast(df_merged, rownames ~ agg, - value.var='value', fun.aggregate = sum) + value.var = 'value', fun.aggregate = sum) rownames(df_l) <- df_l$rownames df_l$rownames <- NULL diff --git a/tests/testthat/test-rankagg.R b/tests/testthat/test-rankagg.R new file mode 100644 index 00000000..4a5530dc --- /dev/null +++ b/tests/testthat/test-rankagg.R @@ -0,0 +1,19 @@ +# tests for rankagg fxn in taxize +context("rankagg") +data(dune.taxon, package = 'vegan') +dat <- dune.taxon +set.seed(1234) +dat$abundance <- round(rlnorm(n = nrow(dat), meanlog = 5, sdlog = 2), 0) +out <- rankagg(data = dat, datacol = "abundance", rank = "Genus") + + +test_that("rankagg throws error", { + expect_error(rankagg(datacol = "abundance", rank = "Genus")) + expect_error(rankagg(data = dat, datacol = "abundance")) +}) + + +test_that("Correct result", { + expect_is(out, "data.frame") + expect_equal(out[1, 2], 13) +}) diff --git a/tests/testthat/test-tax_agg.R b/tests/testthat/test-tax_agg.R index 4869058a..8c3e78a5 100644 --- a/tests/testthat/test-tax_agg.R +++ b/tests/testthat/test-tax_agg.R @@ -8,6 +8,9 @@ species <- c("Bellis perennis", "Empetrum nigrum", "Juncus bufonius", "Juncus ar "Aira praecox") colnames(take) <- species out_ncbi <- tax_agg(take, rank = 'family', db = 'ncbi', verbose = FALSE) +take2 <- take +colnames(take2) <- NULL + test_that("tax_agg returns the correct class", { @@ -21,6 +24,7 @@ test_that("tax_agg returns the correct value", { expect_that(nrow(out_ncbi$x), equals(nrow(take))) expect_that(nrow(out_ncbi$by), equals(length(unique(colnames(take))))) expect_that(out_ncbi$n_pre, equals(length(unique(colnames(take))))) + expect_error(tax_agg(as.matrix(take2), rank = 'family', db = 'ncbi', verbose = FALSE)) })