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:
我有一个数据框,我想通过knitr和RMarkdown将它作为带有条件格式的表格输出到HTML文件中。例:
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
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.
我希望突出显示具有不同x和y值的行。所以在这种情况下,那将是第1,3和5行。如果HTML文件中的输出是HTML表格,那么会很好,但是如果图像也没问题那么失败。
2 个解决方案
#1
22
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:
我一直想用这个功能在我的pander包中扩展pandoc.table,但是没有得到时间。但这个问题真的很鼓舞人心,可能会在接下来的几天里做到这一点。在那之前,怎么样:
-
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))
-
Update your lines to be marked as strong in Pandoc:
更新要在Pandoc中标记为强的行:
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
-
Covert the markdown to e.g. HTML with
brew
syntax:将降价转换为例如使用brew语法的HTML:
Pandoc.brew(text = '<%=n%>', output = tempfile(), convert = 'html')
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:
更新:我更新了pander以获取一些新参数以轻松突出显示行/列/单元格。虽然我还在进行一些其他辅助函数来简化这个过程,但是这里有一个快速演示,以便您可以看到它如何帮助您的工作流程:
> 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
-----------
Update: pander
gained some helper functions to highlight cells in the tables even easier:
更新:pander获得了一些辅助函数,可以更轻松地突出显示表格中的单元格:
> 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
----------------------------------------------------
Or directly with pander
method:
或直接使用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
---------------------------------------------------------
Please note that these new features are not published on CRAN yet, but you can find in the most recent version hosted on GitHub.
请注意,这些新功能尚未在CRAN上发布,但您可以在GitHub上托管的最新版本中找到。
#2
17
Here a solution based on xtable
with a custom css
. I think the solution is flexible since once you get it work , you can customize your html tables indefinitely if you know some css tricks.
这里是一个基于xtable和自定义css的解决方案。我认为解决方案很灵活,因为一旦你开始工作,如果你知道一些css技巧,你可以无限期地自定义你的html表。
Here we go. The solution consist in 3 files:
开始了。该解决方案包含3个文件:
-
a css file where I alternate table row color.
一个css文件,我替换表行颜色。
table { max-width: 95%; border: 1px solid #ccc; } th { background-color: #000000; color: #ffffff; } table tr:nth-child(odd) td{ background-color: #FF0000; } table tr:nth-child(even) td{ background-color: #00FFFF; }
-
an R script file to set RStudio markdown with the following content:
一个R脚本文件,用于设置RStudio markdown,内容如下:
options(rstudio.markdownToHTML = function(inputFile, outputFile) { require(markdown) markdownToHTML(inputFile, outputFile, stylesheet='customstyle.css') } )
-
create a new markdown with the following:
使用以下内容创建新的降价:
```{r} source('initmd.R') ``` ```{r,results='asis'} library(xtable) n <- data.frame(x = c(1,1,1,1,1), y = c(0,1,0,1,0)) print(xtable(n),type='html') ```
finally convert the markdwon to html using knit HTML
button and you should get something like this :
最后使用编织HTML按钮将markdwon转换为html,你应该得到这样的东西:
#1
22
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:
我一直想用这个功能在我的pander包中扩展pandoc.table,但是没有得到时间。但这个问题真的很鼓舞人心,可能会在接下来的几天里做到这一点。在那之前,怎么样:
-
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))
-
Update your lines to be marked as strong in Pandoc:
更新要在Pandoc中标记为强的行:
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
-
Covert the markdown to e.g. HTML with
brew
syntax:将降价转换为例如使用brew语法的HTML:
Pandoc.brew(text = '<%=n%>', output = tempfile(), convert = 'html')
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:
更新:我更新了pander以获取一些新参数以轻松突出显示行/列/单元格。虽然我还在进行一些其他辅助函数来简化这个过程,但是这里有一个快速演示,以便您可以看到它如何帮助您的工作流程:
> 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
-----------
Update: pander
gained some helper functions to highlight cells in the tables even easier:
更新:pander获得了一些辅助函数,可以更轻松地突出显示表格中的单元格:
> 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
----------------------------------------------------
Or directly with pander
method:
或直接使用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
---------------------------------------------------------
Please note that these new features are not published on CRAN yet, but you can find in the most recent version hosted on GitHub.
请注意,这些新功能尚未在CRAN上发布,但您可以在GitHub上托管的最新版本中找到。
#2
17
Here a solution based on xtable
with a custom css
. I think the solution is flexible since once you get it work , you can customize your html tables indefinitely if you know some css tricks.
这里是一个基于xtable和自定义css的解决方案。我认为解决方案很灵活,因为一旦你开始工作,如果你知道一些css技巧,你可以无限期地自定义你的html表。
Here we go. The solution consist in 3 files:
开始了。该解决方案包含3个文件:
-
a css file where I alternate table row color.
一个css文件,我替换表行颜色。
table { max-width: 95%; border: 1px solid #ccc; } th { background-color: #000000; color: #ffffff; } table tr:nth-child(odd) td{ background-color: #FF0000; } table tr:nth-child(even) td{ background-color: #00FFFF; }
-
an R script file to set RStudio markdown with the following content:
一个R脚本文件,用于设置RStudio markdown,内容如下:
options(rstudio.markdownToHTML = function(inputFile, outputFile) { require(markdown) markdownToHTML(inputFile, outputFile, stylesheet='customstyle.css') } )
-
create a new markdown with the following:
使用以下内容创建新的降价:
```{r} source('initmd.R') ``` ```{r,results='asis'} library(xtable) n <- data.frame(x = c(1,1,1,1,1), y = c(0,1,0,1,0)) print(xtable(n),type='html') ```
finally convert the markdwon to html using knit HTML
button and you should get something like this :
最后使用编织HTML按钮将markdwon转换为html,你应该得到这样的东西: