How to change the cell color of a cell of an R Shiny data table dependent on it's value?

前端 未结 2 1136
鱼传尺愫
鱼传尺愫 2021-01-06 10:09

I am attempting to change the cell color of cells of an R Shiny data table dependent on their value. As an example, I\'ve created the following app:

# ui.R

         


        
2条回答
  •  悲哀的现实
    2021-01-06 10:51

    This page has a bunch of tips for formatting DT data tables: https://rstudio.github.io/DT/010-style.html

    For your specific question, there's the function formatStyle that allows you to set aesthetics based on specific values in the table:

    library(DT)
    options(DT.options = list(pageLength = 5))
    df = as.data.frame(cbind(matrix(round(rnorm(50), 3), 10), sample(0:1, 10, TRUE)))
    
    # style V6 based on values of V6
    datatable(df) %>% formatStyle(
        'V6',
        backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
    )
    
    # style V1 based on values of V6
    datatable(df) %>% formatStyle(
        'V1', 'V6',
        backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
    )
    

提交回复
热议问题