【问题标题】:TextInput filter in R dataset ShinyR数据集中的TextInput过滤器闪亮
【发布时间】:2018-05-23 00:12:56
【问题描述】:

我正在基于“电影”数据集在 Shiny 中创建可视化。在数据集中,除其他外,每部电影都有一个属性 plot_keywords (格式为谋杀|犯罪|警察|男人|侦探 - 5 个单词,用 | 分隔,不带空格)。我想为此属性实现一个交互式过滤器,而不考虑大写/大写字母 - 也就是说,例如,当您输入“Murder”时,Shiny 应该显示在 plot_keywords 属性的任何部分存在“murder”的所有电影。在我的代码中,如果用户没有在过滤器框中输入任何内容(默认情况下),则会显示所有电影。在'else'之后我应该使用什么功能?

部分UI代码

ui <- fluidPage(
fluidRow(
column(3,
    wellPanel(
      textInput("plot", "I want to watch movie about...",NULL)
      )),

部分服务器代码

server <- function(input, output) {
p <- input$plot
m <- movies %>%
filter(
  if(p != NULL) && (p != "")
    {plot_keywords == movies$plot_keywords}
  else
)`

【问题讨论】:

  • 这个here有几个答案

标签: r regex filter shiny


【解决方案1】:

一个想法如下所示。这也允许用户输入多个单词进行搜索。即当用户输入CrocODile Ii时,电影Crocodile Dundee II也会出现。我们使用的grepl 函数有一个参数ignore.case 以确保我们的查询不区分大小写。

希望这会有所帮助!



library(shiny)
library(ggplot2movies)
library(dplyr)

ui <- fluidPage(
  textInput("plot", "I want to watch movie about...",NULL),
  dataTableOutput('my_data')
)

server <- function(input, output) {
  output$my_data <- renderDataTable({
    p <- input$plot
    if(p != '')
      movies %>% filter(Reduce(`&`, lapply(strsplit(p,' ')[[1]], grepl, title,ignore.case=T)))
    else
      movies
  })
}

shinyApp(ui,server)

【讨论】:

  • 谢谢弗洛里安,您的回答帮助很大。它有效!
  • @JohnTown 很高兴我能帮上忙。哦,顺便说一句,欢迎来到 StackOverflow! ;)
猜你喜欢
  • 2015-03-09
  • 1970-01-01
  • 1970-01-01
  • 2016-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-22
相关资源
最近更新 更多