From 4875e844cbd8639367dd8cf67b226cb118228422 Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Sat, 31 Aug 2024 17:37:39 -0700 Subject: [PATCH 1/2] Curly brace pairs condition printing fix --- DESCRIPTION | 4 ++-- NEWS.md | 2 ++ R/add_p.R | 2 +- R/add_stat.R | 4 ++-- R/add_vif.R | 2 +- R/combine_terms.R | 2 +- R/tbl_uvregression.R | 10 +++++----- tests/testthat/_snaps/add_overall.tbl_ard_summary.md | 8 ++++++++ 8 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 tests/testthat/_snaps/add_overall.tbl_ard_summary.md diff --git a/DESCRIPTION b/DESCRIPTION index 49c2e9e2e4..7bc04dd730 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: gtsummary Title: Presentation-Ready Data Summary and Analytic Result Tables -Version: 2.0.1.9011 +Version: 2.0.1.9012 Authors@R: c( person("Daniel D.", "Sjoberg", , "danield.sjoberg@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-0862-2018")), @@ -45,7 +45,7 @@ BugReports: https://github.com/ddsjoberg/gtsummary/issues Depends: R (>= 4.2) Imports: - cards (>= 0.2.1.9009), + cards (>= 0.2.1.9017), cli (>= 3.6.1), dplyr (>= 1.1.3), glue (>= 1.6.2), diff --git a/NEWS.md b/NEWS.md index 9b45e6643d..a6acb14a30 100644 --- a/NEWS.md +++ b/NEWS.md @@ -26,6 +26,8 @@ Updates to address regressions in the v2.0.0 release: * Added function `tbl_likert()` for summarizing ordered categorical (or Likert scales) data as well as the associated `add_n.tbl_likert()` S3 method. (#1660) +* Fix where error or warning condition messages containing curly brace pairs could not be printed. + # gtsummary 2.0.1 Updates to address regressions in the v2.0.0 release: diff --git a/R/add_p.R b/R/add_p.R index de5c777ef6..b3e85d85df 100644 --- a/R/add_p.R +++ b/R/add_p.R @@ -261,7 +261,7 @@ calculate_and_add_test_results <- function(x, include, group = NULL, test.args, if (!is.null(lst_captured_results[["warning"]])) { cli::cli_inform(c( "The following warning was returned in {.fun {calling_fun}} for variable {.val {variable}}", - "!" = lst_captured_results[["warning"]] + "!" = "{lst_captured_results[['warning']]}" )) } diff --git a/R/add_stat.R b/R/add_stat.R index a44972af4f..cb4f572b1d 100644 --- a/R/add_stat.R +++ b/R/add_stat.R @@ -242,12 +242,12 @@ eval_fn_safe <- function(variable, tbl, fn) { if (!is_empty(result[["warning"]])) { cli::cli_inform( - c("There was a warning for variable {.val {variable}}", "!" = result[["warning"]]) + c("There was a warning for variable {.val {variable}}", "!" = "{result[['warning']]}") ) } if (!is_empty(result[["error"]])) { cli::cli_inform( - c("There was a error for variable {.val {variable}}", "x" = result[["error"]]) + c("There was a error for variable {.val {variable}}", "x" = "{result[['error']]}") ) } diff --git a/R/add_vif.R b/R/add_vif.R index 3cb3569b5b..3e3462d7b7 100644 --- a/R/add_vif.R +++ b/R/add_vif.R @@ -37,7 +37,7 @@ add_vif <- function(x, statistic = NULL, estimate_fun = label_style_sigfig(digit cardx::ard_car_vif(x$inputs$x) ) if (!is_empty(ard_vif[["error"]])) { - cli::cli_abort(ard_vif[["error"]], call = get_cli_abort_call()) + cli::cli_abort("{ard_vif[['error']]}", call = get_cli_abort_call()) } x$cards$vif <- ard_vif[["result"]] diff --git a/R/combine_terms.R b/R/combine_terms.R index 72014e51ff..5a2f14da53 100644 --- a/R/combine_terms.R +++ b/R/combine_terms.R @@ -68,7 +68,7 @@ combine_terms <- function(x, formula_update, label = NULL, quiet, ...) { i = "There are two common causes for an error. See error message below.", "*" = "The model type is not supported by {.fun stats::anova}.", "*" = "The number of observations used to estimate the full and reduced models is different.", - "x" = anova[["error"]] + "x" = "{anova[['error']]}" ), call = get_cli_abort_call() ) diff --git a/R/tbl_uvregression.R b/R/tbl_uvregression.R index af8e6e6a4d..1202588e6b 100644 --- a/R/tbl_uvregression.R +++ b/R/tbl_uvregression.R @@ -301,14 +301,14 @@ is_quo_empty <- function(x) { if (!is_empty(tbl_i[["error"]])) { cli::cli_abort( c("There was an {cli::col_red('error')} running {.fun tbl_regression} for variable {.val {variable}}. See message below.", - "x" = tbl_i[["error"]]), + "x" = "{tbl_i[['error']]}"), call = get_cli_abort_call() ) } if (!is_empty(tbl_i[["warning"]])) { cli::cli_inform( c("There was a {cli::col_yellow('warning')} running {.fun tbl_regression} for variable {.val {variable}}. See message below.", - "!" = tbl_i[["warning"]]) + "!" = "{tbl_i[['warning']]}") ) } @@ -329,14 +329,14 @@ is_quo_empty <- function(x) { if (!is_empty(model_i[["error"]])) { cli::cli_abort( c("There was an {cli::col_red('error')} constructing the model for variable {.val {variable}}. See message below.", - "x" = model_i[["error"]]), + "x" = "{model_i[['error']]}"), call = get_cli_abort_call() ) } if (!is_empty(model_i[["warning"]])) { cli::cli_inform( c("There was a {cli::col_yellow('warning')} constructing the model for variable {.val {variable}}. See message below.", - "!" = model_i[["warning"]]) + "!" = "{model_i[['warning']]}") ) } @@ -363,7 +363,7 @@ is_quo_empty <- function(x) { if (!is_empty(formula_i[["error"]])) { cli::cli_abort( c("There was an error constructing the formula for variable {.val {variable}}. See message below.", - "x" = formula_i[["error"]]), + "x" = "{formula_i[['error']]}"), call = get_cli_abort_call() ) } diff --git a/tests/testthat/_snaps/add_overall.tbl_ard_summary.md b/tests/testthat/_snaps/add_overall.tbl_ard_summary.md new file mode 100644 index 0000000000..09206b181c --- /dev/null +++ b/tests/testthat/_snaps/add_overall.tbl_ard_summary.md @@ -0,0 +1,8 @@ +# add_overall.tbl_summary() errors + + Code + add_overall(tbl_ard_summary(ard_overall), cards = ard_overall) + Condition + Error: + ! object 'ard_overall' not found + From b5a8b17bd7c151872d5a44ffbbfe2d6b24bb0765 Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Sun, 1 Sep 2024 20:25:40 -0700 Subject: [PATCH 2/2] adding unit tests --- tests/testthat/_snaps/add_stat.md | 35 +++++++++++++++++++++++++++++++ tests/testthat/test-add_stat.R | 18 ++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/tests/testthat/_snaps/add_stat.md b/tests/testthat/_snaps/add_stat.md index 9a28e5c30d..b8cee68b43 100644 --- a/tests/testthat/_snaps/add_stat.md +++ b/tests/testthat/_snaps/add_stat.md @@ -38,3 +38,38 @@ Error in `add_stat()`: ! The `x` argument must be class , not a data frame. +# add_stat() with curly braces in errors/warnings + + Code + curly_warning <- (function(x, ...) { + warning("{curly} warning") + 10 + }) + as.data.frame(add_stat(tbl, fns = ~curly_warning)) + Message + There was a warning for variable "age" + ! {curly} warning + There was a warning for variable "marker" + ! {curly} warning + Output + **Characteristic** **Drug A** \nN = 98 **Drug B** \nN = 102 add_stat_1 + 1 Age 46 (37, 60) 48 (39, 56) 10.0 + 2 Marker Level (ng/mL) 0.84 (0.23, 1.60) 0.52 (0.18, 1.21) 10.0 + +--- + + Code + curly_error <- (function(x, ...) { + stop("{curly} error") + }) + as.data.frame(add_stat(tbl, fns = ~curly_error)) + Message + There was a error for variable "age" + x {curly} error + There was a error for variable "marker" + x {curly} error + Output + **Characteristic** **Drug A** \nN = 98 **Drug B** \nN = 102 add_stat_1 + 1 Age 46 (37, 60) 48 (39, 56) + 2 Marker Level (ng/mL) 0.84 (0.23, 1.60) 0.52 (0.18, 1.21) + diff --git a/tests/testthat/test-add_stat.R b/tests/testthat/test-add_stat.R index b39290af0c..b5a453d66a 100644 --- a/tests/testthat/test-add_stat.R +++ b/tests/testthat/test-add_stat.R @@ -241,4 +241,22 @@ test_that("add_stat() with tbl_svysummary()", { ) }) +test_that("add_stat() with curly braces in errors/warnings", { + expect_snapshot({ + curly_warning <- \(x, ...) { + warning("{curly} warning"); 10 + } + tbl |> + add_stat(fns = ~ curly_warning) |> + as.data.frame() + }) + expect_snapshot({ + curly_error <- \(x, ...) { + stop("{curly} error") + } + tbl |> + add_stat(fns = ~ curly_error) |> + as.data.frame() + }) +})