Reputation: 551
Below is a simple R Shiny app example with textInput and actionButton, after clicking the button it is supposed to set the text value to NULL and print NULL value, however I have observed that after clicking the submit button it still prints the value entered in the textInput. Can anyone please explain why there is this delay in updating the textInput value ?
library(shiny)
ui <- fluidPage(
textInput("text", "Test"),
actionButton("Submit","Submit")
)
server <- function(input, output, session) {
observeEvent(input$Submit,{
updateTextInput(session,"text", value = " ")
print(input$text)
})
}
shinyApp(ui, server)
After running the app if I enter "hello" in the text input, I should get " " on the print console, but it shows the value of input$text , i.e "hello" in the print console. Clearly the assignment of " " is not happening in the sequence it is written in the code.
Please share if anyone can explain this behavior in Shiny app. Thank you for your time and suggestions.
Upvotes: 2
Views: 747
Reputation: 2031
The reason for this behaviour is that updateTextInput (and all update*Input functions for that matter) does not update the input
object in the server function. Instead, it sends a message to the client (the browser) to change the text input field. And when the text input field in the browser is updated, the browser sends back a signal to the server that input$text
has been changed. But before that happens, the execution of code inside the observeEvent
block has continued, and so when executing print(input$text)
, the value is still "hello".
Upvotes: 2