使用RMarkdown+;创建具有条件格式的表;克尼特
我有一个数据帧,我想通过knitr和RMarkdown将其输出到一个HTML文件中,作为一个带有条件格式的表。例如:使用RMarkdown+;创建具有条件格式的表;克尼特,r,knitr,R,Knitr,我有一个数据帧,我想通过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 n xy 1 1 0 2 1 1 3 1 0 4 1 1 5 1 0 我希望高亮显示x和y值不同的行。在本例中,这将是第1、3和5行。如果HTML文件中的输出是一个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
n
xy
1 1 0
2 1 1
3 1 0
4 1 1
5 1 0
我希望高亮显示x和y值不同的行。在本例中,这将是第1、3和5行。如果HTML文件中的输出是一个HTML表格,那就好了,但如果输出失败,图像也可以。我一直想用这个功能扩展我的
pandoc.table
,但是没有时间。但这个问题真的很鼓舞人心,可能会在未来几天内解决。在那之前,关于:
library(pander)
n <- data.frame(x = c(1,1,1,1,1), y = c(0,1,0,1,0))
brew
语法将标记转换为例如HTML:
Pandoc.brew(text = '<%=n%>', output = tempfile(), convert = 'html')
更新:
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
----------------------------------------------------
请注意,这些新功能尚未在CRAN上发布,但您可以在上托管的最新版本中找到。这里是一个基于
xtable
和自定义css
的解决方案。我认为这个解决方案是灵活的,因为一旦你成功了,你可以无限期地定制你的html表格,如果你知道一些css技巧的话
我们开始吧。解决方案包含3个文件:
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;
}
options(rstudio.markdownToHTML =
function(inputFile, outputFile) {
require(markdown)
markdownToHTML(inputFile, outputFile, stylesheet='customstyle.css')
}
)
```{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')
```
`{r}
源('initmd.R')
```
```{r,results='asis'}
图书馆(xtable)
n建议使用ReportRs软件包FlexTable
> 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
---------------------------------------------------------
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;
}
options(rstudio.markdownToHTML =
function(inputFile, outputFile) {
require(markdown)
markdownToHTML(inputFile, outputFile, stylesheet='customstyle.css')
}
)
```{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')
```