Search code examples
flextable

Multiple Flextables in RMarkdown chunk (with echo = F) do not render


I am using R 3.5.1 on OS Windows 7 x64. My package versions are:
flextable: 0.5.1
officer: 0.3.2
tidyverse: 1.2.1
tidyr: 0.8.2

I am trying to create and then print multiple flextables in a single chunk to Word. I also do not want the actual R code in the Word Doc, so I've set echo = FALSE. However, I've run into something strange.

If I use the following code (echo = T):

```{r, echo = F, warning=FALSE, message=FALSE}
library(tidyverse)
library(flextable)
library(officer)
```

```{r, echo = T}
iris_data <- head(iris, n = 10)
iris_data_ft <- iris_data %>% flextable() %>%
    hline(part = 'header', border = fp_border(color = "black", width = 3)) %>%
    align(align ='center', part = 'all') %>%
    align(j = 1, align ='left', part = 'all') %>%
    add_header_lines('this is a test')
iris_data_ft

iris_data2 <- tail(iris, n = 10)
iris_data2_ft <- iris_data2 %>% flextable() %>%
    hline(part = 'header', border = fp_border(color = "black", width = 3)) %>%
    align(align ='center', part = 'all') %>%
    align(j = 1, align ='left', part = 'all') %>%
    add_header_lines('this is a test')
iris_data2_ft
```

I get the following output of proper flextables in Word: enter image description here

If I use the exact same code, but with echo = F:

```{r, echo = F}
iris_data <- head(iris, n = 10)
iris_data_ft <- iris_data %>% flextable() %>%
    hline(part = 'header', border = fp_border(color = "black", width = 3)) %>%
    align(align ='center', part = 'all') %>%
    align(j = 1, align ='left', part = 'all') %>%
    add_header_lines('this is a test')
iris_data_ft

iris_data2 <- tail(iris, n = 10)
iris_data2_ft <- iris_data2 %>% flextable() %>%
    hline(part = 'header', border = fp_border(color = "black", width = 3)) %>%
    align(align ='center', part = 'all') %>%
    align(j = 1, align ='left', part = 'all') %>%
    add_header_lines('this is a test')
iris_data2_ft
```

I get the following in Word (this goes on for 58 pages): flextablesEchoF

Finally, if I split the one chunk for the two flextables into two separate chunks and set echo = F:

```{r, echo = F}
iris_data <- head(iris, n = 10)
iris_data_ft <- iris_data %>% flextable() %>%
    hline(part = 'header', border = fp_border(color = "black", width = 3)) %>%
    align(align ='center', part = 'all') %>%
    align(j = 1, align ='left', part = 'all') %>%
    add_header_lines('this is a test')
iris_data_ft
```

```{r, echo = F}
iris_data2 <- tail(iris, n = 10)
iris_data2_ft <- iris_data2 %>% flextable() %>%
    hline(part = 'header', border = fp_border(color = "black", width = 3)) %>%
    align(align ='center', part = 'all') %>%
    align(j = 1, align ='left', part = 'all') %>%
    add_header_lines('this is a test')
iris_data2_ft
```

The tables render just fine: flextablesEchoFtwoChunks

While I don't mind separating the flextables into different chunks, I am wondering why this is happening. Any guidance would be greatly appreciated. Thanks!


Solution

  • Here is a workaround:

    
    
    ---
    title: "Untitled"
    output: word_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    library(flextable)
    ```
    
    ```{r echo=FALSE, results='asis'}
    library(htmltools)
    ft <- flextable(head(iris))
    for(i in 1:3){
      cat("```{=openxml}\n")
      cat(format(ft, type = "wml"), "\n")
      cat("```\n")
    }
    ```