在r markdown循环中使用flextable不生成表 [英] Using flextable in r markdown loop not producing tables

查看:270
本文介绍了在r markdown循环中使用flextable不生成表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多表要创建,并且正在尝试循环创建它们.我在rstudio中使用带有rmarkdown的flextable.在循环中使用print(theFlextable)命令会生成文本列表,而不是表格.对于docx和html输出类型,会发生这种情况.如果我不使用循环,则flextable渲染正确.这是一个演示:

I have many tables to create and am trying to create them in a loop. I'm using flextable with rmarkdown inside rstudio. Using print(theFlextable) command in a loop produces a list of text rather than the table. This happens for docx and html output types. If I don't use a loop flextable renders correctly. Here is a demo:

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

这是上面最后一个块的单词输出:

This is the output in word of the last block above:

type:可伸缩对象. col_keys:Sepal.Length,Sepal.Width,Petal.Length,Petal.Width,Species标头有1行,主体有10行原始数据集样本: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

type: flextable object. col_keys: Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species header has 1 row(s) body has 10 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

type:可伸缩对象. col_keys:Sepal.Length,Sepal.Width,Petal.Length,Petal.Width,Species标头有1行,主体有10行原始数据集样本: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维吉尼亚105 6.5 3.0 5.8 2.2维吉尼亚

type: flextable object. col_keys: Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species header has 1 row(s) body has 10 row(s) original dataset sample: Sepal.Length Sepal.Width Petal.Length Petal.Width Species 101 6.3 3.3 6.0 2.5 virginica 102 5.8 2.7 5.1 1.9 virginica 103 7.1 3.0 5.9 2.1 virginica 104 6.3 2.9 5.6 1.8 virginica 105 6.5 3.0 5.8 2.2 virginica

推荐答案

我不确定这是否是正确的答案,但是我用它来解决我的问题:

I'm not sure if this is the correct answer but I used this to solve my problem:

在knitr和rmarkdown中遍历代码

这篇关于在r markdown循环中使用flextable不生成表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆