使用RMarkdown + knitr以条件格式创建表 [英] Create tables with conditional formatting with RMarkdown + knitr
问题描述
我有一个数据框,我想通过knitr和RMarkdown在HTML文件中将其输出为带有条件格式的表格.示例:
I have a data frame and I want to output this in an HTML file through knitr and RMarkdown as a table with conditional formatting. Example:
n <- data.frame(x = c(1,1,1,1,1), y = c(0,1,0,1,0))
> n
x y
1 1 0
2 1 1
3 1 0
4 1 1
5 1 0
我希望突出显示具有不同x和y值的行.因此,在这种情况下,将是第1、3和5行.如果HTML文件中的输出为HTML表,那将是很好的选择,但如果图像也没有问题,就可以了.
I want rows with different values of x and y to be highlighted. So in this case, that would be rows 1, 3 and 5. Would be nice if the output in the HTML file would be an HTML table, but failing that an image would be fine as well.
推荐答案
我一直想在pandoc.table > pander package 具有此功能,但没有时间.但是这个问题确实令人鼓舞,可能会在接下来的几天中做到这一点.在那之前,该怎么办?
I always wanted to extend pandoc.table
in my pander package with this feature, but failed to get the time for that. But this question is really inspiring, probably will do that in the next few days. Until then, what about:
-
加载程序包:
Load the package:
library(pander)
加载数据:
Load your data:
n <- data.frame(x = c(1,1,1,1,1), y = c(0,1,0,1,0))
将您的行更新为在Pandoc中标记为 strong :
for (i in c(1, 3, 5))
n[i, ] <- pandoc.strong.return(n[1, ])
显示表格的降价版本:
Show the markdown version of your table:
pandoc.table(n)
pander(n) # S3 method
将降价转换为具有brew
语法的HTML:
Covert the markdown to e.g. HTML with brew
syntax:
Pandoc.brew(text = '<%=n%>', output = tempfile(), convert = 'html')
更新:我已经更新了pander
,使一些新参数可以轻松突出显示行/列/单元格.尽管我仍在研究一些辅助功能以简化此过程,但这里还是进行了一个快速演示,以便您可以看到它如何帮助您的工作流程:
Update: I have updated pander
to take some new arguments to highlight rows/columns/cells easily. Although I am still working on some further helper functions to make this process easier, here goes a quick demo so that you might see how it could help your workflow:
> pandoc.table(n, emphasize.rows = c(1, 3, 5))
-------
x y
--- ---
*1* *0*
1 1
*0* *1*
1 1
*1* *0*
-------
> pandoc.table(n, emphasize.strong.cells = which(n == 1, arr.ind = TRUE))
-----------
x y
----- -----
**1** 0
**1** **1**
**1** 0
**1** **1**
**1** 0
-----------
更新:pander
获得了一些帮助器功能,可以更加轻松地突出显示表格中的单元格:
Update: pander
gained some helper functions to highlight cells in the tables even easier:
> t <- mtcars[1:3, 1:5]
> emphasize.cols(1)
> emphasize.rows(1)
> pandoc.table(t)
----------------------------------------------------
mpg cyl disp hp drat
------------------- ------ ----- ------ ----- ------
**Mazda RX4** *21* *6* *160* *110* *3.9*
**Mazda RX4 Wag** *21* 6 160 110 3.9
**Datsun 710** *22.8* 4 108 93 3.85
----------------------------------------------------
或直接使用pander
方法:
> emphasize.strong.cells(which(t > 20, arr.ind = TRUE))
> pander(t)
---------------------------------------------------------
mpg cyl disp hp drat
------------------- -------- ----- ------- ------- ------
**Mazda RX4** **21** 6 **160** **110** 3.9
**Mazda RX4 Wag** **21** 6 **160** **110** 3.9
**Datsun 710** **22.8** 4 **108** **93** 3.85
---------------------------------------------------------
请注意,这些新功能尚未在CRAN上发布,但是您可以在 GitHub .
这篇关于使用RMarkdown + knitr以条件格式创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!