0

Thanks for the time.

I am trying to get a shiny app to work correctly, and for some reason I am having difficulty on a highlighting issue when rendering a DT data table.

For example, this works:

output$DT = DT::renderDataTable({DT = datatable(DT,options = list(searching = FALSE,paging = FALSE,lengthChange = FALSE,ordering = FALSE,rownames= FALSE)) %>% 
      formatStyle('TEST',backgroundColor = styleEqual(c(1,2,3,4,5), c('chartreuse', 'chartreuse4','yellow','indianred','indianred4'))) )})

However, when attempting to add this additional line, the highlighting is not appearing, yet the code runs:

%>% 
      formatStyle('TEST2',backgroundColor = styleEqual(c(TRUE,FALSE),c('green','red'))

I have also tried styleInterval, and am getting the same results.

Thanks.

Reproducible Code:

ui <- fluidPage(

    dataTableOutput('DF')

  )



server <- function(input, output, session) {
  DF = as.data.frame(matrix(NA,nrow=2,ncol = 2))
  DF$V1 = c(TRUE,FALSE)
  DF$V2 = c(1,2)

  output$DF = renderDataTable(DF)

  output$DF = DT::renderDataTable({DF = datatable(DF,options = list(searching = FALSE,paging = FALSE,lengthChange = FALSE,ordering = FALSE,rownames= FALSE)) %>% 
    formatStyle('V2',backgroundColor = styleEqual(c(1,2,3,4,5), c('chartreuse', 'blue','yellow','indianred','indianred4'))) %>% 
    formatStyle('V1',backgroundColor = styleEqual(c(TRUE,FALSE),c('green','red')))})

}


shinyApp(ui = ui, server = server) # RUN THE APPLICATION 
  • If you include a fully reproducible example plus some data, it would be much easier to help you. – SeGa Feb 22 '19 at 14:28
  • The code I am working with is very,very long (1000+ lines of code), basically this section contains a dataframe with two variables, one TRUE/FALSE (let's keep it simple, and say that there is no order) and the other contains values 1-5 (same idea). – Brendan Newell Feb 22 '19 at 14:38
  • 1
    Even if the code or data are big, you can always create a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with fake data or data that's commonly available in base packages. Not that I edited because this is an issue with styling a `DT` table, not Shiny itself. – camille Feb 22 '19 at 14:40
  • Ok I will do this and get back to you guys. – Brendan Newell Feb 22 '19 at 14:50
  • Just added. Sorry it took so long, still a little new to stackOverflow! – Brendan Newell Feb 22 '19 at 15:01

1 Answers1

0
> styleEqual(c(TRUE,FALSE),c('green','red'))
[1] "value == 'TRUE' ? 'green' : value == 'FALSE' ? 'red' : ''"
attr(,"class")
[1] "JS_EVAL"

value is not 'TRUE' or 'FALSE', it is true or false.

You can do:

...... %>% 
  formatStyle('V1', 
    backgroundColor = JS("value == true ? 'green' : value == false ? 'red' : ''"))
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225