gt table printing to Word document knit

Would like the following table to print to a Word knit in Markdown

gt(table3_baro_gt)  |>
 gt::cols_label(edema.yn = "Edema-yes/no",
  proportion = "Prevalence",
  proportion_low = "Lower Bound- 95% CI",
  proportion_upp="Upper Bound- 95% CI",
  total = "Count") |> 
  tab_spanner("Proportion", contains("prop")) |> 
  tab_spanner("Total", contains("total")) |> 
  tab_header(title=md("**Baro- Edema**")) |>
  fmt_percent(contains("prop"), drop_trailing_zeros = T, decimals=1) |> 
  cols_width(proportion:total ~ px(75))

Hi @graceahey,
Rmarkdown of {gt} objects to Word is not (yet) supported.
Here's a link to a summary of the situation
https://www.danieldsjoberg.com/gtsummary/articles/rmarkdown.html

A workaround is either to learn {flextable} or make an intermediate RTF file like this:

library(gt)

# Use `gtcars` to create a gt table; add a header; then export as RTF code
tab_rtf <-
  gtcars %>%
  dplyr::select(mfr, model) %>%
  dplyr::slice(1:8) %>%
  gt() %>%
  tab_header(
    title = md("Data listing from **gtcars**"),
    subtitle = md("`gtcars` is an R dataset")
  ) %>%
  as_rtf()

# Write the RTF code to a file.
my_conn <- file("table.RTF", "w")
  writeLines(tab_rtf, my_conn)
close(my_conn)

# Then open that file manually in Microsoft Word.
1 Like

Thanks! Is there an easy conversation between gt and flextable?

FWIW You could probably follow this issue to know when word output will be supported:

Regarding conversion, I don't a good solution. It is not as easy to do, and won't probably be done in flextable

Ok; fingers crossed! Any suggested work arounds in the meantime? flextable is a pain...

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.