如何在R Shiny中加载数据时显示shinyalert弹出窗口?

huangapple go评论73阅读模式
英文:

How do I display a shinyalert pop-up while loading data in R shiny?

问题

我正在处理一个相当“大”的数据集,需要至少20秒才能在启动我的Shiny应用程序时完全读取。我想在这个读取时间内显示一个弹出式等待消息,就像下面这样,当读取完成时,它会自动关闭。

然而,我不知道如何指定一个条件来自动关闭警报。
到目前为止,我已经完成了以下工作:

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
    )
}
英文:

I'am working on a pretty "big" dataset that takes at least 20 seconds to be read entirely at the launch of my Shiny app. I would like to display a pop-up waiting message like the one below during this reading time, which will close automatically when it is done.

However, I have no idea how to specify a condition to automatically close the alert.
Below what I have done so far :

server &lt;- function(input, output, session) {
   data =read_sav(&quot;BS.SAV&quot;)
 shinyalert(
      title = &quot;Wait&quot;,
      text = &quot;Waiting for data loading&quot;,
      size = &quot;xs&quot;,
      closeOnEsc = TRUE,
      closeOnClickOutside = TRUE,
      html = TRUE,
      type = &quot;info&quot;,
      showConfirmButton = TRUE,
      confirmButtonText = &quot;OK&quot;,
      confirmButtonCol = &quot;#004192&quot;,
      showCancelButton = FALSE,
      imageUrl = &quot;&quot;,
      animation = TRUE
    )
}

答案1

得分: 1

以下是翻译好的部分:

这是一个示例。可以使用 CloseAlert() 来关闭警报。.csv 是一个reactive,由 fileInput 读取。我们使用自定义的点击处理程序来检查是否点击了 fileInput 按钮。然后,输出最终呈现在 DT::datatable 中,当该对象最终呈现时,警报将关闭。

library(shiny)
library(shinyalert)

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

js <- HTML(
    "
$(function() {
  $('.shiny-input-container .input-group-btn .btn').on('click', function() {
     const id = $(this).find('input[type=\"file\"]').attr('id');
     Shiny.setInputValue(id + '_click', Math.random());
  })
})"
)

shinyApp(
    ui = fluidPage(
        singleton(tags$head(tags$script(js))),
        fileInput(
            inputId = 'file',
            label = '选择CSV文件',
            accept = c('text/csv',
                       'text/comma-separated-values,text/plain',
                       '.csv')
        ),
        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 = "等待",
                             text = "等待数据加载",
                             size = "xs",
                             closeOnEsc = TRUE,
                             closeOnClickOutside = TRUE,
                             html = TRUE,
                             type = "info",
                             showConfirmButton = TRUE,
                             confirmButtonText = "确定",
                             confirmButtonCol = "#004192",
                             showCancelButton = FALSE,
                             imageUrl = "",
                             animation = TRUE
                         )
                         
                     },
                     ignoreNULL = FALSE,
                     ignoreInit = TRUE)
    }
)

如何在R Shiny中加载数据时显示shinyalert弹出窗口?

英文:

Here is one example. The alert can be closed using CloseAlert(). The .csv is a reactive which gets read by a fileInput. We use a custom click handler in order to check whether the fileInput button gets clicked. The output gets then rendered into a DT::datatable and when this object is finally rendered, the alert gets closed.

library(shiny)
library(shinyalert)
options(shiny.maxRequestSize = 30 * 1024 ^ 2 * 2)
js &lt;- HTML(
&quot;
$(function() {
$(&#39;.shiny-input-container .input-group-btn .btn&#39;).on(&#39;click&#39;, function() {
const id = $(this).find(&#39;input[type=\&quot;file\&quot;]&#39;).attr(&#39;id&#39;);
Shiny.setInputValue(id + &#39;_click&#39;, Math.random());
})
})&quot;
)
shinyApp(
ui = fluidPage(
singleton(tags$head(tags$script(js))),
fileInput(
inputId = &#39;file&#39;,
label = &#39;Choose CSV File&#39;,
accept = c(&#39;text/csv&#39;,
&#39;text/comma-separated-values,text/plain&#39;,
&#39;.csv&#39;)
),
DT::dataTableOutput(&quot;table&quot;)
),
server = function(input, output, session) {
input_file &lt;- reactive({
if (is.null(input$file)) {
return(&quot;&quot;)
}
read.csv(file = input$file$datapath)
})
output$table &lt;- DT::renderDataTable({
req(input_file())
closeAlert()
input_file()
})
observeEvent(input$file_click,
{
shinyalert(
title = &quot;Wait&quot;,
text = &quot;Waiting for data loading&quot;,
size = &quot;xs&quot;,
closeOnEsc = TRUE,
closeOnClickOutside = TRUE,
html = TRUE,
type = &quot;info&quot;,
showConfirmButton = TRUE,
confirmButtonText = &quot;OK&quot;,
confirmButtonCol = &quot;#004192&quot;,
showCancelButton = FALSE,
imageUrl = &quot;&quot;,
animation = TRUE
)
},
ignoreNULL = FALSE,
ignoreInit = TRUE)
}
)

如何在R Shiny中加载数据时显示shinyalert弹出窗口?

huangapple
  • 本文由 发表于 2023年7月20日 21:39:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定