Euan Ives
Euan Ives

Reputation: 111

R Shiny DT how to set colour of Rows matching a criteria

I have searched and seen a few examples but cannot get it to work on my own (more experience needed) so some help would be amazing - I am using R shiny App and rendering a table
Example:

A    B    C    D
a1   1    2    3
a2   4    5    6 
a3   7    8    9
  1. I want to render rows a1 and a2 with a background colour (purple) and a3 (blue)- this data has only 3 categories (a1,a2,a3) but 400 lines - see current code below
  2. currently my data table loads a default of 10 rows - how may i set the option for 25 rows by default.

    output$mytable <- DT::renderDataTable({ Forecast %>% filter(grepl(toupper(input$id_select),Name)) %>% arrange(Name, Title) })

Upvotes: 1

Views: 939

Answers (1)

NicE
NicE

Reputation: 21425

You can use formatStyle to change the color of the rows, here's an example:

library(shiny)
library(DT)
set.seed(100)
data <- data.frame(A=sample(c('a1','a2','a3'),10,replace=T),
                   B=1:10,
                   C=11:20,
                   D=21:30)
shinyApp(
  ui = fluidPage(DT::dataTableOutput('tbl')),
  server = function(input, output) {
    output$tbl = DT::renderDataTable(

      datatable(data, options = list(pageLength = 25)) %>%
        formatStyle('A',target="row",backgroundColor = styleEqual(c('a1','a2','a3'),c("purple","purple","blue")))
    )
  }
)

There are a lot of examples for styling here

Upvotes: 1

Related Questions