我正在为我的一位教授在 Shiny 中制作交互式树状图。树形图在最坏的情况下有 3 个级别和 20,000 个叶子。我无法在合理的时间内渲染它。
感兴趣的树形图的数量相当少,所以我创建了highcharter绘图对象并预先将它们序列化:
library(readr)
library(magrittr)
library(highcharter)
library(treemap)
tm_file_name <- function(num_leaves) {
paste0(as.character(num_leaves), "_leaves.rds")
}
create_rand_treemap <- function(num_leaves) {
random.hierarchical.data(n = num_leaves) %>%
treemap(index = c("index1", "index2", "index3"),
vSize = "x",
draw = FALSE) %>%
hctreemap(allowDrillDown = TRUE) %>%
write_rds(tm_file_name(num_leaves))
}
for (leaves in c(50, 500, 5000)) {
create_rand_treemap(leaves)
}
我的闪亮app.r
文件看起来像这样
library(shiny)
library(shinydashboard)
library(readr)
library(magrittr)
library(highcharter)
tm_file_name <- function(num_leaves) paste0(as.character(num_leaves), "_leaves.rds")
load_treemap <- function(num_leaves) tm_file_name(num_leaves) %>% read_rds()
ui <- dashboardPage(
dashboardHeader(title = "Reproducible Interactive Highcharter Treemap Test",
titleWidth = "100%"),
dashboardSidebar(
div(style = "display:inline-block;width:100%;text-align:center;",
radioButtons("num_leaves", "Number of Leaves in Treemap:",
c("50" = 50, "500" = 500, "5000" = 5000)),
actionButton("createTreemap", "Create Treemap"))),
dashboardBody(highchartOutput("treemap", width = "100%", height = "500px")),
)
# loads and renders plot object only on button press
server <- function(input, output) {
output$treemap <- renderHighchart({
if (length(input$createTreemap) != 0) {
if (input$createTreemap > 0) isolate({load_treemap(input$num_leaves)})
}
})
}
shinyApp(ui = ui, server = server)
这个闪亮的应用程序就在这里。具有 50 和 500 个叶子的树形图的渲染时间大约在可接受的范围内。但是,对于 5000 片叶子来说太慢了,我需要一个可以处理多达 20,000 片叶子的最终解决方案。
我能做些什么来提高速度吗?