flextable icon indicating copy to clipboard operation
flextable copied to clipboard

flextable creates an image instead of a table in RTF documents produced by R Markdown

Open daattali opened this issue 1 year ago • 6 comments

When I create an RTF document with a flextable in it, the table is converted to an image instead of being a table. This has two major drawbacks: (1) if the table has many rows, then the image simply gets cut off at the end of the page, whereas a table would naturally continue on the following page; (2) the resulting table is un-editable

For example, create a test.Rmd file with the following content:

---
output: word_document
---

```{r}
flextable::flextable(head(cars))
```

After knitting this document, you'll get a Word file with a real table in it. But if you replace word_document with rtf_document, then the resulting output file has an image of a table.

daattali avatar Oct 18 '24 16:10 daattali

yes, sorry, there is no plan to implement RTF output for 'R Markdown' (as I don't know exactly how to manipulate the font table and other details - from memory, it's not possible)

you can use officer to produce a RTF and embed flextables into it

davidgohel avatar Oct 18 '24 16:10 davidgohel

For anyone who might run into this issue, I came up with a way to "split" the table into multiple tables so that each one can be on its own page. The main limitation of this algorithm is that it cannot "guess" the ideal number of rows per page, it must be manually selected. This is the best workaround I could come up with to deal with the fact that tables are images. Ideally, tables will become real tables in the future.

---
output: rtf_document
---

```{r, echo=FALSE}
knitr::opts_chunk$set(echo = FALSE)

paged_table <- function(data, rows_per_page = 10) {
  tables <- split(data, ceiling(seq_len(nrow(data)) / rows_per_page))
  tables <- lapply(tables, flextable::flextable)
  content <- ""
  for (i in seq_along(tables)) {
    content <- paste0(content, "```{r}\nMYTABLE[[", i, "]]\n```\n")
    if (i < length(tables)) {
      content <- paste0(content, "```{=rtf}\n\\page\n```\n")
    }
  }
  child_env <- new.env()
  child_env$MYTABLE <- tables
  cat(knitr::knit_child(text = content, quiet = TRUE, envir = child_env))
}
```

```{r, results='asis'}
paged_table(cars)
```

daattali avatar Oct 18 '24 16:10 daattali

depending on your tables, some can be produced/expressed with 'rtables' or 'tables' and these packages allow you to split the table into many tables that then can be transformed into flextables with as_flextable()

davidgohel avatar Oct 18 '24 16:10 davidgohel

Those other packges are for specific types of tables - I was using {flextable} because it allows me to display the exact contents of a dataframe easily and with a lot of visual customizations. It's true that RTF is a weird format to write, so I understand if you don't want to support it with "native" tables. Perhaps this could be mentioned somewhere in some documentation.

daattali avatar Oct 18 '24 19:10 daattali

I didn't say we don't support RTF, I said:

  • we don't support RTF with “R Markdown”.
  • You can generate flextable in RTF documents using the 'officer' package.

As for the documentation, I don't understand what I could add and where.

  1. If you consult the book which is provided as the first URL https://ardata-fr.github.io/flextable-book/ in the suggested help, it's written in the very first chapter.
Capture d’écran 2024-10-19 à 08 28 56
  1. If you prefer to read the manual, the flextable() manual says :
Capture d’écran 2024-10-19 à 08 30 22
  1. And the knit_print manual says:
Capture d’écran 2024-10-19 à 08 29 33

davidgohel avatar Oct 19 '24 06:10 davidgohel

The documentation is clearly very thorough (which is one of the reasons I love using this over other packages)! The only "improvement" I meant is that there is no mention of what happens to RTF in Rmd -- that it gets converted to tables and isn't fully supported. It's true that RTF is not listed as a supported format with Rmd, but because it does work to some extent, it's a bit ambiguous. I thought it would be more clear to state that it has limited support, rather than omit discussing it entirely. But I won't push for this, it was just a comment. You can close this issue if there's no plans to support rtf in rmd.

daattali avatar Oct 20 '24 03:10 daattali

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

github-actions[bot] avatar May 01 '25 05:05 github-actions[bot]