【问题标题】:shiny throws error while loading excel files when csv file is supposed to be loaded当应该加载 csv 文件时,闪亮会在加载 excel 文件时抛出错误
【发布时间】:2017-11-07 16:58:28
【问题描述】:

我设计了一个使用闪亮的工具,要求用户上传通过单选按钮设计的 csv 或 xlsx 文件。现在有四个条件

  1. 他选择了csv,上传了一个csv文件,没错
  2. 选择 xlsx 并上传 xlsx 文件,同样正确
  3. 选择csv并上传xlsx文件,这是不正确的
  4. 选择 xlsx 并上传 csv 文件也不正确

现在对于第 3 点和第 4 点,我检查是否有任何不匹配,如果是则警告用户重新加载正确的文件类型。但是,第 4 点没有被检测到,并且工具通过其自身的错误。

用户界面代码

library(shiny)
library(xlsx)
library(tools)
shinyUI(fluidPage(
titlePanel("fileinput"),
sidebarLayout(
sidebarPanel(
  radioButtons("filetype", "Select file type",choices=c("csv file","xlsx file")),
  fileInput("file1","upload",accept = c("text/csv","text/comma-separated-values,text/plain",".csv",".xlsx",".xls"))

  ),
  mainPanel(
  uiOutput("Image_Data")
  )
  )
  )
  )

服务器代码

shinyServer(function(input, output) {
# loading data
data <- reactive({
if(input$filetype=="xlsx file"){
  inFile <- input$file1
  if (is.null(inFile))
    return(NULL)
  a<-read.xlsx(inFile$datapath, sheetIndex=1,header =TRUE)
  as.data.frame(a)
  }else{
  inFile <- input$file1
  if (is.null(inFile))
    return(NULL)
  a<-read.csv(inFile$datapath,header =TRUE,stringsAsFactors = TRUE)
  as.data.frame(a)
  }})
# info of the file
output$about_file<-renderTable({
input$file1})
# to display data
output$display <- renderDataTable({
data()})
# passing to UI finally
output$Image_Data<-renderUI({
if(is.null(data())){
h1("forecasting tool")
}
else if(input$filetype=="csv file" & input$file1$type != "text/csv"){
sprintf("reload correct file type")}
else if(input$filetype=="xlsx file" & input$file1$type != 
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"){
  sprintf("reload correct file type")}
  else if(input$filetype=="xlsx file" & input$file1$type != 
                                             "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"){
sprintf("reload correct file type")
}else{
tabsetPanel(tabPanel("DATA",dataTableOutput("display")), 
           tabPanel("aboufile", tableOutput("about_file")))
  }})})

我确实安装了 java 64 位,这是在 r 中使用 excel 文件所必需的。

【问题讨论】:

  • 将文件名作为文本字符串引入并检查扩展名,然后根据扩展名加载不是更容易吗?

标签: r shiny


【解决方案1】:

您的问题是您在检查数据类型之前正在读取文件。在读取文件之前尝试检查文件结尾。

例如,您可以检查一个文件是否是一个类似这样的 excel 文件

regexpr("\\.xlsx",input$file1) != -1

【讨论】:

  • @ Bertil else if(input$filetype=="xlsx file" & input$file1$type != "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"){ sprintf("重新加载正确文件类型")} 已被替换为 else if(regexpr("\\.xlsx",input$file1$type) != -1){sprintf("reload correct file type")} 但仍然抛出与之前相同的错误.
【解决方案2】:

很抱歉,从我的角度来看,这是一个基本的 RnD 缺乏。

问题不在于闪亮,而在于 read.xlsx。

read.xlsx 不会加载 xlsx 以外的任何其他文件类型,因此会引发错误,而 read.csv 不会引发任何错误(它给出警告)并加载其他类型(xlsx),尽管加载的文件在加载后变得无关紧要。

我所做的只是使用了 tryCatch 函数并在其他情况下进行了相应的修改。 下面是server.r的更正代码

shinyServer(function(input, output) {
# loading data
data <- reactive({
if(input$filetype=="xlsx file"){
  inFile <- input$file1
  if (is.null(inFile))
    return(NULL)
  a<-tryCatch(read.xlsx(inFile$datapath, sheetIndex=1,header =TRUE),error = function(e) return("wrong"))
  as.data.frame(a)
}else{
  inFile <- input$file1
  if (is.null(inFile))
    return(NULL)
  a<-read.csv(inFile$datapath,header =TRUE,stringsAsFactors = TRUE)
  as.data.frame(a)
}})
# info of the file
 output$about_file<-renderTable({
          input$file1})
 # to display data
 output$display <- renderDataTable({
 data()})
# passing to UI finally
output$Image_Data<-renderUI({
if(is.null(data())){
  h1("forecasting tool")
}else if(input$filetype =="csv file" & input$file1$type != "text/csv"){

  h4(br(),br(),"Mismatch in formats of file selected and uploaded!!",style = "color: red;")


}else if(length(data())==1){
  #h4(br(),br(),"Mismatch in formats of file selected and uploaded!",style = "font-style: italic;font-weight: bold;color: red;text-align: center;")
  h4(br(),br(),"Mismatch in formats of file selected and uploaded!",style = "color:red")
  #sprintf("testing")
}else{
  tabsetPanel(tabPanel("DATA",dataTableOutput("display")), 
              tabPanel("aboufile", tableOutput("about_file")))
}})})

【讨论】:

    猜你喜欢
    • 2015-04-06
    • 2020-10-30
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 2022-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多