Using reactive_Values across different renderfunctions.

Hello all. New to shiny - and i guess still learning about how to use reactivity properly.
The issue stems from wanting to use values calculated in different render functions, using a function on these, then using the result in all render functions.
MWE below

## Only run examples in interactive R sessions
if (interactive()) {
  
  ui <- fluidPage(
    plotOutput("plot1"),
    plotOutput("plot2")
  )
  
  # The comments below show the equivalent logic using reactiveValues()
  server <- function(input, output, session) {
    Reactive_values <- reactiveValues()       # rv <- reactiveValues(value = 0)
    
    output$plot1 <- renderPlot({
      x <- rnorm(100)
      Reactive_values$xmax1 <-  max(x)
      xmax <- max(c(Reactive_values$xmax1, Reactive_values$xmax2))
      y <- sin(x^2)
      plot(x,y, main = paste0(round(max(x),3)), xlim = c(-3, xmax) )
    })
    
    output$plot2 <- renderPlot({
      x <- rnorm(100, mean = 1)
      Reactive_values$xmax2 <-  max(x)
      xmax <- max(c(Reactive_values$xmax1, Reactive_values$xmax2))
      
      y <- sin(x^2)
      plot(x,y, main = paste0(round(max(x),3)), xlim = c(-3, xmax))
    })
  }
  
  shinyApp(ui, server)
  
}

Hi @NielsKrarup. Your code is semantically correct but the Reactive_values trigger the reactive event frequently, so the plot also renew too frequently and cannot display. xmax1 trigger the change of xmax2 and vice versa. If you want your code to work, change the Reactive_values to list will do.
reactiveValues is like a list. You can store anythings as it's element and the change of element will trigger the reactive event.

## Only run examples in interactive R sessions
library(shiny)


if (interactive()) {
  
  ui <- fluidPage(
    plotOutput("plot1"),
    plotOutput("plot2")
  )
  
  # The comments below show the equivalent logic using reactiveValues()
  server <- function(input, output, session) {
    Reactive_values <- list()       # rv <- reactiveValues(value = 0)
    
    output$plot1 <- renderPlot({
      x <- rnorm(100)
      Reactive_values$xmax1 <-  max(x)
      xmax <- max(c(Reactive_values$xmax1, Reactive_values$xmax2))
      y <- sin(x^2)
      plot(x,y, main = paste0(round(max(x),3)), xlim = c(-3, xmax) )
    })
    
    output$plot2 <- renderPlot({
      
      x <- rnorm(100, mean = 1)
      Reactive_values$xmax2 <-  max(x)
      xmax <- max(c(Reactive_values$xmax1, Reactive_values$xmax2))
      
      y <- sin(x^2)
      plot(x,y, main = paste0(round(max(x),3)), xlim = c(-3, xmax))
    })
  }
  
  shinyApp(ui, server)
  
}

Thanks much appreciated

I had a feeling that the events would trigger each other, but didn't know how to fix it.
Although the plots show now, the xmax, the xmax is not used as the right limit for both plots.

br

@NielsKrarup. What the purpose of your code to use reactiveValues? If you want to automatically trigger each plot, you can delay the update of reactiveValues and let it have time to update the plots.

library (shiny)
library (shinyjs)
if (interactive()) {
  
  ui <- fluidPage(
    useShinyjs(),
    plotOutput("plot1"),
    plotOutput("plot2")
  )
  
  # The comments below show the equivalent logic using reactiveValues()
  server <- function(input, output, session) {
    Reactive_values <- reactiveValues()       # rv <- reactiveValues(value = 0)
    
    output$plot1 <- renderPlot({
      xmax2 <- Reactive_values$xmax2
      isolate({
        x <- rnorm(100)
        delay(500, {Reactive_values$xmax1 <-  max(x)})
        xmax <- max(c(Reactive_values$xmax1, xmax2))
        y <- sin(x^2)
        plot(x,y, main = paste0(round(max(x),3)), xlim = c(-3, xmax) )
      })
      
    })
    
    output$plot2 <- renderPlot({
      xmax1 <- Reactive_values$xmax1
      isolate({
        x <- rnorm(100, mean = 1)
        delay(500, {Reactive_values$xmax2 <-  max(x)})
        xmax <- max(c(xmax1, Reactive_values$xmax2))
        y <- sin(x^2)
        plot(x,y, main = paste0(round(max(x),3)), xlim = c(-3, xmax))
      })
    })
  }
  
  shinyApp(ui, server)
  
}