[英]How to allow R shiny to read .xlsx and csv files without conflicts?
I am trying to allow users to upload .xlsx and csv files but always get errors.我试图让用户上传 .xlsx 和 csv 文件,但总是出错。
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
Because you can't use read.csv
to read Excel files, they are not CSV files.因为您不能使用
read.csv
读取 Excel 文件,所以它们不是 CSV 文件。 Try read_excel
from { readxl }.从 { readxl } 尝试
read_excel
。
So you can do things like, if it is csv you use read.csv
, if it is xlsx
, use readxl
所以你可以做这样的事情,如果它是 csv 你使用
read.csv
,如果它是xlsx
,使用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)
}
Try this:试试这个:
inFile <- input$file
extension <- tools::file_ext(inFile$name)
filepath <- inFile$datapath
df <- switch(extension,
csv = readr::read_csv(filepath),
xls = readxl::read_xls(filepath),
xlsx = readxl::read_xlsx(filepath)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.