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)
Relevant Functions
-
File Upload Control - fileInput
fileInput(inputId, label, multiple = FALSE, accept = NULL, width = NULL, buttonLabel = "Browse...", placeholder = "No file selected", capture = NULL)
-
Check for required values - req
req(..., cancelOutput = FALSE)
-
Validate input values and other conditions — validate
validate(..., errorClass = character(0))
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:
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.Specify the
outputId
andlabel
parameters offileInput()
to define the identifier and label of the date selector.Optionally, provide an
accept
parameter to limit which file extensions the user can pick to upload.
In the server
function of the app:
- You can use
input$<fileInput_ID>
to refer to the uploaded file. The uploaded file contains a list of 4 elements:name
,size
,type
, anddatapath
. - 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)
- Use the
input$<fileInput_ID>$datapath
to point to the temporary file created to load the file (e.g.,read.csv()
orreadr::read_csv()
). - Use the loaded object in the corresponding reactive function or output function (e.g.,
renderPrint()
orrenderTable()
).
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)