How to allow R shiny to read .xlsx and csv files without conflicts?

0

Issue

I am trying to allow users to upload .xlsx and csv files but always get errors.

 ui:
fileInput(
              inputId = "file",
              label = "",
              multiple = TRUE,
              accept = c("text/csv",".xlsx",
                         "text/comma-separated-values,text/plain",
                         ".csv",
                         '.xlsx'),
              placeholder = "Use Ctrl key to choose files"
            ))



Server:

    read.csv(input$file$datapath) -Here, how to deal with .xlsx files

Solution

Because you can’t use read.csv to read Excel files, they are not CSV files.
Try read_excel from {readxl}.

So you can do things like, if it is csv you use read.csv, if it is xlsx, use readxl

You didn’t provide the full code, so here I can just give you some snippets:

if(stringr::str_ends(input$file$datapath, "csv")) {
    read.csv(input$file$datapath)
} else if (stringr::str_ends(input$file$datapath, "(xlsx|xls)")) {
    readxl::read_excel(input$file$datapath)
}

Answered By – lz100

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More