Melissa Allen
Melissa Allen

Reputation: 103

R Shiny - What is the problem with my observe function ({UpdateSelectInput})?

I've looked on similar posts for my problem, but nothing is helping and I'm running out of patience.

I have a very simple app, where I'm trying to find all counties within a selected US state. I'm using SelectInput in the UI for users to select what state they want, and an UpdateSelectInput in the server so users can select what county they want from their state selection.

This is what my simplified data looks like:

**STATE_NAME      NAMELSAD**
Alabama            Clay County  
Alabama            Marengo County
Arkansas           Scott County

My code looks like this:

global.r

library(shiny)
library(leaflet)
library(sp)
library(rgdal)
library(htmltools)
library(DT)
library(ggplot2)
library(shinythemes)

path <- "C:/Users/user/Desktop/Countyapp/Countyapp/test_covid/"
setwd(path)

counties <- read.csv("us_counties1.csv")

UI.r

ui <- fluidPage(
        selectInput(inputId = "selectstate", label = "Select State", choices = (counties$STATE_NAME)),
        selectInput(inputId = "selectcounty", label =  "Select County", choices = NULL)
)

And finally, the server.R

server <- function(session, input, output) {

    observe({
        updateSelectInput(session, "selectcounty", "Select County", 
                          choices = counties$NAMELSAD[counties$STATE_NAME==input$STATE_NAME])
    })

}

shinyApp(ui = ui, server = [enter image description here][1]server) 

Basically, my first SelectInput works, you can choose whatever state you want. But the second selector is blank! Whyyyyyyy. There is something wrong with my observe function, but I've been sitting here forever and am out of possible solutions. Please help if you can! Thank you!

Upvotes: 1

Views: 1353

Answers (3)

Melissa Allen
Melissa Allen

Reputation: 103

Gosh, I'm stupid.

It's because I named the input wrong in my observe event.

got it working. Answer:

  observe({
        updateSelectInput(session, "selectcounty", "Select County", 
                          choices = counties$NAMELSAD[counties$STATE_NAME==input$selectstate])
    })

Upvotes: 0

Ian Campbell
Ian Campbell

Reputation: 24790

The purpose of the observe() function is to watch for a change in a reactive expression. However, updateSelectInput() is not a reactive expression as you have written it, because none of the expressions within it involve any reactive values or expressions.

Instead, you should observe() an input's value for changes, for example, like this:

observe({
 if(input$selectstate != ""){
 updateSelectInput(session, "selectcounty", "Select County", 
                          choices = counties$NAMELSAD[counties$STATE_NAME==input$selectstate,])
 }
else {
 updateSelectInput(session, "selectcounty", "Select County", 
                          choices = "Select State")
 }
})

The expression in observe is now reactive because input is reactive.

See this answer for an excellent discussion.

Upvotes: 1

Ash
Ash

Reputation: 1513

Instead of observe, try using observeEvent

There is also a typo, where input$STATE_NAME should be input$selectstate

Below is a minimal working example:

library(shiny)

#Create your dataframe (runs once when app starts)
counties <- data.frame(
    STATE_NAME = c('Alabama', 'Alabama', 'Arkansas'),
    NAMELSAD   = c('Clay County', 'Marengo County', 'Scott County'),
    stringsAsFactors = FALSE
)

#UI
ui <- fluidPage(

    selectInput(inputId = "selectstate",  label = "Select State",  choices = (counties$STATE_NAME)),
    selectInput(inputId = "selectcounty", label = "Select County", choices = NULL)

)

#Server
server <- function(input, output, session) {

    #Runs when input$selectstate changes
    observeEvent(input$selectstate, {

        updateSelectInput(session, inputId = "selectcounty", label = "Select County", choices = counties[counties$STATE_NAME == input$selectstate,]$NAMELSAD)

    })

}

shinyApp(ui, server)

Upvotes: 1

Related Questions