diff --git a/R/add_p.R b/R/add_p.R index d403f660d..12a11fa05 100644 --- a/R/add_p.R +++ b/R/add_p.R @@ -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", diff --git a/R/add_p.tbl_continuous.R b/R/add_p.tbl_continuous.R index f3d2cb765..d7914c95a 100644 --- a/R/add_p.tbl_continuous.R +++ b/R/add_p.tbl_continuous.R @@ -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), diff --git a/man/add_p.tbl_continuous.Rd b/man/add_p.tbl_continuous.Rd index 13fa7ef2a..33aedecdb 100644 --- a/man/add_p.tbl_continuous.Rd +++ b/man/add_p.tbl_continuous.Rd @@ -51,9 +51,15 @@ Default is \code{NULL}. See \link{tests} for methods that utilize the \code{grou Add p-values } \examples{ -\dontshow{if (gtsummary:::is_pkg_installed("cardx")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if ((identical(Sys.getenv("NOT_CRAN"), "true") || identical(Sys.getenv("IN_PKGDOWN"), "true")) && gtsummary:::is_pkg_installed("cardx")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +# 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") \dontshow{\}) # examplesIf} } diff --git a/tests/testthat/test-add_p.tbl_continuous.R b/tests/testthat/test-add_p.tbl_continuous.R index 0a5376fdf..91154dde3 100644 --- a/tests/testthat/test-add_p.tbl_continuous.R +++ b/tests/testthat/test-add_p.tbl_continuous.R @@ -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*" + ) +})