Chapter 9. Uploads and Downloads

Transferring files to and from the user is a common feature of apps. You can use it to upload data for analysis or download the results as a dataset or as a report. This chapter shows the UI and server components that you’ll need to transfer files in and out of your app. We begin by loading shiny:

library(shiny)

Upload

We’ll start by discussing file uploads, showing you the basic UI and server components, and then showing how they fit together in a simple app.

UI

The UI needed to support file uploads is simple: just add fileInput() to your UI:

ui <- fluidPage(
  fileInput("upload", "Upload a file")
)

Like most other UI components, there are only two required arguments: id and label. The width, buttonLabel, and placeholder arguments allow you to tweak the appearance in other ways. I won’t discuss them here, but you can read more about them in ?fileInput.

Server

Handling fileInput() on the server is a little more complicated than other inputs. Most inputs return simple vectors, but fileInput() returns a data frame with four columns:

name

The original filename on the user’s computer.

size

The file size, in bytes. By default, the user can only upload files up to 5 MB. You can increase this limit by setting the shiny.maxRequestSize option prior to starting Shiny—to allow up to 10 MB run options(shiny.maxRequestSize = 10 * 1024^2), for example.

type

The “MIME type” of the file.1 This is a formal specification of the file type that ...

Get Mastering Shiny now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.