list - 从列表中循环 flextable 不会在 Word 中创建表格

标签 list loops datatables r-markdown flextable

我有一系列从列表中创建的表。我在 R-Markdown 中使用 flextable() 将它们输出到 Word 文档,我想将它们分成每页一个表格。解决方法来自此 earlier posted question不符合我的问题。以下是我的可重现代码:

```{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")
}

```

我在输出中得到的是这样的:

## 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

感谢您的帮助!

最佳答案

以下是我根据 David Gohel 的回复解决上述问题的方法。

```{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)
}

```

关于list - 从列表中循环 flextable 不会在 Word 中创建表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66959549/

相关文章:

python - Django 在本地时区获取时间

algorithm - 翻转二维列表(或列表列表)的维度,其中每个子列表具有相等的长度

list - 获取列表的第一个和最后一个原子并附加它们

java - 需要 java 泛型参数列表 <T> 的帮助

loops - Tcl foreach 循环中的 awk 命令

java - 循环未按预期工作

python - 如何在 python 列表中查找所有组?

javascript - 通过过滤连接两个数组

javascript - 将回调附加到数据表 : what's the right syntax?

javascript - DataTable中是否有停止显示 "No data available in table"的设置?