1
votes

I want to change the colour of rows in a DT when they are selected in my app, from the default blue. I've used this answer and it works, but for some reason it shows the text of the style tag in the app.

Example of style tag showing

I've tried changing to fluidPage() instead of navbarPage() and that gets rid of the issue - but I want to use navbarPage for my navigation tabs!

I assume I'm doing something stupid - any ideas?

EXAMPLE CODE:

library(shiny)
library(DT)
library(data.table)

example_data <- data.table(words = c("on", "scone", "wrong", "stone"), 
                           description = c("The word on", "Scone is not on", "Not on either", "Not here at all"))

ui = shinyUI(navbarPage(tags$style(HTML('table.dataTable tr.selected td, table.dataTable td.selected {background-color: cornsilk !important;}')),
                        title = "Random example",

    fluidRow(
      dataTableOutput("word_searched")
    )
  )
)


server = shinyServer(function(input, output, session) {

  output$word_searched <- renderDataTable({
    datatable(example_data)
  })

  })

shinyApp(ui = ui, server = server)

1
This article may be helpful: shiny.rstudio.com/articles/css.htmlJoris C.
Thanks, I'm aware of the article and have used it before but the below answer related to my specific issue.Jaccar

1 Answers

2
votes

Just put the style$tag part within fluidRow and it will work. You don't need the HTML function either in style tag:

library(shiny)
library(DT)
library(data.table)

example_data <- data.table(words = c("on", "scone", "wrong", "stone"), 
                           description = c("The word on", "Scone is not on", "Not on either", "Not here at all"))

ui = shinyUI(navbarPage(
  title = "Random example",
      fluidRow(
        tags$style('table.dataTable tr.selected td, table.dataTable td.selected {background-color: cornsilk !important;}'),
        dataTableOutput("word_searched")
      )
)
)


server = shinyServer(function(input, output, session) {

  output$word_searched <- renderDataTable({
    datatable(example_data)
  })

})

shinyApp(ui = ui, server = server)

Out:

enter image description here