闪亮的应用程序:禁用下载按钮 [英] shiny app : disable downloadbutton

查看:89
本文介绍了闪亮的应用程序:禁用下载按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的闪亮应用程序会生成一些用户可以下载的文件。为此,我已将下载按钮放在ui中。但是,当页面启动时,在完成任何计算之前,没有任何内容可供下载。我想阻止用户下载空白页。

My shiny app produces some files that user can download. I have put downloadbutton in the ui for this purpose. However, when the page launches and before any calculation is done, there is nothing to download. I want to prevent user from downloading empty pages.

为此,我想在输出准备好之前禁用downloadButton。但我不知道该怎么做。我已经找到了禁用ActionButton的方法(比如ShinyBS包和其他JS代码),但是没有用于downloadButton的方法。

For this, I'm thinking to disable the downloadButton before the output is ready. But I don't know how to do that. I have found ways to disable ActionButton (such as ShinyBS package and other JS codes), but nothing for downloadButton.

现在,如果输出没有准备好,我会使用validate()来抛出错误。但是,当单击downloadButton时,会打开一个新的空网页,其中包含一条很难看的错误信息。

Right now, I use validate() to throw errors if the output is not ready. However, when the downloadButton is clicked, a new empty web page opens with an error message in it which is ugly.

让我知道你的想法。

这是我的ui代码

 downloadButton('download', 'Download Lasso component matrix')),

这是我的服务器代码:

  output$download_matrix <- downloadHandler(
      filename = function() { 
      validate(
      need(is.null(outputData())==FALSE, "No data to download yet")
      )
      paste('combined_model_matrix', '.txt', sep='') },
    content = function(file) {
      write.csv(outputData()$combinedAdjMtr, file)
})


推荐答案

根据您的评论:


是数据处理取决于用户输入。 USer将上传一些文件并单击anAction按钮开始处理。下载按钮位于选项卡集中。

yes data processing depends on the user input. USer will upload some files and click anAction button to start the processing. The download button is in a tab set.

假设操作按钮名为 input $ start_proc

shinyServer(function(input, output, session) {
   #... other code
   observe({
       if (input$start_proc > 0) {
           # crunch data...
           # when data is ready:
           session$sendCustomMessage("download_ready", list(...))
           # you can put extra information you want to send to the client 
           # in the ... part.
       } 
   })
   #... other code
})

然后在 ui.R 中,您可以编写一些javascript来处理自定义消息事件。

Then in ui.R, you can write some javascript to handler the custom message event.

一个完整的例子是:

library(shiny)

fakeDataProcessing <- function(duration) {
  # does nothing but sleep for "duration" seconds while
  # pretending some background task is going on...
  Sys.sleep(duration)
}

shinyServer(function(input, output, session) {

  observe({
    if (input$start_proc > 0) {
      fakeDataProcessing(5)
      # notify the browser that the data is ready to download
      session$sendCustomMessage("download_ready", list(fileSize=floor(runif(1) * 10000)))
    }
  })

  output$data_file <- downloadHandler(
       filename = function() {
         paste('data-', Sys.Date(), '.csv', sep='')
       },
       content = function(file) {
         write.csv(data.frame(x=runif(5), y=rnorm(5)), file)
       }
  )
})



ui.R



ui.R

library(shiny)

shinyUI(fluidPage(
  singleton(tags$head(HTML(
'
  <script type="text/javascript">
    $(document).ready(function() {
      // disable download at startup. data_file is the id of the downloadButton
      $("#data_file").attr("disabled", "true").attr("onclick", "return false;");

      Shiny.addCustomMessageHandler("download_ready", function(message) {
        $("#data_file").removeAttr("disabled").removeAttr("onclick").html(
          "<i class=\\"fa fa-download\\"></i>Download (file size: " + message.fileSize + ")");
      });
    })
  </script>
'
))),
  tabsetPanel(
    tabPanel('Data download example',
      actionButton("start_proc", h5("Click to start processing data")),
      hr(),

      downloadButton("data_file"),
      helpText("Download will be available once the processing is completed.")
    )
  )
))

在示例中,数据处理是等待伪造的持续5秒。
然后下载按钮就绪了。我还在邮件中添加了一些假 fileSize 信息,以证明您可以向用户发送额外信息。

In the example the data processing is faked by waiting for 5 seconds. Then the download button will be ready. I also added some "fake" fileSize information in the message to demonstrate that how you can send extra information to the user.

请注意,因为Shiny将 actionButton 实现为< a> 标记而不是< ;按钮> ,并绑定点击事件。因此,为了完全禁用它,除了添加禁用属性以使其显示为禁用之外,还需要覆盖其单击事件通过添加内联 onclick 属性。否则,用户仍然可能会意外点击(看似已禁用)下载按钮并触发下载。

Note that because Shiny implements actionButton as <a> tag instead of <button>, and it binds click event on it. Therefore, in order to fully disable it, in addition to add a disabled attribute to make it appear to be disabled, you also need to override its click event by adding an inline onclick attribute. Otherwise the user can still accidentally click the (seemingly disabled) download button and triggers the download.

这篇关于闪亮的应用程序:禁用下载按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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