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)