Learn R Programming

yonder (version 0.0.5)

downloadEvent: Trigger downloads

Description

downloadEvent allows a custom reactive event to trigger a file download.

Usage

downloadEvent(event, filename, handler,
  domain = getDefaultReactiveDomain())

Arguments

event

A reactive input value (e.g. input$click), a call to a reactive expression, or a new expression inside curly braces. When event is triggered the file download is triggered.

filename

A reactive input value, a call to a reactive expression, a function, or a new expression inside curly braces which returns a string specifying the name of the downloaded file.

handler

A function with one argument that write's the content of the downloaded file. A temporary file is passed to the function, which is expected to write content (text, images, etc.) to the temporary file.

domain

A shiny session object, defaults to getDefaultReactiveDomain().

Downloading a file

shinyApp(
  ui = container(
    textInput("name", "File name"),
    buttonInput("trigger", "Download")
  ),
  server = function(input, output) {
    downloadEvent(input$trigger, {
      if (is.null(input$name)) {
        "default"
      } else {
        input$name
      }
    }, function(file) {
      cat("hello, world!", file = file)
    })
  }
)

See Also

Other utilities: enableInput, invalidateInput, updateInput