Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Aug 25, 2023
1 parent c05e347 commit 66abea2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
18 changes: 11 additions & 7 deletions R/plot.dw_data_tabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@
plot.dw_data_tabulates <- function(x, value_lab = TRUE, remove_na = FALSE,
na_label = "(Missing)", error_bar = TRUE,
...) {
lapply(x, plot.dw_data_tabulate,
value_lab = value_lab, remove_na = remove_na,
na_label = na_label, error_bar = error_bar
)
if (length(x) == 1) {
plot.dw_data_tabulate(
x[[1]], value_lab = value_lab, remove_na = remove_na,
na_label = na_label, error_bar = error_bar
)
} else {
lapply(x, plot.dw_data_tabulate,
value_lab = value_lab, remove_na = remove_na,
na_label = na_label, error_bar = error_bar
)
}
}

#' @rdname plot.dw_data_tabulate
Expand Down Expand Up @@ -51,9 +58,6 @@ plot.dw_data_tabulate <- function(x, value_lab = TRUE, remove_na = FALSE,
ci <- 1.96 * suppressWarnings(sqrt(rel_frq * (1 - rel_frq) / total_n))
dat$upper.ci <- total_n * (rel_frq + ci)
dat$lower.ci <- total_n * (rel_frq - ci)
}

if (isTRUE(error_bar)) {
dat$label <- paste0(dat$N, " (", round(dat$output, 2), "%)")
} else {
dat$label <- paste0(dat$N, "\n(", round(dat$output, 2), "%)")
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-plot.dw_data_tabulate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test_that("`plot.dw_data_tabulate()` works with single table", {
x <- datawizard::data_tabulate(mtcars, select = "cyl")
expect_s3_class(plot(x), "gg")
})

test_that("`plot.dw_data_tabulate()` works with multiple tables", {
x <- datawizard::data_tabulate(mtcars, select = c("cyl", "carb", "am"))
expect_true(is.list(plot(x)))
})

0 comments on commit 66abea2

Please sign in to comment.