observe not registering reactive value

Hello,

I am using a reactive value (df_age) in an observer and the expression is never re-evaluated when df_age changes.
Here is the code:


  observe({
    plot_age <- adaptative_dot_plot(
      df_age(),
      df_age()$pat_age
    )

    output$plot_age <- plot_age
    output$plot_age_zoomed <- plot_age
  })

How do I know df_age is reactive changes? I added observe(message(nrow(df_age()))) right before the observer and it prints a message when df_age is changed.

I have also done another experiment. I have added message(nrow(df_age())) to the first line of the observer and magically the observer is now reactive to changes to df_age!

The observer does not seem to register that it should update when df_age updates.

If anyone has an idea to solve this issue...
Thank you!

Hi,

Welcome to the RStudio community!

Reactive expression are only evaluated when they are needed. It is hard to guess from just this piece of code, but one thing that is a bit weird is you seem to be updating plots (output$plot_age, output$plot_age_zoomed) without using a renderPlot() function.

The observe is not the best reactive wrapper in this case, you would want to either make a reactive variable or put code in the render plot function.

library(shiny)

ui <- fluidPage(
  
  actionButton('myButton', "click"),
  plotOutput("plot1"),
  plotOutput("plot2")
  
)

server <- function(input, output) {
  
  dataPlot1 = reactive({
    input$myButton
    plot(data.frame(x = 1:5, y = runif(5)))
  })
  
  output$plot1 = renderPlot({
    dataPlot1()
  })
  
  output$plot2 = renderPlot({
    input$myButton
    plot(data.frame(x = 1:5, y = runif(5)))
  })
  
}

shinyApp(ui, server)

Please provide a reprex if this is not what you are looking for. Shiny debugging and reprex guide

Hope this helps,
PJ

Ok thank you.
I had the renderPlotly in the adaptative_dot_plot function and it was not working that way.
Now that renderPlotly is out of the function it works.

thanks!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.