R shiny: Check if some download is available

2019-08-05 20:09发布

问题:

Suppose I have a shiny application that has a download button, and I want to check if that download button has received a file, how can I do it? Basically if the download button has a tag download1, I want to check if output$download1 is null. This is basically an example of what I'm trying to do. It's a simplified version of my current application, and I know that checking if output$download1 is null is not necessary in my case, but I need it for something else that I am doing. Is that doable?

This is my server file:

library(shiny)
library(shinyjs)
library(shinyBS)

shinyServer(function(input, output) {

  observe({

    shinyjs::disable("download1")

    if(is.null(input$file1))
      return()

    # download data
    data <- mtcars
    output$download1 <- downloadHandler(
      filename = function() {
        paste("data-", Sys.Date(), ".csv", sep="")
      },
      content = function(file) {
        write.csv(data, file)
      }
    )

    shinyjs::enable("download1")

    # Check if download is there
     if(!is.null(output$download1)){ # This is what doesn't work
       # Do something
     }

  })

})

and the ui file:

# Define UI for application that draws a histogram
shinyUI(fluidPage(

    fileInput("file1", "Upload file1"),
    downloadButton("download1"),
    helpText("Download after something is done.")

))
标签: r shiny shinyjs