Hi,
I am trying to create a shiny dashboard, where the user can select one of various different datasets (note: these datasets have exactly the same format, ie same data for different sites - represented by cars1 and cars2 in my reprex).
Once the dataset is selected, the user can select further subsets of the selected dataset (represented in my reprex by the option to filter on amount of cylinders). The filtered data is then used for the output.
However, I am find that I cannot apply "filter" on the reactive dataset (this gives an error). I am still getting used to the "reactive" and "observe" concepts, but I suspect that I will need to apply these concepts for the solution...
Any help much appreciated
library(shiny)
library(tidyverse)
cars1 <- head(mtcars)
cars2 <- tail(mtcars)
ui <- fluidPage(
selectInput("dataset",
"Choose dataset",
choices = c("Cars1", "Cars2")),
selectInput("cyl",
"Choose amount of cylinders",
choices = c("4", "6", "8")),
tableOutput("cars_table")
)
server <- function(input,output,session) {
Cars_dataset <- reactive({
switch(input$dataset,
"Cars1" = Cars1,
"Cars2" = Cars2
)
})
data <- reactive ({
if(input$cyl == "4") {
Cars_dataset %>% filter(cyl == 4)
}
else if(input$cyl == "6") {
Cars_dataset %>% filter(cyl == 6)
}
else {Cars_dataset %>% filter(cyl == 8)}
})
output$cars_table <- renderTable({
data
})
}
shinyApp(ui, server)