Conditional formatting tables in RMarkdown documents

后端 未结 2 637
礼貌的吻别
礼貌的吻别 2021-02-04 16:49

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
相关标签:
2条回答
  • 2021-02-04 17:02

    knitr contain vignette with jQuery DataTables example.

    vignette("datatables", package = "knitr")
    
    0 讨论(0)
  • 2021-02-04 17:19

    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))
    ```
    

    enter image description here

    For more example, please visit https://davidgohel.github.io/ReporteRs/articles/FlexTable.html

    0 讨论(0)
提交回复
热议问题