【发布时间】:2017-02-22 00:41:08
【问题描述】:
我正在尝试创建一个响应式闪亮应用程序,当我从数据表中选择一个项目(一对单词)时,它可以突出显示文本。我已经让数据表选择工作了。我正在使用includeHTML() 函数来包含和显示文本文件。
是否可以在includeHTML() 显示的文本中突出显示从数据表中选择的所有项目?
【问题讨论】:
-
这当然是可能的。一个可重现的例子会有所帮助。
我正在尝试创建一个响应式闪亮应用程序,当我从数据表中选择一个项目(一对单词)时,它可以突出显示文本。我已经让数据表选择工作了。我正在使用includeHTML() 函数来包含和显示文本文件。
是否可以在includeHTML() 显示的文本中突出显示从数据表中选择的所有项目?
【问题讨论】:
如果您想对任意 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)
【讨论】: