本文介绍了错误时再次循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图阅读所有内容,但是我有点陷入一个问题.通过使用bigrquery,我向Google BigQuery创建查询以获取数据-不幸的是,有时由于超时,我的查询无法正常工作.Q是一个SQL查询,BQ应该存储从BigQuery下载的数据.

I tried to read everything, but i kind of got stuck on one problem.By using bigrquery I create queries to Google BigQuery to get data - unfortunately sometimes my query doesn't work because of a time-out.Q is a SQL-Query and BQ is supposed to store the data downloaded from BigQuery.

有人知道每次tryCatch给我一个错误时如何重新做一个循环吗?

Does anybody know how to re-do a loop every time tryCatch gives me an error?

到目前为止,我已经知道了:

I got this so far:

BQ_Foo <- NULL
tryCatch(
{
repeat{
  BQ_Foo <- query_exec(Q_foo,"bigquery")
  if(is.list(BQ_Foo) == TRUE)break }
}
,error=function(e){cat("ERROR : Query not loaded!", "\n")}
)

我再次尝试了第一种方法,这次我收到以下错误消息:

I tried my first approach again and this time i received this error message:

有人知道如何处理吗?

推荐答案

广泛基于r2evans的答案,这是在withRestarts做同样事情的方法. www.win-vector.com/blog/2012/10/error-handling-in-r/?utm_source=rss&utm_medium=rss&utm_campaign=error-handling-in-r"rel =" noreferrer>此博客文章:

Widely based on r2evans answer, here's how to do the same kind of things with withRestarts, with some helps from This blog post:

set.seed(2)
foo <- NULL
operation <- function(x,tries) {
  message(paste("x is",x,"remaining tries",tries))
  withRestarts(
    tryCatch({
      if (runif(1) < x) stop("fail!") else 1
    },
    error=function(e) { invokeRestart("retry")}),
    retry = function() {
      message("Retrying")
      stopifnot(tries > 0)
      operation(x,tries-1)
    }
  )
}
> operation(0.9,5)
# x is 0.9 remaining tries 5
# Retrying
# x is 0.9 remaining tries 4
# Retrying
# x is 0.9 remaining tries 3
# Retrying
# x is 0.9 remaining tries 2
# Retrying
# x is 0.9 remaining tries 1
[1] 1

这是一种递归调用,因此您可以在再次调用该函数之前做任何想做的事情.

It's a kind of recursive call, so you can do whatever you want before calling the function again.

您可以以相同的方式在tryCatch错误处理程序中执行此操作,使用重新启动处理程序的兴趣在于调用特定函数,如果您有两个想要几乎相同的处理程序行为的tryCatch,则可以添加参数并使用每次尝试捕获都使用相同的处理程序,即:

You may do it in the tryCatch error handler the same way, the interest to use restarts handlers is to call a specific function, if you had two tryCatch for which you want nearly same handler behavior then you can add a parameter and use the same handler for each try catch, i.e.:

testfun <- function(x) {
  withRestarts({
    tryCatch(
      {
        ifelse(runif(1) < 0.5,stop("Error Message"),warning("Warning message"))
      },
      warning=function(e) { invokeRestart("logger", level="warning", message=e ) },
      error=function(e) { invokeRestart("logger", level="error", message=e ) }
    )
    },
    logger = function(level,message) {
      message(date()," [",level,"]: ",message[['message']])
    }
  )
}

给予:

> set.seed(2)
> testfun()
Fri Jul 29 14:15:11 2016 [error]: Error Message
> testfun()
Fri Jul 29 14:15:12 2016 [warning]: Warning message
> testfun()
Fri Jul 29 14:15:13 2016 [warning]: Warning message
> testfun()
Fri Jul 29 14:15:13 2016 [error]: Error Message

这里的主要兴趣是记录器方法的分解和减少代码重复.

Main interest here is the factorizing of the logger method and to reduce code duplication.

这篇关于错误时再次循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:06