robertspierre
robertspierre

Reputation: 4431

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.

Upvotes: 4

Views: 74

Answers (2)

robertspierre
robertspierre

Reputation: 4431

At last it was implemented in this PR.

Here is the result of the previous code:

gtsummary::tbl_summary(mtcars,
                       statistic = gtsummary::all_continuous() ~ "\\makecell{{{mean}}}")

enter image description here

Upvotes: 0

the-mad-statter
the-mad-statter

Reputation: 8886

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

Upvotes: 3

Related Questions