闪亮:显示通知,直到浏览器不再繁忙

时间:2017-07-03 00:54:50

标签: r shiny

我有一个Shiny应用程序,它绘制了大型树图。我提前创建了树形图对象并将它们序列化:

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)
}

然后我像这样加载它们:

library(shiny)
library(shinydashboard)
library(shinycssloaders)
library(readr)
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 Shiny Example",
    titleWidth = "100%"
  ),

  dashboardSidebar(
    radioButtons(
      "num_leaves", "Number of Leaves in Treemap:",
      c("50" = 50, "500" = 500, "5000" = 5000)
    ),
    actionButton("createTreemap", "Create Treemap")
  ),

  dashboardBody(
    # CSS spinner
    withSpinner(highchartOutput("treemap", width = "100%", height = "500px"),
                type = 7))
)

server <- function(input, output) {
  output$treemap <- renderHighchart({

    # load and render only on button press
    if (length(input$createTreemap) != 0) {
      if (input$createTreemap > 0) {

        # notification attempt
        progress <- shiny::Progress$new()
        on.exit(progress$close())
        progress$set(message = 'Generating treemap...')

        isolate({load_treemap(input$num_leaves)})
      }
    }
  })
}

shinyApp(ui = ui, server = server)

我想显示加载通知或微调器,直到树形图完全渲染。

以上示例live here包含两次尝试。

第一次尝试使用Shiny进度对象来创建通知。但是,通知仅在消失之前显示很短的时间。我相信这是因为Shiny只需要读取一个序列化文件然后认为任务已经完成,但是浏览器需要很长时间来渲染该图。

我也尝试了shinycssloaders方法,但是

  • CSS spinner在应用程序首次加载时会短暂显示,这是我不想要的。
  • 渲染大树图(即5000片叶子)后,旋转器在几秒钟后冻结。

最后,我尝试了从this SO question进行的一些盲目复制粘贴,但要么无法使解决方案与shinydashboard一起使用,要么一直持续到渲染完成。

在Shiny 和浏览器不再忙碌之前,如何显示闪亮的通知?

0 个答案:

没有答案