Creating Shiny reactive variable that indicates which widget was last modified

后端 未结 2 1952
傲寒
傲寒 2020-12-16 07:22

Let\'s say we have a set of widgets each with their own input label. How do we create a reactive object whose value is the character that represents the input ID of the last

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 07:51

    Here's a solution that works, though it looks a little awkward because of a nested observe(). I'm not sure what a better way would be, but there could be something nicer.

    Basically, use an observe() to loop over all the available inputs, and for each input, use another observe() that will only trigger when that input is changed and set a variable to the id of the input.

    runApp(shinyApp(
      ui = shinyUI(
        fluidPage(
          textInput('txt_a', 'Input Text A'),
          textInput('txt_b', 'Input Text B'),
          uiOutput('txt_c_out'),
          verbatimTextOutput("show_last")
        )
      ),
      server = function(input, output, session) {
        output$txt_c_out <- renderUI({
          textInput('txt_c', 'Input Text C')
        })
    
        values <- reactiveValues(
          lastUpdated = NULL
        )
    
        observe({
          lapply(names(input), function(x) {
            observe({
              input[[x]]
              values$lastUpdated <- x
            })
          })
        })
    
        output$show_last <- renderPrint({
          values$lastUpdated
        })
      }
    ))
    

提交回复
热议问题