2016-10-13 5 views
1

데이터 테이블에서 항목 (단어 쌍)을 선택할 때 텍스트를 강조 표시 할 수있는 반응적인 반짝이는 응용 프로그램을 만들려고합니다. 데이터 테이블 선택 작업이 있습니다. 나는 includeHTML() 함수를 사용하여 텍스트 파일을 포함하고 표시합니다.includeHTML에 의해 표시된 텍스트를 강조 표시하는 방법

includeHTML()에 표시된 텍스트에서 데이터 테이블에서 선택한 항목의 모든 항목을 강조 표시 할 수 있습니까?

+0

확실히 가능합니다. 재생산 가능한 예제가 도움이 될 것입니다. – Carl

답변

3

임의의 HTML 파일에 대해이 작업을 수행하려는 경우이 방법은 효과가 없을 수 있지만 여기에는 순수한 R 솔루션이 있습니다. 자바 스크립트 솔루션을 사용하면 더 편리 할 것입니다.

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) 
관련 문제