Вот полный отлаженный код приложения!
Сначала необходимые библиотеки:
library(shiny)
library(tidyverse)
library(DT)
library(magrittr)
Затем функция, добавляющая тег HTML:
wordHighlight <- function(SuspWord,colH = 'yellow') {
paste0('<span style="background-color:',colH,'">',SuspWord,'</span>')
}
СейчасЧасть пользовательского интерфейса:
ui <- fluidPage(
titlePanel("Text Highlighting"),
sidebarLayout(
sidebarPanel(
textInput("wordSearch", "Word Search")
),
mainPanel(
DT::dataTableOutput("table")
)
)
)
Наконец, на стороне сервера:
server <- function(input, output) {
sentence <- "The term 'data science' (originally used interchangeably with 'datalogy') has existed for over thirty years and was used initially as a substitute for computer science by Peter Naur in 1960."
sentence2 = "One of the things we will want to do most often for social science analyses of text data is generate a document-term matrix."
YourData = data.frame(N = c('001','002'), T = c(sentence,sentence2), stringsAsFactors=FALSE)
highlightData <- reactive({
if (input$wordSearch!="")
{
patterns = input$wordSearch
YourData2 = YourData
YourData2[,2] %<>% str_replace_all(regex(patterns, ignore_case = TRUE), wordHighlight)
return(YourData2)
}
return(YourData)
})
output$table <- DT::renderDataTable({
data <- highlightData()
}, escape = FALSE)
}
Запустите приложение:
shinyApp(ui = ui, server = server)