rshinyrhandsontableshinybs

Force draw of hidden table


I have the following app, which is just a collapse panel containing an rhandsontable table.

library(shiny)
library(shinyBS)
library(rhandsontable)

ui <- function() {
  fluidPage(
    bsCollapsePanel(
      "Test",
      rHandsontableOutput("table")
    )
  )
}

server <- function(input, output, session) {
  output$table <- renderRHandsontable({
    rhandsontable(
      data.frame(
        a = 1:2,
        b = 2:3
      )
    )
  })
}

shinyApp(ui, server)

It works as expected: the panel starts with its contents hidden, and if we click it the panel opens and we see the table.

However, there is a noticeable "lag" between the panel opening and the table appearing. I assume this is because the table hadn't been initialized until then, and so all the work actually creating the table only happens at that moment.

If we then close the panel and reopen it, there is no such lag and we can even gradually see the table as the panel reopens.

I don't know if this is a feature or a bug, or who's "fault" it is: rhandsontable, for being lazy in starting up? shinyBS, for being lazy starting its contents up? shiny in general, for only triggering redraws immediately when needed? I'd assume it's rhandsontable, since more basic elements like textInput() don't have this problem, but can't know for sure.

So, is there a way to force this initialization of the table when the app starts up, instead of only when the panel expands?

I've thought of setting the panel to start open and then hack the server to close the panel on startup, but I'm not entirely sure how that'd work... or if it'd even work (if it closes prior to the first redraw, what difference will it make? if it's after the first redraw, that'd imply a flicker on startup, right?).


Solution

  • I think this should do it:

    server <- function(input, output, session) {
      output$table <- renderRHandsontable({
        rhandsontable(
          data.frame(
            a = 1:2,
            b = 2:3
          )
        )
      })
      outputOptions(output, "table", suspendWhenHidden = FALSE)
    }