在r markdown循环中使用flextable不生成表

编程入门 行业动态 更新时间:2024-10-27 00:33:16
本文介绍了在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不生成表

本文发布于:2023-10-26 01:21:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528663.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:markdown   flextable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!