rshinyrenderuiuioutput

uiOutput rendered based on input, resets when button is clicked, and then can't be updated again when the input is updated


I have a shiny app with a mix of inputs, and uiOutputs that summarize what the user has input. I have a button that resets all the input values and clears the uiOutputs, but once the uiOutputs are cleared, they no longer update based on the inputs.

This doesn't reset the uiOutput:

library(shiny)

ui <- fluidPage(
  selectInput('select1', label = 'test', choices = c('a', 'b', 'c'), selected = character(0)),
  
  uiOutput('output1'),
  
  actionButton('reset', 'reset')
)

server <- function(input, output, session) {
  output$output1 <- renderUI(
      if(length(input$select1) > 0) { ## this essentially determines whether the input is valid
        h6(input$select1)
      } else {
        h6('')
      }
    )
  
  observeEvent(input$reset, {
    updateSelectInput(session, 'select1', selected = character(0))
    print('clicked')
  })
}

shinyApp(ui = ui, server = server)

This one resets it but doesn't allow it to be updated again based on the input:

library(shiny)

ui <- fluidPage(
  selectInput('select1', label = 'test', choices = c('a', 'b', 'c'), selected = character(0)),
  
  uiOutput('output1'),
  
  actionButton('reset', 'reset')
)

server <- function(input, output, session) {
  output$output1 <- renderUI(h6(input$select1))
  observeEvent(input$reset, {
    output$output1 <- renderUI(
        h6('')
    )
    updateSelectInput(session, 'select1', selected = character(0))
    print('clicked')
  })
}

shinyApp(ui = ui, server = server)

How can I make the uiOutput reset, either with a renderUI() statement, and then update again when a new input is selected?

Thanks!


Solution

  • As you noted, clearing the selection in a selectInput will not trigger the output (dependent on that input) to update. One option is to create a reactiveVal to store your text, that you can clear directly when reset. Would this work for you?

    server <- function(input, output, session) {
      
      rv <- reactiveVal('')
      
      output$output1 <- renderUI(
        h6(rv())
      )
      
      observe({
        if(length(input$select1) > 0) {
          rv(input$select1)
        } else {
          rv('')
        }
      })
      
      observeEvent(input$reset, {
        updateSelectInput(session, 'select1', selected = character(0))
        rv('')
      })
      
    }