use*_*973 8 r flextable r-markdown
我有很多表要创建,并且正在尝试在循环中创建它们。我在 rstudio 中使用带有 rmarkdown 的 flextable。print(theFlextable)
在循环中使用命令会生成文本列表而不是表格。这发生在 docx 和 html 输出类型上。如果我不使用循环 flextable 会正确呈现。这是一个演示:
---
title: "Demo"
output: word_document
---
```{r setup, include=FALSE}
library(flextable)
```
## This Works
```{r iris, echo=F, message=F, error=F, results='asis'}
ft<-flextable(iris[1:10,])
ft
```
## This produces no output
```{r echo=F, message=F, error=F, results='asis'}
doThese<-c("setosa","virginica")
for (i in doThese){
tbl<-subset(iris, Species==i)
ft<-flextable(tbl[1:10,])
ft
}
```
## This produces incorrect output
```{r echo=F, message=F, error=F, results='asis'}
doThese<-c("setosa","virginica")
for (i in doThese){
tbl<-subset(iris, Species==i)
ft<-flextable(tbl[1:10,])
print(ft)
cat("\n\n")
}
```
Run Code Online (Sandbox Code Playgroud)
这是上面最后一个块的 word 输出:
类型:弹性对象。col_keys: Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species header has 1 row(s) body has 10 row(s) 原始数据集样本: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.
类型:弹性对象。col_keys: Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species header has 1 row(s) body has 10 row(s) 原始数据集样本:Sepal.Length Sepal.Width Petal.Length Petal.Width Species 101 6.3 3.3 6.0 2.5 维吉尼亚 102 5.8 2.7 5.1 1.9 维吉尼亚 103 7.1 3.0 5.9 2.1 维吉尼亚 104 6.3 2.9 5.6 1.8 53265 维吉尼亚 5
ano*_*red 10
如果您有 Pandoc 版本 >= 2(与 RStudio 1.2 捆绑),您可以使用knit_print
. 我发现
cat(knit_print(ft))
Run Code Online (Sandbox Code Playgroud)
成功地循环打印了表格。