Imran Ali
Imran Ali

Reputation: 2279

How to highlight text displayed by includeHTML

I am trying to create a reactive shiny app that can highlight text when I select an item(pair of words) from a datatable. I have got the datatable selection working. I am using includeHTML() function to include and display the text file.

Is it possible to highlight all occurrence of the item selected from the datable, in the text displayed by includeHTML()?

Upvotes: 2

Views: 1088

Answers (1)

Carl
Carl

Reputation: 5779

If you want to do this for any arbitrary HTML file this probably won't work, but here is a pure R solution. You are probably better off with a javascript solution:

library(shiny)
library(DT)

ui <- shinyUI(fluidPage(mainPanel(
  DT::dataTableOutput("test"),
  htmlOutput("html")
)))

server <- shinyServer(function(input, output, session) {
  words <- data.frame(stringsAsFactors = FALSE,
                      words = c("the", "hello", "world"))
  output$test <- DT::renderDataTable({
    words
  }, selection = list(mode = "single", target = "row"))

  text <- "This is the hello world example for this problem."

  output$html <- renderUI({
    if (is.null(input$test_rows_selected))
      return(HTML(text))

    HTML(gsub(
      words$words[input$test_rows_selected],
      paste0("<mark>",
             words$words[input$test_rows_selected],
             "</mark>"),text ))
  })
})

shinyApp(ui = ui, server = server)

Upvotes: 5

Related Questions