using try vs TryCatch in R

huangapple go评论109阅读模式
英文:

using try vs TryCatch in R

问题

我的R代码如下:

  1. errors = 0
  2. for(i in c(1:100)){
  3. tryCatch(expr = {
  4. API调用
  5. }, error = {errors=errors+1}, finally = {})
  6. 后续代码
  7. }

这段代码的目的是即使API调用失败,也要继续执行,并计算出错误发生的次数。
但是我发现,如果在某次迭代中API调用出现错误,那么后续的迭代代码将不会执行。
如果我使用try(),那么我将无法计算错误的次数。

英文:

My R code is as follows

  1. errors = 0
  2. for(i in c(1:100)){
  3. tryCatch(expr = {
  4. API Call
  5. }, error = {errors=errors+1}, finally = {})
  6. further code
  7. }

The code is meant to continue execution even if the API call fails and count the number of times error occurred.
But I see that if for an iteration there is an error in API call, the code is not executed for further iterations.
If I use try(), then I wont be able to count the number of errors.

答案1

得分: 3

tryCatch函数的error=参数应该是一个函数。如果在for循环之前预先实例化errors <- 0,有几种选择:

我更喜欢的是捕获并检查是否继承了"error"

  1. errors <- 0L
  2. for (...) {
  3. res <- tryCatch({
  4. ...API调用...
  5. },
  6. error = function(e) e)
  7. if (inherits(res, "error")) {
  8. errors <- errors + 1L
  9. } else {
  10. 这里是其他操作
  11. }
  12. }

等效的方式是使用try

  1. errors <- 0L
  2. for (...) {
  3. res <- try({
  4. ...API调用...
  5. }, silent = TRUE)
  6. if (inherits(res, "try-error")) {
  7. errors <- errors + 1L
  8. } else {
  9. 这里是其他操作
  10. }
  11. }

虽然我更喜欢这种方式可能主要是主观的原因。另一种方式是更内部的:

  1. errors <- 0L
  2. for (...) {
  3. res <- tryCatch({
  4. ...API调用...
  5. },
  6. error = function(e) { errors <<- errors + 1L; })
  7. 这里是其他操作
  8. }
英文:

The error= argument of tryCatch should be a function. If you pre-instantiate errors &lt;- 0 before the for loop, there are a couple of options:

My preferred is to catch and check to see if it inherits &quot;error&quot;:

  1. errors &lt;- 0L
  2. for (...) {
  3. res &lt;- tryCatch({
  4. ...API call...
  5. },
  6. error = function(e) e)
  7. if (inherits(res, &quot;error&quot;)) {
  8. errors &lt;- errors + 1L
  9. } else {
  10. something else here
  11. }
  12. }

Equivalently with try:

  1. errors &lt;- 0L
  2. for (...) {
  3. res &lt;- try({
  4. ...API call...
  5. }, silent = TRUE)
  6. if (inherits(res, &quot;try-error&quot;)) {
  7. errors &lt;- errors + 1L
  8. } else {
  9. something else here
  10. }
  11. }

Though the reasons I prefer that may be mostly subjective. Another way would be more internal:

  1. errors &lt;- 0L
  2. for (...) {
  3. res &lt;- tryCatch({
  4. ...API call...
  5. },
  6. error = function(e) { errors &lt;&lt;- errors + 1L; })
  7. something else here
  8. }

答案2

得分: 0

  1. # 设置场景
  2. api_call <- function(in_1){
  3. if(in_1 %in% c(5,7)){
  4. stop("bad num")
  5. }
  6. in_1 + 0.5
  7. }
  8. for(i in 1:10){
  9. print(api_call(i))
  10. }
  11. # 封装函数
  12. library(purrr)
  13. safe_api_call <- safely(api_call)
  14. for(i in 1:10){
  15. print(safe_api_call(i)$result)
  16. }
  17. # 添加错误计数
  18. error_count <- 0
  19. for(i in 1:10){
  20. r <- safe_api_call(i)
  21. print(r$result)
  22. error_count <- error_count + inherits(r$error, "error")
  23. }
  24. error_count

以上是要翻译的内容。

英文:
  1. # set up scenario
  2. api_call &lt;- function(in_1){
  3. if(in_1 %in% c(5,7)){
  4. stop(&quot;bad num&quot;)
  5. }
  6. in_1+.5
  7. }
  8. for(i in 1:10){
  9. print(api_call(i))
  10. }
  11. # wrap it up
  12. library(purrr)
  13. safe_api_call &lt;- safely(api_call)
  14. for(i in 1:10){
  15. print(safe_api_call(i)$result)
  16. }
  17. # add counting of errors
  18. error_count &lt;- 0
  19. for(i in 1:10){
  20. r &lt;- safe_api_call(i)
  21. print(r$result)
  22. error_count &lt;- error_count + inherits(r$error,&quot;error&quot;)
  23. }
  24. error_count
  25. </details>

huangapple
  • 本文由 发表于 2023年8月9日 03:13:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定