ddsjoberg / gtsummary

Presentation-Ready Data Summary and Analytic Result Tables
http://www.danieldsjoberg.com/gtsummary
Other
1.03k stars 113 forks source link

Bug Report: tbl_svysummary modify_header not recognising {N_unweighted} #1911

Closed jakeanders closed 2 weeks ago

jakeanders commented 3 weeks ago

Since updating the package to 2.0.0 I've experienced an issue with modify_header() throwing an error when using it to modify the header of gtsummary tables created using tbl_svysummary() to add unweighted counts (i.e., {N_unweighted} glue value). The error provided suggests (to me) it's not recognising {N_unweighted}, but this option remains featured in the function documentation, so I don't think it's a deliberate change.

If I revert the installed version of the package to 1.7.2 (using r devtools::install_version("gtsummary", version = "1.7.2")) then everything works as expected, so I think this is a change related to the update. I can replicate the issue in a simple setup and with an example dataset (not just my research dataset), so I would anticipate this having the potential to affect others.

I'm running: R version 4.4.1 (2024-06-14) -- "Race for Your Life" Platform: aarch64-apple-darwin20 (64-bit)

Also, first time submitting an issue, so my apologies if I've overlooked providing anything needed or I am overlooking something obvious!

# Load minimal packages
library(dplyr)
library(survey)
library(gtsummary)

# Load sample dataset included in survey package
data(election)
election_design <- svydesign(id = ~1,
                             fpc = ~p,
                             data = election_pps,
                             pps = "brewer")

# Create gtsummary tbl_svysummary table
table <- tbl_svysummary(election_design, include = c("Bush", "Kerry"))

# Request the weighted N in the table header
table |> modify_header(stat_0 ~ "N = {N}")
# Works fine (but it's not what's wanted)

# Request the unweighted N in the table header
# This should just give the same as the default
table |> modify_header(stat_0 ~ "N = {N_unweighted}")

# Error message says:
# Error in `modify_header()`:
#! There was an error in the `glue::glue()` evaluation of "N = {N_unweighted}" for column "label".
#ℹ Run `gtsummary::show_header_names()` for information on values available for glue interpretation.

# So let's run show_header_names to see if it has useful advice
gtsummary::show_header_names(table)
# It tells us about the columns we can rename, but no advice on values available for glue interpretation.
ddsjoberg commented 3 weeks ago

Thank you for the report @jakeanders !

NOTE: Add N_unweighted, n_unweighted, and p_unweighted to x$table_styling$header$modify_stat_* in the .add_table_styling_stats() function.