0
votes

I need to know how to filter from a dataset using the filter() function according to input values selected on ConditionalPanel(). At the moment, I can achieve this by filtering just one of the values on the reactive data. However, when I complete naming filters for all the values on the dataset I got a single graph as output, no matter what options are selected on the conditionalPanels.

Here's the 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")
         )))

Here is the Server

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

1 Answers

0
votes

Your if statement is the problem. The mazda input doesn't go away from the server just because the UI did - so the mazda data persists (I've added a table output of the UI inputs to demonstrate). You need to incorporate the input$brand to filter:

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)