【发布时间】:2019-10-31 04:24:35
【问题描述】:
在闪亮中使用 DT,我希望能够突出显示选定的单词。设置 searchHighlight = TRUE 接近我想要的,但这也会突出显示包含搜索的单词。例如,如果我搜索“on”,它也会匹配“stone”,突出显示中间的“on”。
示例图片:
我可以细化搜索选项,以便 regex = TRUE,但不会出现突出显示。例如,如果我想使用像“on|in”这样的正则表达式,这也是正确的。
示例(包括正则表达式):
library(shiny)
library(DT)
library(data.table)
example_data <- data.table(words = c("on", "scone", "wrong", "stone"),
description = c("The word on", "Scone is not on.", "Not on either", "Not here at all"))
ui = shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
textInput("word_select", label = "Word to search")
),
mainPanel(
dataTableOutput("word_searched")
)
)
))
server = shinyServer(function(input, output, session) {
output$word_searched <- renderDataTable({
datatable(
example_data,
options = list(searchHighlight = TRUE,
search = list(regex = TRUE,
search = paste0("\\b", tolower(input$word_select), "\\b")))
)
})
})
shinyApp(ui = ui, server = server)
DT 已经被反应式表达式过滤在单词上,所以所有字段肯定会包含所选单词,但我只是想避免用户认为更长的单词被错误地包含在搜索中而造成混淆。我在示例中没有这样做,但只是确认这不是我关心的元素。
感谢您的帮助。
(已编辑以在示例数据中添加带有标点符号的单词示例。)
【问题讨论】: