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

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

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

问题

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

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

  1. server <- function(input, output, session) {
  2. data =read_sav("BS.SAV")
  3. shinyalert(
  4. title = "Wait",
  5. text = "Waiting for data loading",
  6. size = "xs",
  7. closeOnEsc = TRUE,
  8. closeOnClickOutside = TRUE,
  9. html = TRUE,
  10. type = "info",
  11. showConfirmButton = TRUE,
  12. confirmButtonText = "OK",
  13. confirmButtonCol = "#004192",
  14. showCancelButton = FALSE,
  15. imageUrl = "",
  16. animation = TRUE
  17. )
  18. }
英文:

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 :

  1. server &lt;- function(input, output, session) {
  2. data =read_sav(&quot;BS.SAV&quot;)
  3. shinyalert(
  4. title = &quot;Wait&quot;,
  5. text = &quot;Waiting for data loading&quot;,
  6. size = &quot;xs&quot;,
  7. closeOnEsc = TRUE,
  8. closeOnClickOutside = TRUE,
  9. html = TRUE,
  10. type = &quot;info&quot;,
  11. showConfirmButton = TRUE,
  12. confirmButtonText = &quot;OK&quot;,
  13. confirmButtonCol = &quot;#004192&quot;,
  14. showCancelButton = FALSE,
  15. imageUrl = &quot;&quot;,
  16. animation = TRUE
  17. )
  18. }

答案1

得分: 1

以下是翻译好的部分:

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

  1. library(shiny)
  2. library(shinyalert)
  3. options(shiny.maxRequestSize = 30 * 1024 ^ 2 * 2)
  4. js <- HTML(
  5. "
  6. $(function() {
  7. $('.shiny-input-container .input-group-btn .btn').on('click', function() {
  8. const id = $(this).find('input[type=\"file\"]').attr('id');
  9. Shiny.setInputValue(id + '_click', Math.random());
  10. })
  11. })"
  12. )
  13. shinyApp(
  14. ui = fluidPage(
  15. singleton(tags$head(tags$script(js))),
  16. fileInput(
  17. inputId = 'file',
  18. label = '选择CSV文件',
  19. accept = c('text/csv',
  20. 'text/comma-separated-values,text/plain',
  21. '.csv')
  22. ),
  23. DT::dataTableOutput("table")
  24. ),
  25. server = function(input, output, session) {
  26. input_file <- reactive({
  27. if (is.null(input$file)) {
  28. return("")
  29. }
  30. read.csv(file = input$file$datapath)
  31. })
  32. output$table <- DT::renderDataTable({
  33. req(input_file())
  34. closeAlert()
  35. input_file()
  36. })
  37. observeEvent(input$file_click,
  38. {
  39. shinyalert(
  40. title = "等待",
  41. text = "等待数据加载",
  42. size = "xs",
  43. closeOnEsc = TRUE,
  44. closeOnClickOutside = TRUE,
  45. html = TRUE,
  46. type = "info",
  47. showConfirmButton = TRUE,
  48. confirmButtonText = "确定",
  49. confirmButtonCol = "#004192",
  50. showCancelButton = FALSE,
  51. imageUrl = "",
  52. animation = TRUE
  53. )
  54. },
  55. ignoreNULL = FALSE,
  56. ignoreInit = TRUE)
  57. }
  58. )

如何在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.

  1. library(shiny)
  2. library(shinyalert)
  3. options(shiny.maxRequestSize = 30 * 1024 ^ 2 * 2)
  4. js &lt;- HTML(
  5. &quot;
  6. $(function() {
  7. $(&#39;.shiny-input-container .input-group-btn .btn&#39;).on(&#39;click&#39;, function() {
  8. const id = $(this).find(&#39;input[type=\&quot;file\&quot;]&#39;).attr(&#39;id&#39;);
  9. Shiny.setInputValue(id + &#39;_click&#39;, Math.random());
  10. })
  11. })&quot;
  12. )
  13. shinyApp(
  14. ui = fluidPage(
  15. singleton(tags$head(tags$script(js))),
  16. fileInput(
  17. inputId = &#39;file&#39;,
  18. label = &#39;Choose CSV File&#39;,
  19. accept = c(&#39;text/csv&#39;,
  20. &#39;text/comma-separated-values,text/plain&#39;,
  21. &#39;.csv&#39;)
  22. ),
  23. DT::dataTableOutput(&quot;table&quot;)
  24. ),
  25. server = function(input, output, session) {
  26. input_file &lt;- reactive({
  27. if (is.null(input$file)) {
  28. return(&quot;&quot;)
  29. }
  30. read.csv(file = input$file$datapath)
  31. })
  32. output$table &lt;- DT::renderDataTable({
  33. req(input_file())
  34. closeAlert()
  35. input_file()
  36. })
  37. observeEvent(input$file_click,
  38. {
  39. shinyalert(
  40. title = &quot;Wait&quot;,
  41. text = &quot;Waiting for data loading&quot;,
  42. size = &quot;xs&quot;,
  43. closeOnEsc = TRUE,
  44. closeOnClickOutside = TRUE,
  45. html = TRUE,
  46. type = &quot;info&quot;,
  47. showConfirmButton = TRUE,
  48. confirmButtonText = &quot;OK&quot;,
  49. confirmButtonCol = &quot;#004192&quot;,
  50. showCancelButton = FALSE,
  51. imageUrl = &quot;&quot;,
  52. animation = TRUE
  53. )
  54. },
  55. ignoreNULL = FALSE,
  56. ignoreInit = TRUE)
  57. }
  58. )

如何在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:

确定