Gt: Possibility to create an `as_word()` function?

Created on 3 Jan 2019  Â·  10Comments  Â·  Source: rstudio/gt

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:

rtf_word_gt

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.

[3] Advanced [3] High Help Wanted ㋡ [3] High ★ Enhancement

Most helpful comment

I want to mention that I found out (literally a few minutes ago) that our pipelines at work for producing results will be eventually moving to docx output files for tables and listings, so I'm certainly hoping that down the road we can get that as a new output format too.

All 10 comments

The kableExtra package recommended (01-2018) that the way to get output tables into a Word document is to copy/paste HTML table output from the Viewer directly into an open Word doc.

Another option is to just use the rtf_output format that opens in Word (as an RTF file, which could then be saved as a .docx file, for instance).

The knitr::kable() function is able to produce native table code for Word documents, and, the pander package also has this capability (with more options for table output). Here is a document that describes how: https://rmarkdown.rstudio.com/articles_docx.html.

Hi Richard, thanks for your awesome work. It will be fantastic if as_word() function can come in the near future! I wonder if section break (sect) or other similar formatting features can be added to the as_rtf/as_word function?

Hi Richard,
I want to support the development of tools to output gt objects to a word format. I have a Shiny App that produces tables in a *.docx file so my users can incorporate a table into their documents. as_rtf doesn't work in my context. My current Shiny App uses flextable and officer. However, I need to support three separate table formats (HTML and Latex via kable and Word via flextable) to give my users the capabilities they need. Using gt has the potential of really simplifying my app. I need it to be more capable before I can move it into production.

@ABSOD , just my one bullet of think. Did you try pandoc tool to transfer .rtf to .docx ?

I assume the new AST format should be able to use.

https://pandoc.org/MANUAL.html

if this works, as_word could be a simple wrap up of as_rtf and pandoc command.

I want to mention that I found out (literally a few minutes ago) that our pipelines at work for producing results will be eventually moving to docx output files for tables and listings, so I'm certainly hoping that down the road we can get that as a new output format too.

Any suggestion on how to get the table width to be wider so the rows are not split? In the example above, Mercedes-Benz and Porsche as well as the numbers run to multiple lines.

Any progress towards table-making in word ? x)

It is really to bad that collaborators tend to love .docx (ok, it has gotten better over the years) and it is still so hard to programmatically create nice word files... What is the main painpoint for making that work with gt?

Any update on this?

Would outputting rtf formatted tables when rendering a Word document be a reasonable stopgap measure until fuller support for Word could be implemented?

@bwiernik that seems reasonable. And, as an update, there is a PR now that that is focused on actual Word output (e.g., can be used in the R Markdown word_output format). It's pretty far along but won't be included in the release. However, you can definitely try it out by using devtools::install_github("rstudio/gt@w-tbl-exp").

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TuSKan picture TuSKan  Â·  6Comments

DickStartz picture DickStartz  Â·  4Comments

jenniferp96 picture jenniferp96  Â·  4Comments

vincentarelbundock picture vincentarelbundock  Â·  4Comments

steveputman picture steveputman  Â·  3Comments