输出数据帧的循环中的错误处理 [英] Error handling in a loop that outputs a dataframe

查看:54
本文介绍了输出数据帧的循环中的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在R中编写一个循环,预计会出现一些错误.我没有停止循环,而是想出一种方法来捕获错误消息,并将错误信息包含在输出中并继续循环.

I am trying to write a loop in R where I am anticipating some errors. Instead of stopping the loop, I am trying to figure out a way to capture the error message and include that information in the output and continue the loop.

关于数据的注意:数据是来自NOAA网站,它是南方涛动指数数据.第二组是琐碎的(data2),仅显示是为了产生错误.

NOTE about data: This data is from the NOAA website and it is the Southern Oscillation Index data. The second set is trivial (data2) and is presented solely to generate an error.

这是我尝试创建的循环类型的一个小例子.从网上获取一些数据,对其进行一些处理,将其存储(作为df),然后获取更多数据,执行相同的处理并将其(通过rbind)附加到第一个数据:

Here is a slightly trivial example of the type of loop I am trying to create. Take some data from the web, perform some manipulations on it, store it (as df) then get more data, perform the same manipulation and append it (via rbind) to the first data:

data_spec <- c("data")
df <- c()
for (i in data_spec){
  raw <- read.csv(
    paste0("https://www.ncdc.noaa.gov/teleconnections/enso/indicators/soi/",i,".csv"),
           skip = 2, col.names = c("Date","SOI") )
  u <- data.frame(data_spec = i, mean_soi = mean(raw$SOI))
  df <- rbind(df, u)
}

因为 https://www.ncdc.noaa. gov/teleconnections/enso/indicators/soi/data2.csv 不是有效的网址,循环停止并引发错误:

Because https://www.ncdc.noaa.gov/teleconnections/enso/indicators/soi/data2.csv is not a valid url, the loop stops and throws an error:

file(file,"rt")中的错误:无法打开连接另外: 警告消息:在file(file,"rt")中:无法打开URL ' https://www.ncdc.noaa.gov/teleconnections /enso/indicators/soi/data2.csv ': HTTP状态为"404未找到"

Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open URL 'https://www.ncdc.noaa.gov/teleconnections/enso/indicators/soi/data2.csv': HTTP status was '404 Not Found'

预期产量

我正在尝试实现这样的输出,其中将错误消息捕获为对象并相应地附加:

Expected Output

I am trying to achieve an output like this where the error message is captured as object and appended accordingly:

  data_spec                                             mean_soi
1      data                                            0.1223618
2     data2 Error in file(file, rt) : cannot open the connection

试图解决这个问题

所以我想我很清楚我需要在这里使用tryCatch.如果我这样使用它:

Attempts to figure this out

So I think I am clear that I need to use tryCatch here. If I use it like so:

data_spec <- c("data", "data2")
df <- c()
for (i in data_spec){
  tryCatch({
  raw <- read.csv(
    paste0("https://www.ncdc.noaa.gov/teleconnections/enso/indicators/soi/",i,".csv"),
    skip = 2, col.names = c("Date","SOI") )
  u <- data.frame(data_spec = i, mean_soi = mean(raw$SOI))
  df <- rbind(df, u)
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}

循环继续进行,但是错误消息未在输出中捕获(不是我在这里期望的那样).

the loop continues along, but the error message isn't captured in the output (not that I was expecting this here).

另一个选择是使用从demo(error.catching)输出的功能.我对该函数做了一些修改,以便捕获错误消息:

Another option is to use the function outputted from: demo(error.catching). I modified that function a little so that the error message is captured:

tryCatch_mod <- function(expr)
{
  W <- NULL
  w.handler <- function(w){ # warning handler
    W <<- w
    invokeRestart("muffleWarning")
  }
  temp <- list(value = withCallingHandlers(tryCatch(expr, error = function(e) e),
                                   warning = w.handler),
       warning = W)

  unlist(temp[[2]])$message
}

使用"data2"时输出错误:

This outputs an error when using "data2":

tryCatch_mod(read.csv("https://www.ncdc.noaa.gov/teleconnections/enso/indicators/soi/data2.csv",
                      skip = 2, col.names = c("Date","SOI")))

我不知道的事情

我如何包括此功能(或完成相同功能的功能),以便输出是有条件的或是否有错误?就是说,我该如何写出本质上说的函数:

What I can't figure out

How do I include this function (or something that accomplishes the same thing) so that the output is conditional or whether there is a error? So that is, how do I write my function that it essentially says:

  • 出现错误时,请跳过所有操作,并将i和错误消息附加到df
  • 没有错误时,执行操作并将结果附加到同一df
  • When there is an error, skip any manipulations and append i and the error message into df
  • When there is NOT an error, perform the manipulation and append results onto the same df

推荐答案

我重新创建您的data.frame,使其包括另外一列以存储错误消息,因为在R data.frame中,一列应存储一种类型的东西.将错误消息和其他内容存储在一个列中将是一团糟,并且它们的类型将被强制为相同. handle_i功能是要在每个i上执行的功能.错误处理是tryCatch行,它会在发生错误时返回您要存储的错误消息(但它对警告没有任何作用).最后,它将相应的内容存储到data.frame中.

I recreate your data.frame to include one additional column to store error messages, because in R data.frame, one column should store one type of things. It will be a mess to store error messages and other things in one column, and their type will be coerced to be the same. The handle_i function is the function you want to carry out on each i. The error-handling is the tryCatch line, it returns the error message you want to store whenever error happens (but it doesn't do anything about warning). And finally it will store the things correspondingly to the data.frame.

handle_i <- function(i){
    raw <- read.csv(
        paste0("https://www.ncdc.noaa.gov/teleconnections/enso/indicators/soi/",i,".csv"),
        skip = 2, col.names = c("Date","SOI") )
    list(mean_soi = mean(raw$SOI))
}

data_spec <- c("data", "data2")
df <- data.frame(data_spec = data_spec, mean_soi = NA, message = "", stringsAsFactors = FALSE)
for (i in 1:length(data_spec)) {
    r <- tryCatch(handle_i(data_spec[i]), error = function(e) list(message = e$message))
    df[i, names(r)] <- r
}

这篇关于输出数据帧的循环中的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆