RMarkdown文件中的条件格式表 [英] Conditional formatting tables in RMarkdown documents

查看:473
本文介绍了RMarkdown文件中的条件格式表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我可能想使用以下规则来对单元格进行着色:



(编辑为不平凡)


  1. 蓝色如果> 4

  2. 如果<= 4和> = 3.5

  3. 黄色,如果> = 3,< 3.5

  4. 橙色如果< 3


As an example, I might want to use the following rule to color the cells:

(edited to un-trivialize)

  1. Blue if > 4
  2. No fill if <= 4 and >= 3.5
  3. Yellow if >= 3 and < 3.5
  4. 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

解决方案

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 http://davidgohel.github.io/ReporteRs/FlexTable.html

这篇关于RMarkdown文件中的条件格式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆