2

Thanks to rhandonstable, I am asking the user of my shinyApp to fill in a table from which computations are done and plots displayed in real time. This is great! However, because of computation time (simulated by a little Sys.sleep() in the MRE below), the rendering of the handsontable can take longer time than its filling by the user. This makes it unstable and triggers some strange loops. One can see this effect by filling the first column (with integers) on the example below, and then filling "quite suddenly" the second column. If not sudden enough, just increase the Sys.sleep() time to make this effect more obvious. This effect can seem very unimportant, but once the App is on shinyapps.io, it makes it barely usable.

Well, I'm pretty sure I'm doing something wrong as my understanding of the reactive, observer and isolate is very limited. But to avoid this effect (and having to ask the user to be slow), I would like to prioritise the rendering so that no computation is triggered and plot updated while the table is not rendered. Also keeping the real time aspect is important, so I would like to avoid using a "compute" button. Could someone explain me how to do that? Any other suggestion would be very welcome.

library(shiny)
library(rhandsontable)
library(ggplot2)

DF <- data.frame(x=integer(0), y=integer(0))

ui <- shinyUI(fluidPage(

                        mainPanel( 
                          rHandsontableOutput("hot"),
                          plotOutput("plot1")
                        )
))

server <- shinyServer(function(input, output) {

  values <- reactiveValues()

  observe({
    if (!is.null(input$hot)) {
      DF <- hot_to_r(input$hot)
    } else {
      if (is.null(values[["DF"]]))
        DF <- DF
      else
        DF <- values[["DF"]]
    }
    values[["DF"]] <- DF
  })

  output$hot <- renderRHandsontable({
    rhandsontable(values[["DF"]], stretchH = "all", minRows=5)
  })

  output$plot1 <- renderPlot({
      table <- values[["DF"]]
      table <- na.omit(table)

      Sys.sleep(.3) # 

      ggplot(data=table) + geom_line(aes(x=x, y=y))

    })

})

shinyApp(ui=ui, server=server)

0 Answers0