R Shiny - renderDataTable слишком медленный рендеринг огромной матрицы - PullRequest
0 голосов
/ 27 ноября 2018

Я делаю блестящее приложение, которое использует renderDataTable для построения огромной матрицы.Матрица составляет ~ 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 и обновляется каждый раз, когда пользователь нажимает кнопку Обновить.Проблема в том, что каждый раз, когда нажимается кнопка, время рендеринга матрицы слишком медленно.

Большое спасибо, Том С.

1 Ответ

0 голосов
/ 04 декабря 2018

Попробуйте вариант 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)
...