Debian Codegraph @

man/downloadHandler.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/shinywrappers.R
3 \name{downloadHandler}
4 \alias{downloadHandler}
5 \title{File Downloads}
6 \usage{
7 downloadHandler(filename, content, contentType = NULL, outputArgs = list())
8 }
9 \arguments{
10 \item{filename}{A string of the filename, including extension, that the
11 user's web browser should default to when downloading the file; or a
12 function that returns such a string. (Reactive values and functions may be
13 used from this function.)}
14
15 \item{content}{A function that takes a single argument \code{file} that is a
16 file path (string) of a nonexistent temp file, and writes the content to
17 that file path. (Reactive values and functions may be used from this
18 function.)}
19
20 \item{contentType}{A string of the download's
21 \href{https://en.wikipedia.org/wiki/Internet_media_type}{content type}, for
22 example \code{"text/csv"} or \code{"image/png"}. If \code{NULL}, the content type
23 will be guessed based on the filename extension, or
24 \code{application/octet-stream} if the extension is unknown.}
25
26 \item{outputArgs}{A list of arguments to be passed through to the implicit
27 call to \code{\link[=downloadButton]{downloadButton()}} when \code{downloadHandler} is used
28 in an interactive R Markdown document.}
29 }
30 \description{
31 Allows content from the Shiny application to be made available to the user as
32 file downloads (for example, downloading the currently visible data as a CSV
33 file). Both filename and contents can be calculated dynamically at the time
34 the user initiates the download. Assign the return value to a slot on
35 \code{output} in your server function, and in the UI use
36 \code{\link[=downloadButton]{downloadButton()}} or \code{\link[=downloadLink]{downloadLink()}} to make the
37 download available.
38 }
39 \examples{
40 ## Only run examples in interactive R sessions
41 if (interactive()) {
42
43 ui <- fluidPage(
44 downloadButton("downloadData", "Download")
45 )
46
47 server <- function(input, output) {
48 # Our dataset
49 data <- mtcars
50
51 output$downloadData <- downloadHandler(
52 filename = function() {
53 paste("data-", Sys.Date(), ".csv", sep="")
54 },
55 content = function(file) {
56 write.csv(data, file)
57 }
58 )
59 }
60
61 shinyApp(ui, server)
62 }
63
64 }
65 \seealso{
66 \itemize{
67 \item The download handler, like other outputs, is suspended (disabled) by
68 default for download buttons and links that are hidden. Use
69 \code{\link[=outputOptions]{outputOptions()}} to control this behavior, e.g. to set
70 \code{suspendWhenHidden = FALSE} if the download is initiated by
71 programmatically clicking on the download button using JavaScript.
72 }
73 }