如何突出显示includeHTML显示的文本

时间:2016-10-13 03:14:37

标签: r shiny highlight dt

我正在尝试创建一个反应性的闪亮应用程序,当我从数据表中选择一个项目(一对单词)时,它可以突出显示文本。我有数据表选择工作。我正在使用includeHTML()函数来包含和显示文本文件。

是否可以在includeHTML()显示的文本中突出显示从数据中选择的所有项目?

1 个答案:

答案 0 :(得分:4)

如果您想对任意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)