View source: R/shiny-download-stream.R
| stream_download | R Documentation |
Push files to browser downloads using 'StreamSaver.js' for efficient
streaming of large files. This function sends file data in chunks to the browser,
which then writes them directly to disk without buffering the entire file in memory.
stream_download(
filepath,
filename = basename(filepath),
session = shiny::getDefaultReactiveDomain(),
chunk_size = 2 * 1024^2,
cleanup = FALSE,
method = c("streamsaver", "blob"),
quiet = FALSE
)
filepath |
path to the file to download; must be an existing file |
filename |
the file name to use for the download (defaults to the base name
of |
session |
the Shiny session; defaults to |
chunk_size |
size in bytes for each chunk; default is 2 MB |
cleanup |
whether to delete the source file after download completes;
default is |
method |
download method: |
quiet |
whether to suppress progress messages; default is |
This function requires the use_shiny_dipsaus() to be called in your
Shiny UI to load the necessary 'JavaScript' dependencies.
The "streamsaver" method (default) uses service workers to enable true
streaming downloads that start immediately without buffering the entire file.
This is ideal for large files (100MB+). The "blob" method is a fallback
that accumulates chunks in memory before triggering the download, which works
on more browsers but uses more memory.
Invisible NULL; the function is called for its side effects
'StreamSaver.js' requires the following files to be served from the same origin:
streamsaver/StreamSaver.js - Main library
streamsaver/sw.js - Service worker
streamsaver/mitm.html - Man-in-the-middle page
These files are automatically included when using use_shiny_dipsaus.
use_shiny_dipsaus, progress2
if(interactive()) {
library(shiny)
ui <- fluidPage(
use_shiny_dipsaus(),
actionButton("download_btn", "Download Large File")
)
server <- function(input, output, session) {
observeEvent(input$download_btn, {
# Create a temporary file to demonstrate
temp_file <- tempfile(fileext = ".txt")
writeLines(rep("Hello World!\n", 100000), temp_file)
# Stream the file to the browser
stream_download(
filepath = temp_file,
filename = "large_file.txt",
cleanup = TRUE # Delete temp file after download
)
})
}
shinyApp(ui, server)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.