Problem: an updateSelectizeInput
call within observeEvent
changes the value displayed in the browser but does not change the value I can access from code with input$
Background: In my Shiny app, I want to have an input with the following properties:
- the user can type in and select from server-side list of options
- a URL query string can also control it
- blank means all options
- an actionButton allows the user to delay expensive computation until all desired choices have been made
I currently have an observeEvent
watching for the query string, and when it sees one, it calls updateSelectizeInput
, but, after it does so, the input is unchanged.
Example:
library(shiny)
possibleLetters = LETTERS[1:10]
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectizeInput(inputId='letters',
label='Your letters:',
choices=NULL,
selected=NULL,
multiple=T,
width='100%'),
actionButton("recompute",
"Recompute now")
),
mainPanel(
h3("Letters: "),
textOutput('lettersDisplay'),
h3("Indices of letters: "),
textOutput('lettersIndicesDisplay')
)
)
)
server <- function(input, output, session) {
updateSelectizeInput(inputId='letters',
choices=c('',possibleLetters),
server=T)
userLetters = eventReactive(input$recompute, ignoreNULL=FALSE, {
if (length(input$letters) == 0) {
return (possibleLetters)
} else (
return (input$letters)
)
})
userLetterIndices = reactive({
return(match(userLetters(),LETTERS))
})
queryStringObserver = observeEvent(session$clientData$url_search, {
query <- parseQueryString(session$clientData$url_search)
if (!is.null(query$letters)) {
cat(file=stderr(), paste0('observeEvent running - user specified param is: ',query$letters,'\n'))
updateSelectizeInput(session,
inputId="letters",
choices = possibleLetters,
selected = query$letters,
server=T)
cat(file=stderr(), paste0('observeEvent running - ran updateSelectizeInput, input$letters is now: ',input$letters,'\n'))
}
})
output$lettersDisplay = renderText({
return(paste(userLetters(),collapse=' '))
})
output$lettersIndicesDisplay = renderText({
return(paste(userLetterIndices(), collapse=' '))
})
}
shinyApp(ui = ui, server = server, options = list(port=1111))
Steps to reproduce problem: Run the app and then navigate to http://localhost:1111/?letters=A
You will find that "A" has indeed been filled into the selectize field in the browser window, however, the input
value has not been updated. In your console you will see:
observeEvent running - user specified param is: A
observeEvent running - ran updateSelectizeInput, input$letters is now:
Thus, the query string has been correctly parsed, updateSelectizeInput
has been called, and yet when input$letters
is accessed afterwards, its value has not changed.
I suspect this relates to some fundamental shortcoming in my understanding of reactive graphs or something, but after poring over Mastering Shiny, I still can't figure out why this call does nothing.