langtang
langtang

Reputation: 24845

How to speed up renderDT with bslib in shiny

When I try to use DTOutput() and renderDT() within bslib framework, the rendering of those tables is very slow.

Example app with bslib:

library(shiny)
library(DT)
library(data.table)
library(bslib)

dt= mtcars |> as.data.table()

ui <- page_sidebar(
  layout_columns(
      card(card_header('Gear', class="bg-primary"), DTOutput("gear")),
      card(card_header('Carb', class="bg-primary"), DTOutput("carb")),
      card(card_header('AM', class="bg-primary"), DTOutput("am")),
      col_widths = c(4,4,4)
    ),
  sidebar = sidebar(
    selectInput("cyl", "Cylinder", choices=dt[, unique(cyl)])
  )
)

server <- function(input, output, session) {
  output$gear <- renderDT(dt[cyl==input$cyl, .N, gear])
  output$carb <- renderDT(dt[cyl==input$cyl, .N, carb])
  output$am <- renderDT(dt[cyl==input$cyl, .N, am])
}

shinyApp(ui, server)

I can speed this up dramatically, in one of three ways:

  1. change to renderTable() and tableOutput()
  2. change style='auto' (the default in datatable()) to one of the other options ('default', 'bootstrap','bootstrap4', etc)
  3. avoid the bslib framework as below:
library(shiny)
library(DT)
library(data.table)

dt= mtcars |> as.data.table()

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(selectInput("cyl", "Cylinder", choices=dt[, unique(cyl)]),width=2),
    mainPanel = mainPanel(
      fluidRow(column(4, DTOutput("gear")),column(4, DTOutput("carb")),column(4, DTOutput("am"))), width=10
    )
  )
)
server <- function(input, output, session) {
  output$gear <- renderDT(dt[cyl==input$cyl, .N, gear])
  output$carb <- renderDT(dt[cyl==input$cyl, .N, carb])
  output$am <- renderDT(dt[cyl==input$cyl, .N, am])
}

shinyApp(ui, server)

result

Question:

what is going on with bslib and DT and style='auto' such that rendering speed suffers when used in conjunction? What can be done about it, if anything?

Upvotes: 1

Views: 52

Answers (0)

Related Questions