2016-10-13 31 views
1

我試圖創建一個響應式閃亮的應用程序,可以在從數據表中選擇項目(雙字)時突出顯示文本。我有數據表選擇工作。我正在使用includeHTML()函數來包含並顯示文本文件。如何突出顯示由includeHTML顯示的文本

是否可以突出顯示所有從datable中選擇的項目的出現,在由includeHTML()顯示的文本中?

+0

這當然是可以的。一個可重複的例子會有所幫助。 – Carl

回答

3

如果你想這樣做任何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)