如何使用闪亮的dplyr过滤来自ConditionalPanel()的反应数据?

时间:2018-01-11 21:18:03

标签: r shiny dplyr reactive

我需要知道如何根据在ConditionalPanel()上选择的输入值使用filter()函数从数据集中过滤。目前,我可以通过仅过滤反应数据中的一个值来实现此目的。但是,当我为数据集上的所有值完成命名过滤器时,无论在conditionalPanels上选择了哪些选项,我都会得到一个图表作为输出。

这是UI

library(shiny)

ui <- fluidPage(
  sidebarLayout(sidebarPanel(selectInput('brand', h4("Select brand"),
       choices=c("Mazda", "Other")),
          conditionalPanel(condition = "input.brand=='Mazda'",
          selectInput("mazda", h4("Model"),
          choices=c("RX4", "RX4 Wag"))),
          conditionalPanel(condition = "input.brand=='Other'",
          selectInput("other", h4("Model"),
choices=c("Toyota", "Other")))),

mainPanel(

plotOutput("graph")
         )))

这是服务器

server <- function(input, output) {

library(ggplot2)
library(dplyr)

#Here I adapt the mtcars data frame so the first column gives me the brand name#

mtcars2 <- tibble::rownames_to_column(mtcars)
colnames(mtcars2)[1] <- "brand"

#Done! now on to define reactive data#

 plotdata <- reactive ({

    if(input$mazda == "RX4") {
  filter(mtcars2, brand=="Mazda RX4")}
    else 
    if(input$mazda == "RX4 Wag") {
  filter(mtcars2, brand=="Mazda RX4 Wag")}
    else
    if(input$other == "Toyota") {
  filter(mtcars2, brand=="Toyota Corolla")}  
    else
    if(input$other == "Other") {
      filter(mtcars2, brand!="RX4")}
    })

 output$graph <- renderPlot({

datos <- plotdata()

ggplot(datos, aes(cyl, drat)) + 
  geom_col()
    })}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

您的if语句是问题所在。马自达输入并没有因为用户界面而离开服务器 - 所以马自达数据仍然存在(我已经添加了UI输入的表输出来演示)。您需要将input$brand合并到过滤器:

library(shiny)

ui <- fluidPage(
  sidebarLayout(sidebarPanel(selectInput('brand', h4("Select brand"),
                                         choices=c("Mazda", "Other")),
                             conditionalPanel(condition = "input.brand=='Mazda'",
                                              selectInput("mazda", h4("Model"),
                                                          choices=c("RX4", "RX4 Wag"))),
                             conditionalPanel(condition = "input.brand=='Other'",
                                              selectInput("other", h4("Model"),
                                                          choices=c("Toyota", "Other")))),

                mainPanel(

                  plotOutput("graph"),
                  tableOutput("table")
                )))

server <- function(input, output) {

  library(ggplot2)
  library(dplyr)

  #Here I adapt the mtcars data frame so the first column gives me the brand name#

  mtcars2 <- tibble::rownames_to_column(mtcars)
  colnames(mtcars2)[1] <- "brand"

  #Done! now on to define reactive data#

  plotdata <- reactive ({
    if(input$brand == "Mazda"){if(input$mazda == "RX4") {
      filter(mtcars2, brand=="Mazda RX4")
    }else if(input$mazda == "RX4 Wag") {
      filter(mtcars2, brand=="Mazda RX4 Wag")} 
         }else if(input$other == "Toyota") {
        filter(mtcars2, brand=="Toyota Corolla")
           } else if(input$other == "Other") {
        filter(mtcars2, brand!="RX4")}
  })

  output$graph <- renderPlot({

    datos <- plotdata()

    ggplot(datos, aes(cyl, drat)) + 
      geom_col()
  })
  output$table <- renderTable({

   c(input$brand,input$mazda, input$other)

  })
  }

shinyApp(ui = ui, server = server)