observeEvent not triggered when updated with the same value

,

Hi All,

I have a similar question to the one already posted here on the forum.

I am developing an interactive questionnaire form that has dependencies between questions, for example: question B will not be rendered until in question A answer 1 is selected. To trigger dependent questions I am using an observeEvent. When I load a saved form for the first time it works just fine. However, when I load the same form for the second time the form is loaded incorrectly as the dependent questions are not triggered.
As I understand from @pieterjanvc topic it is caused by the lazy nature of reactivity, and when the question is updated with the same answer for the second time the observeEvent is not triggered. In my case it is unacceptable as I need it to run every time.

Has anything changed in that regard since @pieterjanvc brought up this topic 4 years ago?

I would be grateful for any suggestions how to overcome this issue.

Hello,

Now that I've got much more expertise since that last post, I understand better why this is happening and will try to explain it :stuck_out_tongue:

As was mentioned in the original post, it makes sense for Shiny to not update anything in the UI if an input value has not changed, as it should generate the exact same output.

However, sometimes we as coders put additional code in a reactive context triggered by the input that would end in a change when run again with the same value (i.e. it's not solely depending on that input value), but will not be run if the observed input is updated with the same value. The trick is to identify this code (or depended reactive) and parse it out. Then you figure out what the correct trigger to update this is and make it it's own reactive (or move it to an existing one that has this trigger already).

It's very hard to make this concrete without seeing your actual code, but I hope this will help to start identifying the problem.

Hope this helps,
PJ

Hi Pieter, thank you for your answer.

I admit it's not totaly clear to me what you mean. I will try to present tomorrow a reprex of my usecase but if you could explain using the example you made previously and how it should be corrected, that would be a great help to me. In spirit, it is very similar to my case.

library(shiny)

ui <- fluidPage(
  
  numericInput("input1", "Set a value", value = 0),
  actionButton("click", "Click")
  
)

server <- function(input, output, session) {
  
  myVal = reactiveVal()
  
  observeEvent(input$click, {
    print("click registered")
    myVal(input$input1)
  }, ignoreInit = T)
  
  observeEvent(myVal(), {
    showModal(modalDialog("Update"))
  }, ignoreInit = T)
  
}

shinyApp(ui, server)

Hello,

So yes this reprex exactly shows what I tried to explain. The modal window appearing is dependent on clicking the button and not on the value changing. This means it will need to be in a reactive environment that is triggered by it.

In this case both observe events collapse into one:

observeEvent(input$click, {
    print("click registered")
    myVal(input$input1)
    showModal(modalDialog("Update"))
  }, ignoreInit = T)

In this case myVal is a redundant variable, but that's besides the point

I know you probably have a more elaborate scenario where things are more complicated, but I am pretty sure in most cases thinking about the correct triggers and rearranging code can fix the issue :slight_smile:

Grtz,
PJ