rshiny

Issue with updating input values


I am trying to change the choices of a updateSelectizeInput on server.R depending on the values a user entered on a textInput on ui.R.

Extract from server.R:

Update ZIPCode with NewZIP entered by user

isolate({
  if (input$NewZIP != "") {
    ZIPCode<-reactive(input$NewZIP)
  }
})

Fetch file associated with ZIPCode and update the choices of a dependent pull down list

ZIPFile <- read.csv(paste0("./data/",ZIPCode), sep="")
updateSelectizeInput(session, 'MedicalProcedure', server = T, 
                     choices=as.character(ZIPFile$a_description))

This doesn't work and I can't figure out where I did something wrong.

Can somebody help?


Solution

  • isolate is supposed to be used inside a reactive: it makes the reactive 'insensitive' to input changes inside. So you should try something like this:

    ZIPCode <- reactive({
        if(input$NewZip == "") return(NULL)
        input$NewZip
    })
    
    ZIPFile <- reactive({
        if(is.null(ZIPCode())) return(NULL)
        read.csv(paste0("./data/", ZIPCode()), sep="")
    )}
    
    observeEvent(ZIPFile(), {
        updateSelectizeInput(session, 'MedicalProcedure', server = T, 
                             choices = as.character(ZIPFile()$a_description))
    }, ignoreNULL = TRUE)