【问题标题】:How to highlight text displayed by includeHTML如何高亮 includeHTML 显示的文本
【发布时间】:2017-02-22 00:41:08
【问题描述】:

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

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

【问题讨论】:

  • 这当然是可能的。一个可重现的例子会有所帮助。

标签: r shiny highlight dt


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 2011-12-13
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多