我正在制作一个使用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反应中计算,每次用户点击“更新”按钮时都会更新。问题是每次单击按钮时矩阵的渲染时间都太慢。
非常感谢Tom C.
试试选项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)