Reputation: 393
I have series of tables I created from a list. I'm using flextable()
in R-Markdown
to output them to a Word document and I'd like to separate them into one table per page. The workaround from this earlier posted question doesn't fit my issue. Below is my reproducible code:
```{r echo = FALSE, message = FALSE, warning=FALSE}
library(tidyverse)
library(flextable)
my_list <- iris %>%
group_by(Species) %>%
split(f = as.factor(.$Species))
for (i in 1:length(my_list)) {
myft <- flextable(my_list[[i]]) %>%
set_caption(paste0("Table ", as.roman(i),".\n", names(my_list[i]), ".")) %>%
padding(padding = 1.5, part = "all")
print(myft)
cat("\n\n")
}
```
What I get in my output is this:
## a flextable object.
## col_keys: `Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`, `Species`
## header has 1 row(s)
## body has 50 row(s)
## original dataset sample:
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
##
##
## a flextable object.
## col_keys: `Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`, `Species`
## header has 1 row(s)
## body has 50 row(s)
## original dataset sample:
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 7.0 3.2 4.7 1.4 versicolor
## 2 6.4 3.2 4.5 1.5 versicolor
## 3 6.9 3.1 4.9 1.5 versicolor
## 4 5.5 2.3 4.0 1.3 versicolor
## 5 6.5 2.8 4.6 1.5 versicolor
##
##
## a flextable object.
## col_keys: `Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`, `Species`
## header has 1 row(s)
## body has 50 row(s)
## original dataset sample:
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 6.3 3.3 6.0 2.5 virginica
## 2 5.8 2.7 5.1 1.9 virginica
## 3 7.1 3.0 5.9 2.1 virginica
## 4 6.3 2.9 5.6 1.8 virginica
## 5 6.5 3.0 5.8 2.2 virginica
Thanks for the help!
Upvotes: 2
Views: 646
Reputation: 393
Here's how I solved the above issue based on David Gohel's reply.
```{r results='asis'}
library(tidyverse)
library(flextable)
#Print annual yield and area per field
my_list <- iris %>%
group_by(Species) %>%
split(f = as.factor(.$Species))
for (i in 1:length(my_list)) {
myft <- flextable(my_list[[i]]) %>%
set_caption(paste0("Table ", as.roman(i),".\n", names(my_list[i]), ".")) %>%
padding(padding = 1.5, part = "all")
cat("\n\n\\pagebreak\n") #added this bit to break page per table
flextable_to_rmd(myft)
}
```
Upvotes: 2