read csv files and excel files in shiny

Hello i want to include in this upload option in my shiny app an option to both read csv files and excel files , i can't figure it out , Thank you in advance.

  data = reactive({
    file1 = input$file
    if(is.null(file1)) {return(NULL)}
    if('.csv' %in% (file1$datapath)){
      data = read.csv(file1$datapath)
        data = read_excel(file1$datapath)

Here is an example you can run and explore to upload a .csv file

#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#>     dataTableOutput, renderDataTable

# Define UI
ui <- shinyUI(fluidPage(
  fileInput('target_upload', 'Choose file to upload',
            accept = c(
  radioButtons("separator","Separator: ",choices = c(";",",",":"), selected=";",inline=TRUE),

# Define server logic
server <- shinyServer(function(input, output) {
  df_products_upload <- reactive({
    inFile <- input$target_upload
    if (is.null(inFile))
    df <- read.csv(inFile$datapath, header = TRUE,sep = input$separator)
  output$sample_table<- DT::renderDataTable({
    df <- df_products_upload()

# Run the application 
shinyApp(ui = ui, server = server)

Shiny applications not supported in static R Markdown documents

Created on 2022-01-11 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.