I am making an interactive treemap in Shiny for a professor of mine. The treemap has in the worst case 3 levels and 20,000 leaves. I am having trouble getting this to render in a reasonable amount of time.
The number of treemaps of interest is rather small, so I've created highcharter plot objects and serialized them beforehand:
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)
}
My Shiny app.r file then looks like this
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)
This Shiny app is live here. Rendering time for the treemaps with 50 and 500 leaves is approximately within the acceptable range. However, for 5000 leaves it's too slow, and I need a final solution that can deal with up to 20,000 leaves.
Is there anything I can do to eke out more speed?