Apply transformations on reactive dataset (shiny)

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 :slight_smile:

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)

Hello,

It can take indeed a while to get used to reactive data and how to manipulate it. Your code was nearly perfect for just two little mistakes

  • Your variable cars1 was declared with a small c, but when you called it in the first reactive statement you used capital C
  • When you call any reactive value, you need to call it as a function, although in reality is is a variable (or dataset). Just adding () behind the reactive variable solved the issue. Ex: Cars_dataset once declared is called as Cars_dataset()
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)

Hope this helps,
PJ

Awesome, thank you so much. This solved the issue.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.