moho wu
moho wu

Reputation: 491

How to get the input that is invalidated in Shiny?

I'm trying to update a text box based on the click events in multiple graphs. I use the observeEvent function to trigger the update of the text box. A snippet of the code is shown below.

  observeEvent({
    input$plot1_click
    input$plot2_click
    input$plot3_click
    ...
  }, {
    # only need the invalidated input
    invalid_input <- which.invalid(input$plot1_click,
                                   input$plot2_click,
                                   input$plot3_click,
                                   ...)
    updateTextInput(session,
                    "textbox",
                    label = NULL,
                    value = invalid_input)

  })   

Currently the updateTextInput function will run whenever there's a click in any of the plots, which is desired, but I can't figure out how to capture which plot is clicked last and should be used to update the text input. Is there a function to check which input is invalidated from a list of inputs?

Upvotes: 1

Views: 276

Answers (1)

Bertil Baron
Bertil Baron

Reputation: 5003

Hi I think the easiest way is to make a separate observer for each input. This way the input that was changed last will always processed last. You can put it in a lapply if you don't want to bloat you code with repetitive code. It could look something like this

lapply(1:3, function(idx){
    observeEvent({input[[paste0("plot",idx,"_click")]]},
                 {updateTextInput(session,
                                  "textbox",
                                  label = NULL,
                                  value = input[[paste0("plot",idx,"_click")]])})
  })

Hope this helps!

Upvotes: 0

Related Questions