闪亮验证read.csv

问题描述:

我想使用闪亮的验证函数来捕获读取错误,并显示自定义的错误消息,当读取上传的CSV文件,而不是让闪亮转发默认的read.csv错误消息。 下面是简单的代码闪亮验证read.csv

validate(need(try(sd <- read.csv(file = sdFile[1], stringsAsFactors = FALSE)), "Error reading the file")) 

当有CSV文件格式没问题,代码工作正常。但是,当csv文件出现问题时,代码仍会返回默认错误消息(以红色字体显示),例如,错误:未定义列被选中,但不是自定义消息。这里有什么问题?谢谢!

我认为它实际上是打印出来,如果我这样做:

library(shiny) 
validate(need(try(sd <- read.csv(file = "mtcars1.csv", 
           stringsAsFactors = FALSE)), 
           Error reading the file !!!")) 

产生:

Error in file(file, "rt") : cannot open the connection 
In addition: Warning message: 
In file(file, "rt") : 
    cannot open file 'mtcars1.csv': No such file or directory 
Error: Error reading the file !!! 

我得到这个 - 注意你的消息是在最后一行。

您可以supressWarnings抑制警告这样的:

library(shiny) 
suppressWarnings(
+ validate(need(try(sd <- read.csv(file = "mtcars1.csv", 
          stringsAsFactors = FALSE)), 
          "Error reading the file !!!!"))) 

产生:

Error in file(file, "rt") : cannot open the connection 
Error: Error reading the file !!!! 

或者你可以用这个剿一切,但你的信息(使用的tryCatch代替try):

library(shiny) 
suppressWarnings(
validate(need(tryCatch(sd <- read.csv(file = "mtcars1.csv", 
          stringsAsFactors = FALSE),  error=function (e){}), 
          "Error reading the file !!!!"))) 

yielding

Error: Error reading the file !!! 
+0

谢谢。我认为我的错误与读取csv文件无关,但与其他内容无关。是的,它应该可以正常工作。 – athlonshi