I'm trying to get the following to work within a Shiny App:
- There are two pages, each defined by a different UI
- The first page shown has a textInput with some default text (ie,
textInput("text", "Text Input", "word")) - When a user clicks a button: (1) the value of
input$textgets updated via updateTextInput and (2) the page switches.
The third part isn't working for me. I've defined an actionButton, where when that is clicked I have two observeEvents that get triggered: the first one with updateTextInput, and a second one that updates the page. The problem is that input$text isn't being updated - it seems as though the updateTextInput is being ignored.
When I comment out the observeEvent that switches the ui, the updateTextInput works as expected.
Any idea how to get the button to both update the text then switch the UI? This is all part of a larger App I'm making, but the below example reproduces the issue.
# UIs --------------------------------------------------------------------------
ui = (htmlOutput("page"))
ui_page1 = fluidPage(
h1("Page 1"),
actionButton("to_page2", "Go to Page 2"),
textInput("text", "Text Input", "word")
)
ui_page2 <- fluidPage(
h1("Page 2"),
actionButton("to_page1", "Go to Page 1"),
br(),
h4("Displaying input$text - I want this show `new word`"),
textOutput("input_text")
)
# Server -----------------------------------------------------------------------
server = function(input, output, session) {
#### By default, start with page 1
output$page <- renderUI({
div(ui_page1)
})
#### To page 2
observeEvent(input$to_page2, {
updateTextInput(session, "text", value = "new word")
}, priority = 3)
# *** Comment this observeEvent out, and the updateTextInput works as expected
observeEvent(input$to_page2, {
output$page <- renderUI({
div(ui_page2)
})
}, priority = 2)
# To display the input$text value
observeEvent(input$to_page2, {
output$input_text <- renderText(input$text)
}, priority = 1)
#### To page 1
observeEvent(input$to_page1, {
output$page <- renderUI({
div(ui_page1)
})
})
}
# shinyApp ---------------------------------------------------------------------
shinyApp(ui, server)