【问题标题】:Error / Exception Handling with empty files in RR中空文件的错误/异常处理
【发布时间】:2014-08-26 09:07:22
【问题描述】:

两天前我第一次在 python 中听说异常处理,因此我想在 R 中申请。我在堆栈溢出或其他一些在线问答中查看了一些问题,但我仍然真的在使用它时感到困惑。

如果有人能用这个简单的例子回答这个问题,我将不胜感激,以便稍后我可以将它应用到我的问题中。

例如,我有 3 个文件名如下所示的数据文件; 第一个文件是一个 0 字节的空文件。我可以做些什么来继续为所有文件运行循环并且从空文件中提取的数字可以表示为NA?

    > output_names_hdf5_list[1:5]
    [1] "simulation-results fL=0.1,fks=1,fno=0.1,fnc=0.1,fr=0.1,fs=0.1.hdf5"  
    [2] "simulation-results fL=0.1,fks=1,fno=0.1,fnc=0.1,fr=0.1,fs=1.05.hdf5" 
    [3] "simulation-results fL=0.1,fks=1,fno=0.1,fnc=0.1,fr=0.1,fs=2.hdf5"    

    for (i in 1:5){
        channelflow_outlet[,i]=h5read(paste(outputdir, output_names_hdf5_list[i], sep=""),"Channel")$Qc_out[460,][2:100]
    }

使用try 函数,我可以设法运行程序而不会卡在错误消息中,但是当我在try 函数中用channelflow_outlet[,i]= h5read(....) 替换参数时,它只会返回错误。

    for (i in 1:5){
        try(h5read(paste(outputdir, output_names_hdf5_list[i], sep=""),"Channel")$Qc_out[460,][2:100])
    }

如果没有错误处理,就会有这样的错误信息。

    > h5read(paste(outputdir, output_names_hdf5_list[1], sep=""),"Channel")$Qc_out[460,][2:100]
    HDF5: unable to open file
    Error in h5checktypeOrOpenLoc(file, readonly = TRUE) : 
      Error in h5checktypeOrOpenLoc(). File 'D:/Data/Mleonard/pytopkapi.staged.makefile/RunModel/Output/3x6-729-04072014/simulation-results fL=0.1,fks=1,fno=0.1,fnc=0.1,fr=0.1,fs=0.1.hdf5' is not a valid HDF5 file.
    > 

【问题讨论】:

  • 参见?tryCatch 中的示例。您可以即时指定错误或警告的行为。
  • @Roman,我已经尝试使用tryCatch() 并将h5read() 读取文件部分重写为一个函数,但它仍然不起作用。结果全部返回 NA。

标签: r exception exception-handling error-handling try-catch


【解决方案1】:

我希望我的代码有所帮助。对于代码中的这些消息,您可以根据需要将其删除。它们在这里纯粹是为了帮助您查看它在哪里显示警告或错误。

   setwd("D:/Dropbox/Test/"); outputdir = "D:/Dropbox/Test/"

   output_names_hdf5_list=c("simulation-results fL=0.1,fks=1,fno=1.05,fnc=1.05,fr=1.05,fs=1.05.hdf5",
             "simulation-results fL=0.1,fks=1,fno=1.05,fnc=2,fr=1.05,fs=1.05.hdf5",
             "simulation-results fL=0.1,fks=1,fno=2,fnc=1.05,fr=0.1,fs=1.05.hdf5",
             "simulation-results fL=0.1,fks=1,fno=2,fnc=1.05,fr=2,fs=2.hdf5",
             "simulation-results fL=0.5,fks=1,fno=2,fnc=2,fr=0.1,fs=1.05.hdf5")

   channelflow_outlet = matrix(NA, nrow=100, ncol=5)

   hdf5_list_reading_tool= function(output_names_hdf5_list) {
    out = tryCatch(
       { 
            message("This is the 'try' part")
            h5read(paste(outputdir, output_names_hdf5_list, sep=""),"Channel")$Qc_out[460,][2:100]
       },
       error=function(cond) {
            message("Here's the original error message:")
            message(cond)
            return(rep(NA,100))
       },
       warning=function(cond) {
            message("Here's the original warning message:")
            message(cond)
            return(rep(NA,100))
       },
       finally={
            message(paste("Processed URL:", output_names_hdf5_list))
            message("Some other message at the end")
       }
    )
    return(out)
    }

    channelflow_outlet=sapply(output_names_hdf5_list, hdf5_list_reading_tool)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 2011-04-03
    • 2014-01-08
    相关资源
    最近更新 更多