Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Literal curly brackets in gtsummary

How can I obtain literal curly brackets in tbl_summary?

Here is what I tried:

> gtsummary::tbl_summary(mtcars,statistic = all_continuous() ~ "\\makecell{{{mean}}}")
Error in `gtsummary::tbl_summary()`:
! Problem with the `statistic` argument.
Error converting string "{{mean" to a function.
ℹ Is the name spelled correctly and available?
Run `rlang::last_trace()` to see where the error occurred.
like image 469
robertspierre Avatar asked Oct 29 '25 08:10

robertspierre


1 Answers

Because of the way tbl_summary() processes the statistic argument, this does not seem directly possible at the moment.

Therefore, one option would be to post-process the output like so:

library(gtsummary)

# insert placeholders
x <- tbl_summary(
  mtcars,
  statistic = list(all_continuous() ~ "curly_open{mean}curly_close")
)

# remove placeholders from footnote
x$table_styling$footnote <- x$table_styling$footnote |>
  mutate(
    footnote = sub("curly_open", "", footnote),
    footnote = sub("curly_close", "", footnote)
  )

# replace placeholders in body
x$table_body <- x$table_body |>
  mutate(
    stat_0 = sub("curly_open", "{", stat_0),
    stat_0 = sub("curly_close", "}", stat_0)
  )

# print
x

enter image description here

Created on 2025-01-16 with reprex v2.1.1

like image 120
the-mad-statter Avatar answered Oct 31 '25 20:10

the-mad-statter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!