本文介绍了r - rmarkdown - 呈现数据帧列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将 Markdown 文档中的数据框列表呈现为单独的表格?
Is it possible to render a list of data frames in a markdown document as individual tables?
示例
给定代码块
```{r listOfDf}
library(knitr)
df <- data.frame(a=rnorm(10),b=rnorm(10),c=c(rep("a",5), rep("b",5)))
l_df <- split(df, df$c)
kable(l_df)
```
这个输出
但我想要更接近 R 控制台中给出的内容:
But I would like something closer to that given in the R console:
> l_df
$a
a b c
1 1.2869909 -0.3117932 a
2 1.4621792 1.2826924 a
3 -0.4274641 0.3335532 a
4 0.7882973 -1.1966702 a
5 -1.2086910 0.1549691 a
$b
a b c
6 -0.9460508 1.4679430 b
7 0.6580554 0.5806850 b
8 -0.7103335 2.6036176 b
9 -2.0110167 2.0488055 b
10 0.7691045 0.3652907 b
但这也被呈现为表格(因此使用了kable()
)
But that is also rendered as a table(s) (hence the use of kable()
)
我试过一个循环*
for(i in 1:length(l_df)){
kable(l_df[i])
}
但这也不起作用(*但它确实适用于图形)
but that doesn't work either (*but it does work for graphs)
推荐答案
pander
包被创建为 将几乎所有 R 对象转换为 Markdown.例如:
The pander
package was created to transform almost any R objects into markdown. E.g.:
> pander(l_df)
* **a**:
-------------------------
a b c
---------- ---------- ---
0.5853656 1.3888279 a
-0.6172632 -0.5179708 a
-0.3321862 -0.2400778 a
-0.6503349 0.9474224 a
0.9476588 0.2288404 a
-------------------------
* **b**:
-------------------------------------
a b c
-------- ------------ ----------- ---
**6** -0.637650387 0.26547037 b
**7** 0.233576764 -0.47811415 b
**8** -0.283486652 0.61113065 b
**9** -0.794994493 -0.08609191 b
**10** -0.004526202 0.36841059 b
-------------------------------------
<!-- end of list -->
这篇关于r - rmarkdown - 呈现数据帧列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!