基于正则表达式高亮显示DT中的单词 [英] Highlight word in DT in shiny based on regex
问题描述
使用闪亮的DT,我希望能够突出显示所选单词。设置 searchHighlight = TRUE
接近我想要的设置,但这也会突出显示包含搜索的单词。例如,如果我搜索 on,它也将匹配 stone,突出显示中间的 on。
示例图像:
我可以优化搜索选项,使 regex = TRUE
,但是不会突出显示。例如,如果我想使用 on | in之类的正则表达式,也是如此。
示例(包括正则表达式):
library(shiny)
库(DT)
库(data.table)
example_data<-data.table(words = c( on, scone, wrong,石头),
描述= c(单词在,烤饼不在。,不在任何一个,根本不在这里))
ui = ShinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
textInput( word_select,label =要搜索的单词)
),
mainPanel(
dataTableOutput( word_searched)
)
)
))
服务器= ShinyServer(function(input,output,session){
output $ word_searched<-renderDataTable({
datatable(
example_data,
options = list(searchHighlight = TRUE,
search = list(regex = TRUE,
搜索= paste0( \\b,tolower(input $ word_select), \\b)))
)
})
}}
ShinyApp(ui = ui,server = server)
DT已通过反应性表达式过滤在单词上,因此所有字段必定会包含所选单词,但是我只是想避免用户误以为较长的单词被错误地包含在搜索中而引起混淆。我没有在示例中进行此操作,只是确认这不是我关注的元素。
感谢您的帮助。
(已编辑,以在示例数据中添加带标点的单词示例。)
您可以创建 reactive
元素,而不是依赖于数据表的搜索功能,该元素首先按输入进行过滤,然后将匹配的单词替换为相同的单词嵌入到< span style = background-color:yellow;>
标签中。
您需要将 escape = F
添加到 datatable
,因此可以正确解释HTML标记。我已经将 options = list(dom = lt)
添加到 datatable
来删除数据表的搜索字段并直接请注意左侧的搜索字段。
使过滤条件保持相当模糊,以防止表消失,直到找到完美的匹配为止–即,当出现以下情况时,表不应消失您输入 o,因为没有完美匹配,然后重新出现在 on上。然后,仅当找到匹配的单词时才会显示高亮,即 on
, On
和
,但不是 stone
, scone
等。这是它的一瞥看起来像:
这是代码。请注意,我使用dplyr的过滤和变异函数是因为它们可以通过其 * _ all
变量轻松地应用于多个列:
< pre class = lang-r prettyprint-override>
library(shiny)
库(DT)
库(data.table)
库(dplyr)#对于`filter_all`和`mutate_all`。
example_data<-iris
#data.table(words = c( on, scone,错误, stone),
#description = c(单词在,烤饼不在。,不在任何一个,根本不在这里))
ui = ShinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
textInput( word_select,label =要搜索的单词)
),
mainPanel(
dataTableOutput( word_searched)
)
)
))
服务器= ShinyServer(function(input,output,session){
#
df_reactive< -active({
example_data%>%
#过滤输入是否在任何地方,甚至换句话说。
filter_all(any_vars(anypls(grepl(input $ word_select,。,T,T)))%&%;%
#用HTML中的相同单词替换完整单词。
mutate_all(〜gsub(
过去e(c( \\b(,input $ word_select,)\\b),crash =),
< span style ='background-color:yellow; '> \\1< / span>,
。,
是,
是
)
)
})
#在此处渲染您的反应性元素。
output $ word_searched<-renderDataTable({
datatable(df_reactive(),escape = F,options = list(dom = lt))
})
})
ShinyApp(ui = ui,服务器=服务器)
Using DT in shiny, I want to be able to highlight the selected word. Setting searchHighlight = TRUE
is close to what I want, but this will also highlight words that include the search. For example, if I am searching for "on" it will also match "stone", highlighting the "on" in the middle.
EXAMPLE IMAGE:
I can refine the search options so regex = TRUE
, but then no highlighting occurs. This is also true if I want to use regex like "on|in", for example.
EXAMPLE (including regex):
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)
The DT is already being filtered on the word by a reactive expression, so all the fields will definitely include the selected word, but I just want to avoid confusion from users thinking that longer words are being included in the search erroneously. I haven't done this in the example but just confirming this is not the element I'm concerned about.
Thanks for your help.
(EDITED to add an example of a word with punctuation in the example data.)
Instead of relying on datatable's search functionality you can create a reactive
element that first filters by the input, and then replaces the matching words with the same word embedded in a <span style="background-color:yellow;">
tag. This should allow more search flexibility via more complex regex.
You'll need to add escape = F
to datatable
so the HTML tag is interpreted properly. I've added options = list(dom = "lt")
to datatable
to remove the datatable's search field and direct attention to the left search field.
The filtering criteria are left fairly fuzzy to keep the table from disappearing until a perfect match is found – i.e. the table shouldn't disappear when you type "o" because there's no perfect match, and then reappear at "on". The highlights then only appear if a matching word is found, i.e. on
, On
, and on.
, but not stone
, scone
, etc. Here's a glimpse of what it looks like:
And here's the code. Note that I use dplyr's filtering and mutating functions because they can easily be applied to multiple columns via their *_all
variants:
library(shiny)
library(DT)
library(data.table)
library(dplyr) # For `filter_all` and `mutate_all`.
example_data <- iris
# 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) {
# This is your reactive element.
df_reactive <- reactive({
example_data %>%
# Filter if input is anywhere, even in other words.
filter_all(any_vars(grepl(input$word_select, ., T, T))) %>%
# Replace complete words with same in HTML.
mutate_all(~ gsub(
paste(c("\\b(", input$word_select, ")\\b"), collapse = ""),
"<span style='background-color:yellow;'>\\1</span>",
.,
TRUE,
TRUE
)
)
})
# Render your reactive element here.
output$word_searched <- renderDataTable({
datatable(df_reactive(), escape = F, options = list(dom = "lt"))
})
})
shinyApp(ui = ui, server = server)
这篇关于基于正则表达式高亮显示DT中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!