selectInput:具有Multiple = TRUE并基于此过滤掉

时间:2017-02-15 22:43:06

标签: r widget shiny

我有一个闪亮的应用程序如下:

我的目标是能够根据selectInput ui功能从Type列中过滤多个值。我做的时候

selectInput(inputId, label, choices, multiple = TRUE),过滤无效,因为它预计有1个响应。

以下是工作版本,但我无法弄清楚如何在SelectInput中使用多功能。

我目前正在使用

test1<-filter(test, `Type`==input$file4)

当我将此扩展程序添加到multiple=TRUE

时,这不起作用

服务器

library(shiny)
library(readr)
library(dplyr)
library(DT)

actor <- c('Matt Damon','George Clooney','Brad Pitt', 'Clive Owen', 'Morgan Freeman', 'Edward Norton', 'Adrian Granier')
category<-c('action', 'action', 'noir', 'action', 'thriller', 'noir', 'action')
movie <- c('Oceans Eleven', 'Oceans Twelve', 'Fight Club', 'Children of Men', 'The Shawshank Redemption', 'American History X', 'Entourage')
movies <- c(21, 23, 26, 12, 90, 14, 1)
cost <- c(210000, 2300000, 260000, 120000, 90000, 140000, 10000)
Type <- c('A','B','C', 'A', 'B', 'C', 'A')

moviedata<-data.frame(actor, category, movie, movies, cost, Type)

shinyServer(function(input,output){
  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()} 
    read_csv(file=file1$datapath)

  })


    output$sum <- renderDataTable({
    if(is.null(data())){return ()}
    test<-subset(moviedata, category %in% data()[[1]])
    test1<-filter(test, `Type`==input$file4)
    test1$`BUDGET`<-input$file5
    test1$CHECKING<-ifelse(test1$`BUDGET`>test1$cost,"YES", "NO")
    filter(test1, CHECKING=="YES")
  })

  output$tb <- renderUI({
    if(is.null(data()))
      h5("Powered by", tags$img(src='optimatic.png'))
    else
      tabsetPanel(tabPanel("Summary", dataTableOutput("sum")))
   })

} 
)

UI

library(shiny)
shinyUI(fluidPage(
  titlePanel("Actor Finder"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file","Upload Category List: Must have category as header"),
      selectInput("file4", "Select Type", c("A" = "A",
                                            "B" = "B",
                                            "C" = "C"), selected = "A",
                                            multiple=FALSE),
      numericInput("file5", "Choose cost", 1000000000),
      tags$hr()),    
    mainPanel(
      uiOutput("tb")
    )

  )
))

任何帮助都会很棒,谢谢!

1 个答案:

答案 0 :(得分:2)

在对多项选择进行过滤时,您不能使用==,因为其中有一系列选择的值。您可以改用%in%

test1<-filter(test, Type %in% input$file4)

要在NA中包含缺失值,可以在file4中添加“缺少”选项,然后:

test1<-filter(test, Type %in% input$file4 | 
                    (is.na(Type) & "Missing" %in% input$file4 ))