如何高亮includeHTML显示的文本

How to highlight text displayed by includeHTML

我正在尝试创建一个响应式闪亮应用程序,当我 select 来自数据表的项目(一对单词)时,它可以突出显示文本。我已经让数据表 selection 工作了。我正在使用 includeHTML() 函数来包含和显示文本文件。

是否可以在 includeHTML() 显示的文本中突出显示数据表中出现的所有项目 select?

如果您想对任意 HTML 文件执行此操作,这可能行不通,但这是一个纯 R 解决方案。 javascript 解决方案可能会更好:

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)