Does isolate() have better performance than observeEvent()?

Hi there!
Recently, I got confused about the usage of isolate() and observeEvent(). As the codes shown below, those two codes seem to do the same work—rendering the plot only after clicking the button.
However, when I am using the isolate() function in my app deployed on shinyapps.io, my app got crashed because of too much memory usage. (I have simplified the graphing code here only for demonstration. The actual codes are much more complex.) I am wondering if the isolate() does anything with this bug. Will the replacement of the isolate() function by the observeEvent() help improve performance?
Any suggestions will be appreciated. Thanks so much!

  library(shiny)
  library(stats)
  
  runApp(list(
    ui = bootstrapPage(
      textInput(inputId = "text_in",
                label = "Type something:"),
      actionButton(inputId = "submit",
                   label = "submit"),
      plotOutput(outputId = "testpic")
    ),
    server = function(input, output) {
      output$testpic <- renderPlot({
        if (input$submit == FALSE) return()
        isolate({
          x  <- as.matrix(mtcars)
          rc <- rainbow(nrow(x), start = 0, end = .3)
          cc <- rainbow(ncol(x), start = 0, end = .3)
          heatmap(x, col = cm.colors(256), scale = "column",
                  RowSideColors = rc, ColSideColors = cc, margins = c(5,10),
                  xlab = input$text_in, ylab =  input$text_in,
                  main = input$text_in)
        })
      })
    }
  ))
library(shiny)
  library(stats)
  
  runApp(list(
    ui = bootstrapPage(
      textInput(inputId = "text_in",
                label = "Type something:"),
      actionButton(inputId = "submit",
                   label = "submit"),
      plotOutput(outputId = "testpic")
    ),
    server = function(input, output) {
      vplot <- eventReactive(input$submit, {
        x  <- as.matrix(mtcars)
        rc <- rainbow(nrow(x), start = 0, end = .3)
        cc <- rainbow(ncol(x), start = 0, end = .3)
        heatmap(x, col = cm.colors(256), scale = "column",
                           RowSideColors = rc, ColSideColors = cc, margins = c(5,10),
                           xlab = input$text_in, ylab =  input$text_in,
                           main = input$text_in)
      })
      
      observeEvent(input$submit, {
        output$testpic <- renderPlot({vplot()})
      })
    }
  ))

Answered on StackOverflow.

1 Like

isolate is the like the opposite of observeEvent. When you isolate it's like putting your reactive on a desert island or locking them in a wardrobe with very thick doors... no matter how noisy they are nobody is going to listen to them. With observeEvent, it is like putting your reactive on twitter with millions of followers, anything they do and they'll get a reaction.

I see...Thank you for the detailed explanation.
May I ask your opinion on the code below? I don't understand why the plot can be only rendered after clicking the button. As far as I know, the action button will turn from False to TRUE after the first time clicking it. And every time you click it, the value of input$submit will increase by one, like 1,2,3. Thanks again!

  library(shiny)
  library(stats)
  
  runApp(list(
    ui = bootstrapPage(
      textInput(inputId = "text_in",
                label = "Type something:"),
      actionButton(inputId = "submit",
                   label = "submit"),
      plotOutput(outputId = "testpic")
    ),
    server = function(input, output) {
      output$testpic <- renderPlot({
        if (input$submit == FALSE) return()
        isolate({
          x  <- as.matrix(mtcars)
          rc <- rainbow(nrow(x), start = 0, end = .3)
          cc <- rainbow(ncol(x), start = 0, end = .3)
          heatmap(x, col = cm.colors(256), scale = "column",
                  RowSideColors = rc, ColSideColors = cc, margins = c(5,10),
                  xlab = input$text_in, ylab =  input$text_in,
                  main = input$text_in)
        })
      })
    }
  ))

renderPlot only responds to changes in input$submit as you've isolated input$text_in. You might like to cat(input$submit) to see its value when it changes.

This topic was automatically closed 54 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.