0

In my renderDataTable, I am trying to get two things at the same time:

  • coloured rows based on one column values (here - column "a")
  • get all rows as default

The coloured rows code, depending on value of a:

    library(shiny); library(DT)
ui <- fluidPage(mainPanel(
                sliderInput("seed",
                            "Set seed",
                            min = 1,  max = 100,  value = 1, step = 1),
                mainPanel(
                  tabsetPanel(
                    tabPanel("Table", DT::dataTableOutput("table1"))
              ))))
server <- function(input, output, session){
     df1 <- reactive({
  req(input$seed)
  a <- c(rep(1,3), rep(2,3), rep(3,3),rep(4,5))
   set.seed(input$seed)
    b <- c(runif(3),runif(3),runif(3),runif(5))
    df <- data.frame(a,b)
  })     
      output$table1 <- DT::renderDataTable({
      {df1() %>% datatable() %>%
          formatStyle(1, target = 'row', 
                      backgroundColor = styleEqual(c(seq(1, 99, 2),seq(2, 100, 2)), c(rep("#F5F5F5",50),rep("#FFFFFF",50))))}
      #options = list(lengthMenu = list(c(5, 15, -1), c('5', '15', 'All'),pageLength = -1))
    })
}
shinyApp(ui, server)

Then, I can display all the rows of the table as default:

library(shiny); library(DT)
    ui <- fluidPage(mainPanel(
      sliderInput("seed",
                  "Set seed",
                  min = 1,  max = 100,  value = 1, step = 1),
      mainPanel(
        tabsetPanel(
          tabPanel("Table", DT::dataTableOutput("table1"))
        ))))
    server <- function(input, output, session){
      df1 <- reactive({
        req(input$seed)
        a <- c(rep(1,3), rep(2,3), rep(3,3),rep(4,5))
        set.seed(input$seed)
        b <- c(runif(3),runif(3),runif(3),runif(5))
        df <- data.frame(a,b)
      })    
      output$table1 <- DT::renderDataTable(DT::datatable({df1()}, options = list(lengthMenu = list(c(5, 15, -1), c('5', '15', 'All')),pageLength = -1)))
    
    }
    shinyApp(ui, server)

My question is: how to combine those two features? I.e. I would like to display all the rows as default and have different colours of rows, depending on value of column "a".

Thanks in advance!

Greg3er
  • 27
  • 6

1 Answers1

0

I have found the answer. I messed the position of options vs formatStyle parts:

  output$table1 <- DT::renderDataTable({
    DT::datatable(df1(),
    options=list(pageLength=-1,
                 lengthMenu = (list(c(5, 15, -1), c('5', '15', 'All')))
              )) %>%  # end of options
    formatStyle(1, target = 'row',backgroundColor = styleEqual(c(seq(1, 99, 2),seq(2, 100, 2)), c(rep("#F5F5F5",50),rep("#FFFFFF",50))))
 

 })

Have a good day Greg3er

Greg3er
  • 27
  • 6