Uploads and Downloads

Lecture 27

Dr. Eric Friedlander

College of Idaho
CSCI 2025 - Winter 2026

File Uploads

UI: fileInput

  • fileInput(inputId, label, ...): Creates a file upload control.
  • Let’s look at the documentation with ?fileInput.
fileInput("upload", "Upload a file", multiple = TRUE)

Server: input$upload

  • Accessing the input returns a data frame with one row per file:
    • name: Original filename (e.g., “data.csv”).
    • size: Size in bytes.
    • type: MIME type (don’t usually need to worry about this).
    • datapath: Temporary path where the file is saved on the server.
  • Note: datapath is temporary. Read the data from there immediately.
output$contents <- renderTable({
  req(input$upload)
  read_csv(input$upload$datapath)
})

File Size Limits

  • Default limit is 5 MB.
  • Increase it by setting an option (usually at the top of app.R or server.R):
options(shiny.maxRequestSize = 30 * 1024^2) # 30 MB

File Downloads

UI: downloadButton

  • downloadButton(outputId, label): A button to initiate a download.
  • downloadLink(outputId, label): A link to initiate a download.
downloadButton("downloadData", "Download CSV")

Server: downloadHandler

  • Important: Use downloadHandler() NOT render*().
  • Arguments:
    • filename: A function that returns the filename (string).
    • content: A function that takes a file path argument and writes content to it.
output$downloadData <- downloadHandler(
  filename = function() {
    paste("data-", Sys.Date(), ".csv", sep="")
  },
  content = function(file) {
    write.csv(data(), file)
  }
)

Downloading Reports

  • You can also download generated reports (HTML, PDF, Word) using R Markdown or Quarto.
  • This is quite difficult so we won’t address it here.

Wrap Up

Mechanics

  • Upload:
    • UI: fileInput().
    • Server: input$id (dataframe w/ datapath).
  • Download:
    • UI: downloadButton().
    • Server: downloadHandler(filename, content).

Do Next

  1. Read Chapter 9: Uploads and Downlaods from Mastering Shiny.
  2. There’s NO recitation Gem for this textbook but I recommend creating your own and adding the textbook chapter and these slides.
  3. I reommend working through the Exercises in the textbook.
  4. That’s it for tonight! See you tomorrow!