7

I'm trying to apply a Word style (table or paragraph) to a flextable object. My motivation is to find a way to stop a flextable from splitting across rows at a page break when outputted to a Word .docx document via knitr. For example, in the following code I've attempted to apply the "Table of Figures" paragraph style to a flextable in R markdown:

---
title: " "
output:
  word_document: default
---



<div custom-style = "Table of Figures">
```{r, echo = F, message = F}
library(flextable)
library(magrittr)

head(cars) %>%
  regulartable

```
</div>

However, this doesn't seem to work. Does anyone know how to apply table and/or paragraph styles to a flextable object?

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Owen
  • 109
  • 4
  • I have no idea what a flextable is, but I know Word very well. What is the result of a flextable in the opened Word document? A straight-forward Word table (rows and columns)? – Cindy Meister Apr 19 '18 at 13:06
  • 2
    This is a long-standing issue: No really good way to apply Word Table styles. It is easy if you only have one style for all tables, though, but everything more flexible seem not to be possible. https://stackoverflow.com/questions/17858598/add-styling-rules-in-pandoc-tables-for-odt-docx-output-table-borders – Dieter Menne Apr 20 '18 at 17:51
  • output: word_document: reference_docx: docs/word-template.docx – Dieter Menne Apr 20 '18 at 17:57
  • @CindyMeister Yes, the flextable package creates/inserts a chunk of MS Word (Open XML) code into the output Markdown file which Pandoc then directly inserts into the output MS Word document as a native MS Word table. However, as Dieter-Menne indicates, there doesn't appear to be a way to individually apply paragraph or table styles to tables inserted by Flextable (or with any other standard R-Markdown method so far as we can tell). – D. Woods Apr 22 '18 at 23:14
  • @Deiter-Menne Thanks for the link to that other SO question/answer(s). We are currently using the reference_docx feature was unaware that we could assign table styles that way. (It is nice that the linked SO post has some R code for hacking the XML of the created document, we're probably going to have to go that route.) Unfortunately, I think Flextable bypasses that feature since it inserts Open XML code directly into the output MS Word document. Thus, there doesn't appear to be a way to get that Flextable generated table to accept a MS Word paragraph or table style. – D. Woods Apr 22 '18 at 23:20
  • 2
    FWIW " find a way to stop a flextable from splitting across rows at a page break" means setting the "Allow Rows to break across pages" property to False. There's certainly an equivalent in Word's Open XML - the question is whether this is part of "flextable". From the "Answer" below it appears someone may have just added it to the functionality... Just giving you the background information... – Cindy Meister Apr 23 '18 at 14:11

1 Answers1

3

Sorry, that's not the way flextable are working. Formats and properties are managed by flextable functions (i.e. padding, align, etc.).

However, I modified the renderer so that rows won't be split across pages in Word document (you will need to update from Github devtools::install_github("davidgohel/flextable")).

David Gohel
  • 9,180
  • 2
  • 16
  • 34