R 闪亮:显示“正在加载...";函数运行时的消息 [英] R shiny: display "loading..." message while function is running

查看:56
本文介绍了R 闪亮:显示“正在加载...";函数运行时的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Shiny GUI R 包.我正在寻找一种在按下 actionButton 后显示正在加载..."之类的消息的方法.该函数需要几分钟才能执行,所以我需要以某种方式通知用户该按钮实际上触发了某个事件.现在 server.R 代码如下所示:

I use Shiny GUI R package. I'm looking for a way to display a message like "loading..." after the actionButton was pressed. The function takes several minutes to execute, so I need to inform the user somehow that the button actually triggered some event. Now the server.R code looks like this:

DATA <- reactive({
  if(input$DownloadButton>0) {
    RunDownload()
  } else {
    NULL
  }
})

output$Download <- renderText({
  if(NROW(DATA())>0) {
    paste0(Sys.time(),": ",NROW(DATA()), " items downloaded")
  } else {
    ''
  }
})

actionButton() 是一个从互联网下载数据的函数.input$DownloadButton 是 actionButton.所以按下按钮后,用户等待几分钟,然后才看到一条消息,说下载成功.我想在按下 actionButton 后显示一条消息正在加载...",然后显示另一条消息,如 paste0(Sys.time(),": ",NROW(DATA()), " items loaded") 执行结束时.

actionButton() is a function that downloads data from internet. input$DownloadButton is actionButton. So after the button was pressed the user waits for several minutes and only then sees a message saying that download was successful. I would like to show a message "Loading..." just after the actionButton was pressed and then another message like paste0(Sys.time(),": ",NROW(DATA()), " items downloaded") when execution ends.

推荐答案

我已经在使用一种比我之前发布的方法更简单、更可靠的方法.

I'm already using a simpler and more reliable way than the one I posted before.

组合

tags$style(type="text/css", "
           #loadmessage {
             position: fixed;
             top: 0px;
             left: 0px;
             width: 100%;
             padding: 5px 0px 5px 0px;
             text-align: center;
             font-weight: bold;
             font-size: 100%;
             color: #000000;
             background-color: #CCFF66;
             z-index: 105;
           }
  ")

conditionalPanel(condition="$('html').hasClass('shiny-busy')",
                 tags$div("Loading...",id="loadmessage")
)

示例:

runApp(list(
  ui = pageWithSidebar(
      headerPanel("Test"),
         sidebarPanel(
           tags$head(tags$style(type="text/css", "
             #loadmessage {
               position: fixed;
               top: 0px;
               left: 0px;
               width: 100%;
               padding: 5px 0px 5px 0px;
               text-align: center;
               font-weight: bold;
               font-size: 100%;
               color: #000000;
               background-color: #CCFF66;
               z-index: 105;
             }
          ")),
           numericInput('n', 'Number of obs', 100),
           conditionalPanel(condition="$('html').hasClass('shiny-busy')",
                            tags$div("Loading...",id="loadmessage"))
         ),
         mainPanel(plotOutput('plot'))
  ),
  server = function(input, output) {
    output$plot <- renderPlot({ Sys.sleep(2); hist(runif(input$n)) })
  }
))

tags$head() 不是必需的,但将所有样式保留在 head 标签内是一个好习惯.

tags$head() is not required, but it's a good practice to keep all the styles inside head tag.

这篇关于R 闪亮:显示“正在加载...";函数运行时的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆