问题
As an example, I might want to use the following rule to color the cells:
(edited to un-trivialize)
- Blue if > 4
- No fill if <= 4 and >= 3.5
- Yellow if >= 3 and < 3.5
- Orange if < 3
Create tables with conditional formatting with RMarkdown + knitr doesn't help me because I don't just want to highlight cells satisfying one set of criteria.
Example rmd:
---
title: "Untitled"
output: html_document
---
```{r, message = FALSE, results = "asis"}
library(knitr)
library(dplyr)
head(iris) %>% kable
```
I'll take a solution utilizing DataTables
if that's what it takes
回答1:
Hello here a solution using function FlexTable
from package ReporteRs
. This function is intended to create Word table but you can get the html code from FlexTable objects with as.html
:
---
title: "Untitled"
output: html_document
---
```{r, results='asis', warning=FALSE, message=FALSE}
library(ReporteRs)
data(iris)
irisFT = FlexTable( iris )
vars <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
for (i in vars) {
irisFT[iris[, i] < 3, i] = cellProperties( background.color = "orange" )
irisFT[iris[, i] >= 3 & iris[, i] < 3.5, i] = cellProperties( background.color = "yellow" )
irisFT[iris[, i] > 4, i] = cellProperties( background.color = "#81DAF5" )
}
cat(as.html(irisFT))
```

For more example, please visit https://davidgohel.github.io/ReporteRs/articles/FlexTable.html
回答2:
knitr
contain vignette with jQuery DataTables example.
vignette("datatables", package = "knitr")
来源:https://stackoverflow.com/questions/25315309/conditional-formatting-tables-in-rmarkdown-documents