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))
1 Like
DavoWW
February 25, 2022, 2:41am
2
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?
cderv
March 1, 2022, 3:17pm
4
FWIW You could probably follow this issue to know when word output will be supported:
opened 08:09PM - 03 Jan 19 UTC
closed 08:22PM - 20 Aug 22 UTC
Difficulty: [3] Advanced
Effort: [3] High
Help Wanted ㋡
Priority: [3] High
Type: ★ Enhancement
Currently, we can only use the `rtf_document` format in R Markdown to generate a… Word compatible document with **gt** tables embedded within.
Here is a minimal example:
````
---
output: rtf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(gt)
```
# gt Table in RTF document
```{r}
gtcars %>%
dplyr::filter(ctry_origin == "Germany") %>%
dplyr::group_by(mfr) %>%
dplyr::top_n(2, msrp) %>%
dplyr::ungroup() %>%
dplyr::select(mfr, model, drivetrain, msrp) %>%
gt()
```
````
Which produces this document:
<img width="932" alt="rtf_word_gt" src="https://user-images.githubusercontent.com/5612024/50659053-4bd8e880-0f69-11e9-987b-8e5ef28f99af.png">
The problem is that the `word_document` output format for R Markdown doesn't work in this way. Other packages that create display tables haven't yet solved this problem either. Proposal here is to generate a `as_word()` function to create table output that is compatible with the Word document output that R Markdown generates.
Regarding conversion, I don't a good solution. It is not as easy to do, and won't probably be done in flextable
opened 11:34AM - 11 Jun 20 UTC
closed 07:52AM - 30 Aug 20 UTC
I've had some students express interest in the new `gt` package from the RStudio… group: https://gt.rstudio.com/
The package looks like it has a nice interface for constructing tables, but it seems like Office export isn't on their development plan. It would be great if there were an as_flextabl.gt() method to convert a gt object to a flextable for Office output.
Ok; fingers crossed! Any suggested work arounds in the meantime? flextable is a pain...
1 Like
system
Closed
March 22, 2022, 6:31pm
6
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.