我有很多表格要创建,并尝试在循环中创建它们。 我正在使用内部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")
}
```
这是上面最后一个块的Word输出:
类型:flextable对象。列键:萼片长度、萼片宽度、花瓣长度、花瓣宽度、物种;标题有1行,正文有10行。原始数据集样本:萼片长度 萼片宽度 花瓣长度 花瓣宽度 物种 1 5.1 3.5 1.4 0.2 山鸢尾 2 4.9 3.0 1.4 0.2 山鸢尾 3 4.7 3.2 1.3 0.2 山鸢尾 4 4.6 3.1 1.5 0.2 山鸢尾 5 5.0 3.6 1.4 0.2 山鸢尾
类型:flextable对象。列键:萼片长度、萼片宽度、花瓣长度、花瓣宽度、物种;标题有1行,正文有10行。原始数据集样本:萼片长度 萼片宽度 花瓣长度 花瓣宽度 物种 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 维吉尼亚 105 6.5 3.0 5.8 2.2 维吉尼亚
cat(knit_print(x))
。 - Nova