如何将下拉值作为输入传递给Shiny中的过滤器函数

时间:2018-04-19 11:46:05

标签: r shiny dplyr

  • 意图:显示由用户控制的表格。
  • 输入:钻石的净度类型。
  • 输入源:DropDown菜单。

    library(shiny)
    library(ggplot2)
    require(scales)
    library(dplyr)
    
    
    ui <- fluidPage(
      sidebarLayout(
    
        sidebarPanel(
    
          selectInput(inputId = "clarity", 
                      label = "choose a clarity in Diamonds", 
                      choices = c("IF", "VVS1", "VVS2","VS1", "VS2", "SI1","SI2"," I1"," Please Select a Type "),
                      selected = " Please Select a Type " ),
          actionButton(inputId = "action1", label = "action1")
    
        ),
        mainPanel(
    
          tableOutput("view")
    
        ))
    
    )
    
    server <- function(input, output){
    
      #"IF", "VVS1", "VVS2","VS1", "VS2", "SI1","SI2"," I1"," Please Select a Type ")
    
      datasetInput <- reactive({ 
        switch(input$clarity, 
               "IF" = IF, 
               "VVS1" = VVSI, 
               "VVS2" = VVS2,
               "VS1" = VS1, 
               "VS2" = VS2, 
               "SI1" = SI1,
               "SI2" = SI2, 
               "I1" = I1, 
               " Please Select a Type " = NULL) 
      }) 
      output$view <- eventReactive(input$action1,{ 
        filter(diamonds, diamonds$clarity == datasetInput()) 
      }) 
    
    }
    
    shinyApp(ui = ui , server = server)
    

上面的代码给出了一个错误,上面写着

Listening on http://127.0.0.1:4090
Warning: Error in filter_impl: Evaluation error: object 'VVS2' not found.
Stack trace (innermost first):
    103: <Anonymous>
    102: stop
    101: filter_impl
    100: filter.tbl_df
     99: filter
     98: eventReactiveHandler [#21]
     78: output$view
      4: <Anonymous>
      3: do.call
      2: print.shiny.appobj
      1: <Promise>

根据我的分析,当我尝试在单独的脚本中运行代码时,它正常工作,因为我在双反转逗号(即“IF”)内传递过滤条件,因为我无法传递相同的内容格式,而我在我的闪亮应用程序中传递条件,因为我正在调用函数。

请帮助。我想我错过了一件小事

1 个答案:

答案 0 :(得分:1)

我不知道你想要达到的目标。你在哪里定义IF例如?这是你想要做的吗?

server <- function(input, output){

  observeEvent(input$action1, { 
    output$view <- renderTable({
      filter(diamonds, diamonds$clarity == input$clarity)
    }) 
  }) 

}