Search code examples
shinyshinydashboardshiny-servershiny-reactivity

Errors when trying to apply conditional filters


I have the following data for which I am trying to make a set of dropdown filters that will alter the displayed dataframe according to what was selected in the filters:

myData<- structure(list(waterbody = c("Homer", "Homer", "Homer", "Homer", 
"West", "West", "West", "East", "East", "East", "East", "Walnut", 
"Walnut", "Walnut", "Walnut"), transect_number = c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), species = c("LMB", 
"LMB", "GZS", "BLG", "LMB", "GZS", "BLG", "BLG", "BLG", "GZS", 
"BLG", "BLG", "LMB", "GZS", "LMB"), length_mm = c(430L, 430L, 
NA, 165L, 345L, NA, 128L, 117L, 93L, NA, 135L, 161L, 402L, NA, 
347L), wt_g = c(1270L, 1325L, NA, 108L, 545L, NA, 40L, 28L, 15L, 
NA, 42L, 81L, 865L, NA, 525L)), row.names = c(NA, -15L), class = "data.frame")

Additionally, the options in each subsequent filter should change depending on what was selected in the previous one. So the species availbale to select would change depending on what waterbodies are selected (because not all of them occur in each water).

My code so far:

# User Interface
ui <- shinyUI(
  fluidPage(
    titlePanel("File Inputs"),
    sidebarLayout(
      sidebarPanel(
        fileInput(
          'data',
          'Choose CSV File',
          accept = c('text/csv',
                     'text/comma-separated-values,text/plain',
                     '.csv')
        ),
        uiOutput("pick_waterbody"),
        uiOutput("pick_species")
      ),
      mainPanel(DT::dataTableOutput('dt_table'),)
    )
  )
)
  
  
  
# Server
server <- function(input, output, session) {
    myData <- reactive({
      inFile <- input$data
      if (is.null(inFile))
        return(NULL)
      data <- read.csv(inFile$datapath, header = T)
      data
    })
    
    output$dt_table <- DT::renderDataTable({
      DT::datatable(myData())
    })

    
    output$pick_waterbody <- rednderUI({
      pickerInput(
        "pick_waterbody",
        "Water(s) of Interest:",
        choices = unique(myData$waterbody),
        options = list(`actions-box` = TRUE),
      )}
    )
    
    output$pick_species <- renderUI({
      updatePickerInput(
        "pick_species",
        "Species of Interest:",
        choices = as.character(myData[myData$waterbody == input$pick_waterbody, "species"]),
        options = list(`actions-box` = TRUE))
    })
}

When closing the server code after the output$dt_table, the app works fine. But when I include the output$pick_waterbody code I get a "could not find function renderUI" error. Additionally, the output$pick_species code says there are unexpected symbols found. I've combed through it what feels like 1,000 times and I can't seem to figure out these issues.

Would very much appreciate some help in figuring out where I'm going wrong.


Solution

  • You had a typo, needed req(), and not updatePickerInput() just pickerInput. Try this

     output$pick_waterbody <- renderUI({
        req(myData())
        pickerInput(
          "pick_waterbody",
          "Water(s) of Interest:",
          choices = unique(myData()$waterbody),
          options = list(`actions-box` = TRUE),
        )}
      )
      
      output$pick_species <- renderUI({
        req(myData(),input$pick_waterbody)
        pickerInput(
          "pick_species",
          "Species of Interest:",
          choices = as.character(myData()[myData()$waterbody == input$pick_waterbody, "species"]),
          options = list(`actions-box` = TRUE))
      })