R Shiny中的Searchbox [英] Searchbox in R Shiny

查看:159
本文介绍了R Shiny中的Searchbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为用户添加一个常规搜索框,以便在Shiny的输出小部件中找到字符串?在下面的示例中,我希望用户在textInput小部件中键入一个字符串,并让Shiny突出显示verbatimTextOutput中的匹配文本(或类似内容):

It is possible to add a general search box for the user to find a string in an output widget in Shiny? In the example below, I would like the user to type a string in the textInput widget and have Shiny highlight the matching text in the verbatimTextOutput (or something similar):

library(shiny)

text <- "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce nec quam ut tortor interdum pulvinar id vitae magna. Curabitur commodo consequat arcu et lacinia. Proin at diam vitae lectus dignissim auctor nec dictum lectus. Fusce venenatis eros congue velit feugiat, ac aliquam ipsum gravida. Cras bibendum malesuada est in tempus. Suspendisse tincidunt, nisi non finibus consequat, ex nisl condimentum orci, et dignissim neque est vitae nulla." 

ui <- fluidPage(
    sidebarPanel(
      textInput("search", "", placeholder = "Search term") 
      ),
      verbatimTextOutput("text")
  )
)


server <- function(input, output) {

  output$text <- renderText(paste(text))
}

shinyApp(ui = ui, server = server)

到目前为止,我一直在通过将文本拆分为固定长度的行并使用grep在文本中显示字符串的位置来解决此问题. (例如,警告用户字符串lorem在第一行中).

So far, I have been working around this problem by splitting the text in fixed-length rows and using grep to display the location of the string in the text. (For example, alerting the user that the string lorem is in the first line).

可以通过某种方式更直观地完成吗?

Can it somehow be done more intuitively?

修改

@Aurèle的答案是正确的. DT::dataTableOutput 还提供了一个搜索框功能,用于在data.tables中查找字符串,而无需炫耀.

@Aurèle's answer is spot on. DT::dataTableOutput also provides a searchbox feature for finding strings in data.tables, without the higlighting.

推荐答案

这是我的幼稚尝试(它满足更直观的要求吗?):

Here is my naive attempt (does it satisfy the requirement of it being more intuitive?):

library(shiny)
library(stringr)
library(purrr)

text <- paste(
  "Lorem ipsum dolor sit amet,",
  "consectetur adipiscing elit. Fusce nec quam ut tortor", 
  "interdum pulvinar id vitae magna.", 
  "Curabitur commodo consequat arcu et lacinia.", 
  "Proin at diam vitae lectus dignissim auctor nec dictum lectus.", 
  "Fusce venenatis eros congue velit feugiat,", 
  "ac aliquam ipsum gravida. Cras bibendum malesuada est in tempus.", 
  "Suspendisse tincidunt, nisi non finibus consequat, ex nisl", 
  "condimentum orci, et dignissim neque est vitae nulla."
)
insert_mark_tag <- function(s, loc_index, all_locs) {
  str_sub(s, all_locs[loc_index, 2] + 1, all_locs[loc_index, 2]) <- "</mark>"
  str_sub(s, all_locs[loc_index, 1], all_locs[loc_index, 1] - 1) <- "<mark>"
  s
}
ui <- fluidPage(
  sidebarPanel(
    textInput("search", "", placeholder = "Search term") 
  ),
  htmlOutput("text")
)
server <- function(input, output) {
  output$text <- renderText({
    m <- if (nchar(input$search)) 
      str_locate_all(text, fixed(input$search))[[1]] else 
        matrix(ncol = 2)[FALSE, ]
    HTML(reduce_right(seq_len(nrow(m)), insert_mark_tag, all_locs = m, .init = text))
  })
}
shinyApp(ui = ui, server = server)

键是str_locate_all()str_sub<-.

(您可能希望使用coll()而不是fixed(),并且可能将stringr替换为stringi,我不知道性能影响是否可以测量).

(you might want to use coll() instead of fixed(), and maybe replace stringr with stringi, I have no idea if the performance impact would be measurable).

我使用@bartektartanus'(stringi的合著者)回答此处,顺便说一句,我在评论中问是否有比此天真reduce()更干净的方法.

I used @bartektartanus' (co-author of stringi) answer here, btw I asked in a comment whether there is a cleaner way than this naive reduce().

修改

实际上,我不知道为什么让它变得如此复杂. (非常)简单(尽管它的行为与正则表达式有所不同):

Actually, I have no idea why I made it so complicated. This is (much) simpler (though it behaves a little differently wrt regexes):

ui <- fluidPage(
  sidebarPanel(
    textInput("search", "", placeholder = "Search term") 
  ),
  htmlOutput("text")
)
server <- function(input, output) {
  output$text <- renderText(HTML(
    if (nchar(input$search))
      str_replace_all(text, sprintf("(%s)", input$search), "<mark>\\1</mark>") else
        text
  ))
}
shinyApp(ui = ui, server = server)

这篇关于R Shiny中的Searchbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆