diff --git a/R/dplyr_methods.R b/R/dplyr_methods.R index a57299f..e9f8512 100755 --- a/R/dplyr_methods.R +++ b/R/dplyr_methods.R @@ -229,8 +229,7 @@ mutate.SingleCellExperiment <- function(.data, ...) { tst <- intersect( - cols %>% - names(), + cols, get_special_columns(.data) %>% c(get_needed_columns(.data)) ) %>% @@ -240,13 +239,17 @@ mutate.SingleCellExperiment <- function(.data, ...) { if (tst) { columns = get_special_columns(.data) %>% - c(get_needed_columns()) %>% + c(get_needed_columns(.data)) %>% paste(collapse=", ") stop( - "tidySingleCellExperiment says: you are trying to rename a column that is view only", - columns, " ", - "(it is not present in the colData). If you want to mutate a view-only column, make a copy and mutate that one." + "tidySingleCellExperiment says: you are trying to mutate a column that is view only `", + cols, + "` ", + "(it is not present in the colData). If you want to mutate a view-only column, make a copy (e.g. mutate(new_column = ", + cols[1], + ")) and mutate that one." ) + } colData(.data) <- @@ -275,30 +278,38 @@ mutate.SingleCellExperiment <- function(.data, ...) { rename.SingleCellExperiment <- function(.data, ...) { # Check that we are not modifying a key column - cols <- tidyselect::eval_select(expr(c(...)), colData(.data) %>% as.data.frame()) - - tst <- - intersect( - cols %>% - names(), - get_special_columns(.data) %>% - c(get_needed_columns(.data)) - ) %>% - length() %>% - gt(0) - - if (tst) { - columns = - get_special_columns(.data) %>% - c(get_needed_columns(.data)) %>% - paste(collapse=", ") - stop( - "tidySingleCellExperiment says: you are trying to rename a column that is view only", - columns, " ", - "(it is not present in the colData). If you want to mutate a view-only column, make a copy and mutate that one." - ) - } - + read_only_columns <- c( + get_needed_columns(.data), + get_special_columns(.data) + ) + + # Small df to be more efficient + df <- .data[1,1] |> as_tibble() + + # What columns we are going to create + cols_from <- tidyselect::eval_select(expr(c(...)), df) |> names() + + # What are the columns before renaming + original_columns = df |> colnames() + + # What the column after renaming would be + new_colums = df |> rename(...) |> colnames() + + # What column you are impacting + changed_columns = original_columns |> setdiff(new_colums) + + # Check that you are not impacting any read-only columns + if(any(changed_columns %in% read_only_columns)) + stop( + "tidySingleCellExperiment says: you are trying to rename a column that is view only `", + changed_columns, + "` ", + "(it is not present in the colData). If you want to rename a view-only column, make a copy (e.g. mutate(", + cols_from[1], + " = ", + changed_columns[1], + "))." + ) colData(.data) <- dplyr::rename(colData(.data) %>% as.data.frame(), ...) %>% DataFrame() @@ -387,7 +398,7 @@ inner_join.SingleCellExperiment <- function(x, y, by=NULL, copy=FALSE, suffix=c( if(is_sample_feature_deprecated_used( x, when(by, !is.null(.) ~ by, ~ colnames(y)))){ x= ping_old_special_column_into_metadata(x) } - + x %>% as_tibble() %>% dplyr::inner_join(y, by=by, copy=copy, suffix=suffix, ...) %>% diff --git a/data/pbmc_small.rda b/data/pbmc_small.rda index aaf8f65..bba613d 100755 Binary files a/data/pbmc_small.rda and b/data/pbmc_small.rda differ diff --git a/tests/testthat/test-dplyr_methods.R b/tests/testthat/test-dplyr_methods.R index 6faee7c..50d5656 100755 --- a/tests/testthat/test-dplyr_methods.R +++ b/tests/testthat/test-dplyr_methods.R @@ -1,191 +1,233 @@ -context("dplyr test") - -library(magrittr) - -test_that("arrange", { +df <- pbmc_small +df$number <- sample(seq(ncol(df))) +df$factor <- sample( + factor(1:3, labels=paste0("g", 1:3)), + ncol(df), TRUE, c(0.1, 0.3, 0.6)) + +test_that("arrange()", { + expect_identical( + arrange(df, number), + df[, order(df$number)]) + suppressWarnings({ + fd <- df %>% + scater::logNormCounts() %>% + scater::runPCA() + }) + expect_identical( + arrange(fd, PC1), + fd[, order(reducedDim(fd)[, 1])]) + fd <- df %>% + mutate(foo=seq(ncol(df))) %>% + arrange(foo) %>% select(-foo) + expect_identical(fd, df) +}) + +test_that("bind_rows()", { + # warn about duplicated cells names + expect_warning(fd <- bind_rows(df, df)) + # cell names should be unique after binding + expect_true(!any(duplicated(pull(fd, .cell)))) +}) + +test_that("bind_cols()", { + fd <- bind_cols(df, select(df, factor)) + i <- grep("^factor", names(colData(fd))) + expect_length(i, 2) + expect_identical(fd[[i[1]]], df$factor) + expect_identical(fd[[i[2]]], df$factor) + expect_identical( + select(fd, -starts_with("factor")), + select(df, -factor)) +}) + +test_that("distinct()", { + fd <- distinct(df, factor) + expect_equal(nrow(fd), nlevels(df$factor)) + expect_identical(fd[[1]], unique(df$factor)) +}) + +test_that("filter()", { + fd <- filter(df, factor %in% levels(df$factor)) + expect_identical(df, fd) + fd <- filter(df, factor == "g1") + expect_equal(ncol(fd), sum(df$factor == "g1")) + # missing cell names + fd <- df; colnames(fd) <- NULL + expect_silent(filter(df, number == 1)) + expect_message(fd <- filter(fd, number < 10)) + expect_type(pull(fd, .cell), "character") + expect_null(colnames(fd)) +}) + +test_that("group_by()", { + fd <- group_by(df, factor) + expect_equal(n_groups(fd), nlevels(df$factor)) + expect_equal(group_size(fd), tabulate(df$factor)) +}) + +test_that("summaris/ze()", { + fd <- mutate(df, n=runif(ncol(df))) + ne <- summarise(fd, a=mean(n)) + mo <- summarize(fd, b=mean(n)) + expect_identical(ne$a, mean(fd$n)) + expect_identical(ne$a, mo$b) +}) + +test_that("mutate()", { + fd <- mutate(df, peter="pan") + expect_true(all(fd$peter == "pan")) + fd <- mutate(df, number=paste(number)) + expect_identical(fd$number, paste(df$number)) + + # special columns are blocked + df |> + mutate(.cell=1) |> + expect_error(regexp = "you are trying to mutate a column that is view only") + + df |> + mutate(PC_10=1) |> + expect_error(regexp = "you are trying to mutate a column that is view only") +}) + +test_that("rename()", { + fd <- rename(df, num=number, fac=factor) + expect_identical(fd$num, df$number) + expect_identical(fd$fac, df$factor) + + df |> + rename(ne=mo) |> + expect_error(regexp = "Column `mo` doesn't exist") + + # special columns are blocked + # ...'to' cannot be special + + df |> + rename(a=PC_1) |> + expect_error(regexp = "you are trying to rename a column that is view only") + + df |> + rename(a=.cell) |> + expect_error(regexp = "you are trying to rename a column that is view only") + # ...'from' cannot be special + + df |> + rename(PC_1=number) |> + expect_error(regexp = "These names are duplicated") + + df |> + rename(.cell=number) |> + expect_error(regexp = "These names are duplicated") +}) + +test_that("left_join()", { + y <- df |> + distinct(factor) |> + mutate(string=letters[seq(nlevels(df$factor))]) + fd <- left_join(df, y, by="factor") + expect_s4_class(fd, "SingleCellExperiment") + expect_equal(n <- ncol(colData(fd)), ncol(colData(df))+1) + expect_identical(colData(fd)[-n], colData(df)) +}) + +test_that("inner_join()", { + y <- df |> + distinct(factor) |> + mutate(string=letters[seq(nlevels(df$factor))]) |> + slice(1) + fd <- inner_join(df, y, by="factor") + expect_s4_class(fd, "SingleCellExperiment") + expect_equal(n <- ncol(colData(fd)), ncol(colData(df))+1) + expect_equal(ncol(fd), sum(df$factor == fd$factor[1])) +}) + +test_that("right_join()", { + y <- df |> + distinct(factor) |> + mutate(string=letters[seq(nlevels(df$factor))]) |> + slice(1) + fd <- right_join(df, y, by="factor") + expect_s4_class(fd, "SingleCellExperiment") + expect_equal(n <- ncol(colData(fd)), ncol(colData(df))+1) + expect_equal(ncol(fd), sum(df$factor == fd$factor[1])) +}) + +test_that("full_join()", { + # w/ duplicated cell names + y <- tibble(factor="g2", other=1:3) + fd <- expect_message(full_join(df, y, by="factor", relationship="many-to-many")) + expect_s3_class(fd, "tbl_df") + expect_true(all(is.na(fd$other[fd$factor != "g2"]))) + expect_true(all(!is.na(fd$other[fd$factor == "g2"]))) + expect_equal(nrow(fd), ncol(df)+2*sum(df$factor == "g2")) + # w/o duplicates + y <- tibble(factor="g2", other=1) + fd <- expect_silent(full_join(df, y, by="factor")) + expect_s4_class(fd, "SingleCellExperiment") + expect_identical( + select(fd, -other), + mutate(df, factor=paste(factor))) +}) + +test_that("slice()", { + expect_identical(slice(df), df[, 0]) + expect_identical(slice(df, ncol(df)+1), df[, 0]) + expect_identical(slice(df, 1), df[, 1]) + expect_identical(slice(df, -1), df[, -1]) + i <- sample(ncol(df), 5) + expect_identical(slice(df, i), df[, i]) + expect_identical(slice(df, -i), df[, -i]) +}) + +test_that("select()", { + fd <- select(df, .cell, number) + expect_s4_class(fd, "SingleCellExperiment") + expect_equal(dim(fd), dim(df)) + fd <- select(df, number) + expect_s3_class(fd, "tbl_df") + expect_equal(nrow(fd), ncol(df)) +}) + +test_that("sample_n()", { + fd <- sample_n(df, n <- 50) + expect_s4_class(fd, "SingleCellExperiment") + expect_equal(nrow(fd), nrow(df)) + expect_equal(ncol(fd), n) + fd <- sample_n(df, 1e3, TRUE) + expect_s3_class(fd, "tbl_df") + expect_equal(nrow(fd), 1e3) +}) + +test_that("sample_frac()", { + fd <- sample_frac(df, 0.1) + expect_s4_class(fd, "SingleCellExperiment") + expect_equal(nrow(fd), nrow(df)) + expect_equal(ncol(fd), ncol(df)/10) + fd <- sample_frac(df, 10, TRUE) + expect_s3_class(fd, "tbl_df") + expect_equal(nrow(fd), ncol(df)*10) +}) + +test_that("count()", { + fd <- count(df, factor) + expect_s3_class(fd, "tbl_df") + expect_equal(nrow(fd), nlevels(df$factor)) + expect_identical(fd$n, tabulate(df$factor)) +}) + +test_that("add_count()", { + fd <- add_count(df, factor) + expect_identical(select(fd, -n), df) + expect_identical(fd$n, unname(c(table(df$factor)[df$factor]))) +}) + +test_that("rowwise()", { + df |> + summarise(sum(lys)) |> + expect_error(regexp = "object 'lys' not found") - expect_warning( - tt_pca_aranged <- - pbmc_small %>% - arrange(groups) %>% - scater::logNormCounts() %>% - scater::runPCA()) - - expect_warning( - tt_pca <- - pbmc_small %>% - scater::logNormCounts() %>% - scater::runPCA()) - - expect_equal( - reducedDims(tt_pca_aranged)$PCA[sort(colnames(tt_pca_aranged)), 1:3] %>% abs() %>% head(), - reducedDims(tt_pca_aranged)$PCA[sort(colnames(tt_pca_aranged)), 1:3] %>% abs() %>% head(), - tollerance = 1e-3 - ) -}) - -test_that("bind_rows", { - expect_warning( - tt_bind <- pbmc_small %>% - bind_rows(pbmc_small)) - - tt_bind %>% - select(.cell) %>% - tidySingleCellExperiment:::to_tib() %>% - dplyr::count(.cell) %>% - dplyr::count(n, name="m") %>% - nrow() %>% - expect_equal(1) -}) - -test_that("bind_cols", { - tt_bind <- pbmc_small %>% select(groups) - - pbmc_small %>% - bind_cols(tt_bind) %>% - select(groups...7) %>% - ncol() %>% - expect_equal(1) -}) - -test_that("distinct", { - pbmc_small %>% - distinct(groups) %>% - ncol() %>% - expect_equal(1) -}) - -test_that("filter", { - pbmc_small %>% - filter(groups == "g1") %>% - ncol() %>% - expect_equal(44) -}) - -test_that("group_by", { - pbmc_small %>% - group_by(groups) %>% - nrow() %>% - expect_equal(80) -}) - -test_that("summarise", { - pbmc_small %>% - summarise(mean(nCount_RNA)) %>% - nrow() %>% - expect_equal(1) -}) - -test_that("mutate", { - pbmc_small %>% - mutate(groups = 1) %>% - distinct(groups) %>% - nrow() %>% - expect_equal(1) -}) - -test_that("rename", { - pbmc_small %>% - rename(s_score = groups) %>% - select(s_score) %>% - ncol() %>% - expect_equal(1) -}) - -test_that("left_join", { - pbmc_small %>% - left_join(pbmc_small %>% - distinct(groups) %>% - mutate(new_column = 1:2)) %>% - colData() %>% - ncol() %>% - expect_equal(10) -}) - -test_that("inner_join", { - pbmc_small %>% - inner_join(pbmc_small %>% - distinct(groups) %>% - mutate(new_column = 1:2) %>% - slice(1)) %>% - ncol() %>% - expect_equal(36) -}) - -test_that("right_join", { - pbmc_small %>% - right_join(pbmc_small %>% - distinct(groups) %>% - mutate(new_column = 1:2) %>% - slice(1)) %>% - ncol() %>% - expect_equal(36) -}) - -test_that("full_join", { - pbmc_small %>% - full_join(tibble::tibble(groups = "g1", other = 1:4)) %>% - nrow() %>% - expect_equal(212) -}) - -test_that("slice", { - pbmc_small %>% - slice(1) %>% - ncol() %>% - expect_equal(1) -}) - -test_that("select", { - pbmc_small %>% - select(.cell, orig.ident) %>% - class() %>% - as.character() %>% - expect_equal("SingleCellExperiment") - - pbmc_small %>% - select(orig.ident) %>% - class() %>% - as.character() %>% - .[1] %>% - expect_equal("tbl_df") -}) - -test_that("sample_n", { - pbmc_small %>% - sample_n(50) %>% - ncol() %>% - expect_equal(50) - - expect_equal( pbmc_small %>% sample_n(500, replace = TRUE) %>% ncol, 31 ) -}) - -test_that("sample_frac", { - pbmc_small %>% - sample_frac(0.1) %>% - ncol() %>% - expect_equal(8) - - expect_equal( pbmc_small %>% sample_frac(10, replace = TRUE) %>% ncol, 31 ) -}) - -test_that("count", { - pbmc_small %>% - count(groups) %>% - nrow() %>% - expect_equal(2) -}) - -test_that("add count", { - pbmc_small %>% - add_count(groups) %>% - nrow() %>% - expect_equal(230) -}) - -test_that("summarize alias", { - pbmc_small %>% - summarize(nCount_RNA = mean(nCount_RNA)) %>% - nrow() %>% - expect_equal(1) + df$lys <- replicate(ncol(df), sample(10, 3), FALSE) + fd <- df |> rowwise() |> summarise(sum(lys)) + expect_s3_class(fd, "tbl_df") + expect_equal(dim(fd), c(ncol(df), 1)) + expect_identical(fd[[1]], sapply(df$lys, sum)) }) diff --git a/tests/testthat/test-ggplotly_methods.R b/tests/testthat/test-ggplotly_methods.R new file mode 100644 index 0000000..6041ad2 --- /dev/null +++ b/tests/testthat/test-ggplotly_methods.R @@ -0,0 +1,37 @@ +df <- pbmc_small +df$number <- rnorm(ncol(df)) +df$factor <- sample(gl(3, 1, ncol(df))) + +test_that("ggplot()", { + # cell metadata + p <- ggplot(df, aes(factor, number)) + expect_silent(show(p)) + expect_s3_class(p, "ggplot") + # assay data + g <- sample(rownames(df), 1) + fd <- join_features(df, g, shape="wide") + p <- ggplot(fd, aes(factor, .data[[make.names(g)]])) + expect_silent(show(p)) + expect_s3_class(p, "ggplot") + # reduced dimensions + p <- ggplot(df, aes(PC_1, PC_2, col=factor)) + expect_silent(show(p)) + expect_s3_class(p, "ggplot") +}) + +test_that("plotly()", { + # cell metadata + p <- plot_ly(df, x=~factor, y=~number, type="violin") + expect_silent(show(p)) + expect_s3_class(p, "plotly") + # assay data + g <- sample(rownames(df), 1) + fd <- join_features(df, g, shape="wide") + p <- plot_ly(fd, x=~factor, y=g, type="violin") + expect_silent(show(p)) + expect_s3_class(p, "plotly") + # reduced dimensions + p <- plot_ly(fd, x=~PC_1, y=~PC_2, type="scatter", mode="markers") + expect_silent(show(p)) + expect_s3_class(p, "plotly") +}) diff --git a/tests/testthat/test-methods.R b/tests/testthat/test-methods.R index 20d142b..4cabdf5 100644 --- a/tests/testthat/test-methods.R +++ b/tests/testthat/test-methods.R @@ -1,61 +1,71 @@ -context('methods test') - -data("pbmc_small") -library(dplyr) -test_that("join_features",{ - - - pbmc_small %>% - join_features("CD3D") %>% - slice(1) %>% - pull(.abundance_counts) %>% - expect_equal(4, tolerance=0.1) - +df <- pbmc_small +test_that("show()", { + txt <- capture.output(show(df)) + expect_lt(length(txt), 20) + expect_equal(grep("SingleCellExperiment", txt), 1) + i <- grep(str <- ".*Features=([0-9]+).*", txt) + expect_equal(gsub(str, "\\1", txt[i]), paste(nrow(df))) + i <- grep(str <- ".*Cells=([0-9]+).*", txt) + expect_equal(gsub(str, "\\1", txt[i]), paste(ncol(df))) }) +test_that("join_features()", { + gs <- sample(rownames(df), 3) + # long + fd <- join_features(df, gs, shape="long") + expect_s3_class(fd, "tbl_df") + expect_setequal(unique(fd$.feature), gs) + expect_true(all(table(fd$.feature) == ncol(df))) + expect_identical( + matrix(fd$.abundance_counts, nrow=length(gs)), + as.matrix(unname(counts(df)[fd$.feature[seq_along(gs)], ]))) + # wide + fd <- join_features(df, gs, shape="wide", assay="counts") + expect_s4_class(fd, "SingleCellExperiment") + expect_null(fd$.feature) + expect_identical( + unname(t(as.matrix(as_tibble(fd)[, make.names(gs)]))), + as.matrix(unname(counts(df)[gs, ]))) +}) -test_that("duplicated PCA matrices",{ - - pbmc_small@int_colData@listData$reducedDims$PCA2 = pbmc_small@int_colData@listData$reducedDims$PCA - - pbmc_small %>% - mutate(aa = 1) |> - as_tibble() |> - ncol() |> - expect_equal( - (pbmc_small |> as_tibble() |> ncol()) + 1 - ) - - +test_that("as_tibble()", { + fd <- as_tibble(df) + expect_s3_class(fd, "tbl_df") + expect_equal(nrow(fd), ncol(df)) + ncd <- ncol(colData(df)) + ndr <- vapply(reducedDims(df), ncol, integer(1)) + expect_equal(ncol(fd), sum(1, ncd, ndr)) + # duplicated PCs + reducedDim(df, "PCB") <- reducedDim(df, "PCA") + fd <- as_tibble(mutate(df, abc=1)) + expect_equal(ncol(fd), ncol(as_tibble(df))+1) }) -test_that("aggregate_cells() returns expected values", { - - # Create pseudo-bulk object for testing - pbmc_pseudo_bulk <- pbmc_small |> - tidySingleCellExperiment::aggregate_cells(c(groups, ident), assays = "counts") - - # Check row length is unchanged - pbmc_pseudo_bulk |> - nrow() |> - expect_equal(pbmc_small |> nrow()) - - # Check column length is correctly modified - pbmc_pseudo_bulk |> - ncol() |> - expect_equal(pbmc_small |> - as_tibble() |> - select(groups, ident) |> - unique() |> - nrow() - ) - - # Spot check for correctly aggregated count value of ACAP1 gene - assay(pbmc_pseudo_bulk, "counts")["ACAP1", "g1___0"] |> - expect_equal(assay(pbmc_small, "counts")["ACAP1", pbmc_small |> - as_tibble() |> - filter(groups == "g1", ident == 0) |> - pull(.cell)] |> - sum()) +test_that("aggregate_cells()", { + df$factor <- sample(gl(3, 1, ncol(df))) + df$string <- sample(c("a", "b"), ncol(df), TRUE) + tbl <- distinct(select(df, factor, string)) + fd <- aggregate_cells(df, c(factor, string)) + expect_identical(assayNames(fd), assayNames(df)) + # [HLC: aggregate_cells() currently + # reorders features alphabetically] + fd <- fd[rownames(df), ] + expect_s4_class(fd, "SummarizedExperiment") + expect_equal(dim(fd), c(nrow(df), nrow(tbl))) + foo <- mapply( + f=tbl$factor, + s=tbl$string, + \(f, s) { + expect_identical( + df |> + filter(factor == f, string == s) |> + assay() |> rowSums() |> as.vector(), + fd[, fd$factor == f & fd$string == s] |> + assay() |> as.vector()) + }) + # specified 'assays' are subsetted + expect_error(aggregate_cells(df, c(factor, string), assays="x")) + fd <- aggregate_cells(df, c(factor, string), assays="counts") + expect_identical(assayNames(fd), "counts") }) diff --git a/tests/testthat/test-tidyr_methods.R b/tests/testthat/test-tidyr_methods.R index 2006655..be0b9de 100755 --- a/tests/testthat/test-tidyr_methods.R +++ b/tests/testthat/test-tidyr_methods.R @@ -1,78 +1,65 @@ -context("tidyr test") - -tt <- pbmc_small %>% mutate(col2 = "other_col") - -test_that("nest_unnest", { - col_names <- tt %>% colData %>% colnames() %>% c("cell") - - expect_warning( - x <- tt %>% - nest(data = -groups) %>% - unnest(data) %>% - scater::logNormCounts() %>% - scater::runPCA()) - - expect_warning( - y <- tt %>% - scater::logNormCounts() %>% - scater::runPCA()) +df <- pbmc_small +df$number <- sample(seq(ncol(df))) +df$factor <- sample(gl(2, 1, ncol(df), c("g1", "g2"))) +test_that("un/nest()", { + fd <- nest(df, data=-factor) expect_equal( - reducedDims(x)$PCA %>% - as.data.frame() %>% - as_tibble(rownames = "cell") %>% - arrange(cell) %>% - pull(PC1) %>% - abs(), - reducedDims(x)$PCA %>% - as.data.frame() %>% - as_tibble(rownames = "cell") %>% - arrange(cell) %>% - pull(PC1) %>% - abs() - ) + nrow(fd), + nlevels(df$factor)) + expect_equal( + vapply(fd$data, ncol, integer(1)), + tabulate(df$factor)) + fd <- unnest(fd, data) + # [HLC: this is necessary because unnest() + # currently duplicates the 'int_metadata'] + int_metadata(fd) <- int_metadata(df) + expect_equal(fd, df[, colnames(fd)]) }) -test_that("unite separate", { - un <- tt %>% unite("new_col", c(groups, col2), sep = ":") - - un %>% - select(new_col) %>% - slice(1) %>% - pull(new_col) %>% - expect_equal("g2:other_col") - - se <- - un %>% - separate( - col = new_col, - into = c("orig.ident", "groups"), - sep = ":" - ) - - se %>% - select(orig.ident) %>% - ncol() %>% - expect_equal(1) +test_that("unite()/separate()", { + expect_error(unite(df, "x", c(number, x))) + expect_error(separate(df, x, c("a", "b"))) + + fd <- unite(df, "string", c(number, factor), sep=":") + expect_null(fd$number) + expect_null(fd$factor) + expect_identical(fd$string, paste(df$number, df$factor, sep=":")) + + fd <- separate(fd, string, c("a", "b"), sep=":") + expect_null(fd$string) + expect_identical(fd$a, paste(df$number)) + expect_identical(fd$b, paste(df$factor)) + + # special columns are blocked + expect_error(unite(df, ".cell", c(number, factor), sep=":")) + fd <- df; colnames(fd) <- paste(colnames(df), "x", sep="-") + expect_error(separate(fd, .cell, c("a", "b"), sep="-")) }) -test_that("extract", { - tt %>% - extract(groups, - into = "g", - regex = "g([0-9])", - convert = TRUE) %>% - pull(g) %>% - class() %>% - expect_equal("integer") +test_that("extract()", { + expect_error(extract(df, a, "b")) + expect_error(extract(df, factor, "x", "")) + + fd <- mutate(df, factor=paste(factor)) + expect_identical(extract(df, factor, "factor"), fd) + expect_identical(extract(df, factor, "factor", "(.*)"), fd) + + fd <- extract(df, factor, "g", "g([0-9])", convert=FALSE) + expect_identical(fd$g, gsub("^g", "", df$factor)) + expect_null(fd$factor) + + fd <- extract(df, factor, "g", "g([0-9])", convert=TRUE) + expect_identical(fd$g, as.integer(gsub("^g", "", df$factor))) + expect_null(fd$factor) }) -test_that("pivot_longer", { - tt %>% - pivot_longer(c(orig.ident, groups), - names_to = "name", - values_to = "value") %>% - class() %>% - .[1] %>% - expect_equal("tbl_df") +test_that("pivot_longer()", { + abc <- c("a", "b", "c") + df$string <- sample(abc, ncol(df), TRUE) + fd <- pivot_longer(df, c(factor, string)) + expect_s3_class(fd, "tbl_df") + expect_true(!any(c("factor", "string") %in% names(fd))) + expect_setequal(unique(fd$name), c("factor", "string")) + expect_setequal(unique(fd$value), c(levels(df$factor), abc)) })