TOMC
TOMC

Reputation: 163

R Shiny - renderDataTable too slow rendering a huge matrix

I'm making a Shiny app that uses renderDataTable to plot a huge matrix. The matrix is ~ 30 rows and 500 columns. I need the renderDataTable to quickly plot the matrix. Now it's around 2-3 seconds (way too slow for this app). Is there a way to speed up the rendering ?

Here is a minimal example :

library(shiny)
library(DT)

ui <- fluidPage( 
  br(),
  actionButton(inputId = 'Update.button', label = "Update",width = 100),
  br(),br(),
  dataTableOutput(outputId = "myTable")
            )


server <- function(input, output){

myMatrix <- eventReactive(
           input$Update.button, 
           cbind(paste('Name',1:30,sep =''), replicate(500, sample(x=10:100,30)*10^5))
                        )

output$myTable <- DT::renderDataTable({

COLS <-  1:(ncol(myMatrix())-1)
WIDTH <- '60px' ## WIDTH is reactive and depends on user input (let's fix it here to 60px)

DT::datatable(data = myMatrix(), rownames = TRUE,class = 'compact',
 options = list(pageLength = 30,paging = FALSE,searching = FALSE, scrollX = TRUE, ordering=FALSE, info=FALSE,
   autoWidth=TRUE,columnDefs = list(list(className = "dt-center",targets = COLS,width = WIDTH), # apply center and WIDTH on every column except first one
                                   list(className = "dt-center", target = 0)) ## center first column
          ))})

}

shinyApp(ui = ui,server = server)

The matrix is calculated in the myMatrix reactive and is updated everytime the user clicks on the Update button. The problem is that each time the button is clicked the rendering time of the matrix is too slow.

Thanks a lot, Tom C.

Upvotes: 1

Views: 1847

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84699

Try the option server=FALSE:

output$myTable <- renderDT({

  COLS <-  1:(ncol(myMatrix())-1)
  WIDTH <- '60px' ## WIDTH is reactive and depends on user input (let's fix it here to 60px)

  datatable(data = myMatrix(), rownames = TRUE,class = 'compact',
            options = list(pageLength = 30,
                           paging = FALSE,
                           searching = FALSE, 
                           scrollX = TRUE, 
                           ordering=FALSE, 
                           info=FALSE,
                           autoWidth=TRUE,
                           columnDefs = list(list(className = "dt-center",
                                                  targets = COLS,
                                                  width = WIDTH), # apply center and WIDTH on every column except first one
                                             list(className = "dt-center", 
                                                  target = 0)) ## center first column
            ))}, server = FALSE)

Upvotes: 0

Related Questions