1

In my shiny app I display a table (reactable) with an expandable row. I would like to change the background color for certain words, therefor I use html spans. It works fine for the text in the regular row, in the expandable row however only the plain html code is displayed.

enter image description here

I set html = TRUE for both columns yet is not displayed correctly. How do I make it work?

app.R

library(shiny)
library(htmltools)
library(reactable)

ui <- fluidPage(
  reactableOutput("table")
)
server <- function(input, output) {
  
  output$table <- renderReactable({
    df = data.frame("title" = c("This is the <span style='background-color:yellow;'>Title</span>", "This is a longer Title"), 
                    "abstract" = c("This is the <span style='background-color:yellow;'>abstract</span>", "This is an even longer abstract"))
    reactable(
      df,
      columns = list(
        abstract = colDef(show = F, html = TRUE),
        title = colDef( html = TRUE)
      ),
      details = function(index) {
        htmltools::div(style= "background-color:white",
                       htmltools::tags$div(style= "background-color:#eee; padding: .9em; border-color: #ffe;", df[index, "abstract"])
                       
        )
      }
    )
  })
  
}
volfi
  • 435
  • 3
  • 11

1 Answers1

1

Using the html function from here we can do -

library(shiny)
library(htmltools)
library(reactable)


html <- function(x, inline = FALSE) {
  container <- if (inline) htmltools::span else htmltools::div
  container(dangerouslySetInnerHTML = list("__html" = x))
}

ui <- fluidPage(
  reactableOutput("table")
)
server <- function(input, output) {
  
  output$table <- renderReactable({
    df = data.frame("title" = c("This is the <span style='background-color:yellow;'>Title</span>", "This is a longer Title"), 
                    "abstract" = c("This is the <span style='background-color:yellow;'>abstract</span>", "This is an even longer abstract"))
    reactable(
      df,
      columns = list(
        abstract = colDef(show = F, html = TRUE),
        title = colDef( html = TRUE)
      ),
      details = function(index) {
        htmltools::div(style= "background-color:white",
                       htmltools::tags$div(style= "background-color:#eee; padding: .9em; border-color: #ffe;", 
                                           html(df[index, "abstract"]))
                       
        )
      }
    )
  })
}

shinyApp(ui,server)

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213