Skip to content

Commit

Permalink
Updated examples in add_p.tbl_continuous() (#2073)
Browse files Browse the repository at this point in the history
* Updated examples in `add_p.tbl_continuous()`

* Improving error messaging
  • Loading branch information
ddsjoberg authored Nov 21, 2024
1 parent 8bf1a84 commit 6c611cc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion R/add_p.R
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ calculate_and_add_test_results <- function(x, include, group = NULL, test.args,
if (!is.null(lst_captured_results[["result"]])) return(lst_captured_results[["result"]]) # styler: off
# otherwise, construct a {cards}-like object with error
dplyr::tibble(
group1 = x$inputs$by,
group1 = switch(!is_empty(x$inputs$by), x$inputs$by),
variable = variable,
stat_name = switch(calling_fun,
"add_p" = "p.value",
Expand Down
10 changes: 8 additions & 2 deletions R/add_p.tbl_continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
#' @export
#' @return 'tbl_continuous' object
#'
#' @examplesIf gtsummary:::is_pkg_installed("cardx")
#' @examplesIf (identical(Sys.getenv("NOT_CRAN"), "true") || identical(Sys.getenv("IN_PKGDOWN"), "true")) && gtsummary:::is_pkg_installed("cardx")
#' # Example 1 ----------------------------------
#' trial |>
#' tbl_continuous(variable = age, by = trt, include = grade) |>
#' add_p()
#' add_p(pvalue_fun = label_style_pvalue(digits = 2))
#'
#' # Example 2 ----------------------------------
#' trial |>
#' tbl_continuous(variable = age, include = grade) |>
#' add_p(test = everything() ~ "kruskal.test")
add_p.tbl_continuous <- function(x,
test = NULL,
pvalue_fun = label_style_pvalue(digits = 1),
Expand Down
10 changes: 8 additions & 2 deletions man/add_p.tbl_continuous.Rd

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

8 changes: 8 additions & 0 deletions tests/testthat/test-add_p.tbl_continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,11 @@ test_that("add_p.tbl_continuous(group) works", {
unlist(compare)
)
})

test_that("add_p.tbl_continuous() messaging", {
expect_message(
tbl_continuous(trial, variable = age, include = grade) |>
add_p(test = ~"lme4"),
"*argument cannot be missing*"
)
})

0 comments on commit 6c611cc

Please sign in to comment.