File Input

#| standalone: true
#| components: [viewer]
#| viewerHeight: 380

library(shiny)
library(bslib)

ui <- page_fixed(
  fileInput("file1", "Choose a File"),
  verbatimTextOutput("file1_contents")
)

server <- function(input, output) {
  output$file1_contents <- renderPrint({print(input$file1)})

}

shinyApp(ui, server)
library(shiny)
library(bslib)

ui <- page_fixed(
  fileInput("file1", "Choose a File"),
  verbatimTextOutput("file1_contents")
)

server <- function(input, output) {
  output$file1_contents <- renderPrint({print(input$file1)})

}

shinyApp(ui, server)
No matching items

Relevant Functions

No matching items

Details

Use the fileInput() function to provide an interface for users to upload a file. You can use the accept parameter to limit the file extension that can be uploaded.

To add a file upload button to your app:

  1. Add fileInput() to the UI of your app to create a file upload interface. Where you call this function will determine where the file upload interface will appear within the app’s layout.

  2. Specify the outputId and label parameters of fileInput() to define the identifier and label of the date selector.

  3. Optionally, provide an accept parameter to limit which file extensions the user can pick to upload.

In the server function of the app:

  1. You can use input$<fileInput_ID> to refer to the uploaded file. The uploaded file contains a list of 4 elements: name, size, type, and datapath.
  2. Decide what you plan to use the uploaded file for (e.g., load a csv into a dataframe, load an image as a matrix, read a plain text file, etc)
  3. Use the input$<fileInput_ID>$datapath to point to the temporary file created to load the file (e.g., read.csv() or readr::read_csv()).
  4. Use the loaded object in the corresponding reactive function or output function (e.g., renderPrint() or renderTable()).

See also: DataTable, Text, Verbatim Text

Variations

File Input Checking for CSV

Limit the file extension for CSV and check the input loads correctly

#| standalone: true
#| components: [viewer]
#| viewerHeight: 300

library(shiny)
library(bslib)

ui <- page_fixed(
  fileInput("file1", "Choose CSV File", accept = ".csv"),
  verbatimTextOutput("file1_contents"),
  tableOutput("contents")
)

server <- function(input, output) {
  output$file1_contents <- renderPrint({print(input$file1)})

  output$contents <- renderTable({
    file <- input$file1
    req(file)

    ext <- tools::file_ext(file$datapath)
    validate(need(ext == "csv", "Please upload a csv file"))

    read.csv(file$datapath)
  })
}

shinyApp(ui, server)
library(shiny)
library(bslib)

ui <- page_fixed(
  fileInput("file1", "Choose CSV File", accept = ".csv"),
  verbatimTextOutput("file1_contents"),
  tableOutput("contents")
)

server <- function(input, output) {
  output$file1_contents <- renderPrint({print(input$file1)})

  output$contents <- renderTable({
    file <- input$file1
    req(file)

    ext <- tools::file_ext(file$datapath)
    validate(need(ext == "csv", "Please upload a csv file"))

    read.csv(file$datapath)
  })
}

shinyApp(ui, server)
No matching items