R Shiny-renderDataTable渲染大型矩阵太慢

时间:2018-11-27 11:05:59

标签: r shiny dt

我正在制作一个使用renderDataTable绘制巨大矩阵的Shiny应用程序。 矩阵约为30行500列。我需要renderDataTable来快速绘制矩阵。现在大约是2-3秒(对于该应用来说太慢了)。有没有一种方法可以加快渲染速度?

这是一个最小的示例:

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)

该矩阵是在myMatrix反应堆中计算的,并且每次用户单击“更新”按钮时都会更新。问题是每次单击按钮时,矩阵的渲染时间太慢。

非常感谢, 汤姆C。

1 个答案:

答案 0 :(得分:0)

尝试选项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)