如何在加载数据时显示闪亮警报弹出窗口?

问题描述 投票:0回答:1

我正在研究一个相当“大”的数据集,在启动我的

Shiny
应用程序时至少需要 20 秒才能完全读取该数据集。我想在阅读期间显示一条弹出等待消息,如下所示,完成后会自动关闭。

但是,我不知道如何指定一个条件来自动关闭警报。 以下是我到目前为止所做的:

server <- function(input, output, session) {
   data = read_sav("BS.SAV")
 shinyalert(
      title = "Wait",
      text = "Waiting for data loading",
      size = "xs",
      closeOnEsc = TRUE,
      closeOnClickOutside = TRUE,
      html = TRUE,
      type = "info",
      showConfirmButton = TRUE,
      confirmButtonText = "OK",
      confirmButtonCol = "#004192",
      showCancelButton = FALSE,
      imageUrl = "",
      animation = TRUE
    )
}
r shiny shiny-reactivity shinyalert
1个回答
1
投票

这是一个例子。使用下面的代码,我们在

onInput
上附加一个
fileInput
属性,该属性在上传开始时设置某个
InputValue

fileInput(
  inputId = 'file',
  label = 'Choose CSV File',
  accept = c('text/csv',
             'text/comma-separated-values,text/plain',
             '.csv')
) |>
  tagAppendAttributes(
    onInput = "
              const id = $(this).find('input[type=\"file\"]').attr('id');
              Shiny.setInputValue(id + '_click', Math.random());
              "
  )

InputValue
的更改会触发显示警报的
observeEvent
。然后输出被渲染为
DT::datatable
,当最终渲染该对象时,警报将关闭 (
CloseAlert()
),请参阅下面的完整示例。

library(shiny)
library(shinyalert)

options(shiny.maxRequestSize = 30 * 1024 ^ 2 * 4)

shinyApp(
  ui = fluidPage(
    fileInput(
      inputId = 'file',
      label = 'Choose CSV File',
      accept = c('text/csv',
                 'text/comma-separated-values,text/plain',
                 '.csv')
    ) |>
      tagAppendAttributes(
        onInput = "
                  const id = $(this).find('input[type=\"file\"]').attr('id');
                  Shiny.setInputValue(id + '_click', Math.random());
        "
      ),
    DT::dataTableOutput("table")
  ),
  
  server = function(input, output, session) {
    input_file <- reactive({
      if (is.null(input$file)) {
        return("")
      }
      
      read.csv(file = input$file$datapath)
    })
    
    
    output$table <- DT::renderDataTable({
      req(input_file())
      
      closeAlert()
      
      input_file()
    })
    
    observeEvent(input$file_click,
                 {
                   shinyalert(
                     title = "Wait",
                     text = "Waiting for data loading",
                     size = "xs",
                     closeOnEsc = TRUE,
                     closeOnClickOutside = TRUE,
                     html = TRUE,
                     type = "info",
                     showConfirmButton = TRUE,
                     confirmButtonText = "OK",
                     confirmButtonCol = "#004192",
                     showCancelButton = FALSE,
                     imageUrl = "",
                     animation = TRUE
                   )
                   
                 },
                 ignoreNULL = FALSE,
                 ignoreInit = TRUE)
  }
)
© www.soinside.com 2019 - 2024. All rights reserved.