提取循环中错误发生的顺序。

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

Extract in which sequence a loop created an error

问题

我有一个循环来测试函数中的不同值。我想知道哪些值会在后续检查中出现问题。所以我想保存循环的轮次,然后继续循环。
我创建了一些虚拟数据和一个空向量来保存循环的轮次:

stuff <- c(12, 9, 2, "cat", 25, 10, "horse")
troublemaker <- c()

现在是循环部分:

for (i in 1:length(stuff)) {
  print(i)
  tryCatch(expr = print(log(stuff[[i]])),
           error = function(e) {
             message('An Error Occurred!!!');
             troublemaker <- c(troublemaker, i)
           }
  )
}

我在不同的示例中看到过这个方法,但对我来说没有起作用。我甚至重新安装了R。所以,troublemaker 最后应该包含 4 和 7,但它和之前一样为空。这不起作用是否是因为 troublemaker <- c(troublemaker, i) 在该函数内部?我还尝试过 error = troublemaker <- c(troublemaker, i),但然后它会计算每一轮。任何帮助都受欢迎!

英文:

I have a loop to test different values in a function. I would like to know which of the values causes a problem for later examination. So i would like to save the round and then the loop to continue.
I create some dummy data and an empty vector to save the loop round:

stuff &lt;- c(12, 9, 2, &quot;cat&quot;, 25, 10, &quot;horse&quot;)
troublemaker &lt;- c()

And now the loop:

for (i in 1:length(stuff)) {
  print(i)
  tryCatch(expr = print(log(stuff[[i]])),
           error = function(e) {
             message(&#39;An Error Occurred!!!&#39;);
             troublemaker &lt;- c(troublemaker, i)
           }
  )
}

I saw this in different examples, but those didn't work for me. I reinstalled even R. So, troublemaker should contain 4 and 7 at the end, but it is as empty as before. Is this not working, because troublemaker &lt;- c(troublemaker, i)is within that function? I also tried error = troublemaker &lt;- c(troublemaker, i), but then it counts every round.
Any help is welcome!

答案1

得分: 2

Generally in R, functions create a local scope, and expressions inside functions affect variables within that scope. The code function(e) troublemaker &lt;- c(troublemaker, i) modifies troublemaker within the function's scope but not in the global environment.

However, global assignment &lt;&lt;- can be used to modify global variables in such cases. (Also, the stuff vector has been changed to a list as mentioned in comments.)

stuff &lt;- list(12, 9, 2, &quot;cat&quot;, 25, 10, &quot;horse&quot;)
troublemaker &lt;- c()

for (i in 1:length(stuff)) {
  print(i)
  tryCatch(expr = print(log(stuff[[i]])),
           error = function(e) {
             message(&#39;An Error Occurred!!!&#39;);
             troublemaker &lt;&lt;- c(troublemaker, i)
           }
  )
}
# [1] 1
# [1] 2.484907
# [1] 2
# [1] 2.197225
# [1] 3
# [1] 0.6931472
# [1] 4
# An Error Occurred!!!
# [1] 5
# [1] 3.218876
# [1] 6
# [1] 2.302585
# [1] 7
# An Error Occurred!!!
troublemaker
# [1] 4 7
英文:

Generally in R, expressions evaluated inside functions are local to those functions. function(e) troublemaker &lt;- c(troublemaker, i) will modify troublemaker in the context of the anonymous function, but it will not modify troublemaker in your global environment.

Though usually (and rightly) frowned upon, this is a case where global assignment &lt;&lt;- may make sense. (Note that I've also changed your stuff vector to a list as mentioned in comments.)

stuff &lt;- list(12, 9, 2, &quot;cat&quot;, 25, 10, &quot;horse&quot;)
troublemaker &lt;- c()

for (i in 1:length(stuff)) {
  print(i)
  tryCatch(expr = print(log(stuff[[i]])),
           error = function(e) {
             message(&#39;An Error Occurred!!!&#39;);
             troublemaker &lt;&lt;- c(troublemaker, i)
           }
  )
}
# [1] 1
# [1] 2.484907
# [1] 2
# [1] 2.197225
# [1] 3
# [1] 0.6931472
# [1] 4
# An Error Occurred!!!
# [1] 5
# [1] 3.218876
# [1] 6
# [1] 2.302585
# [1] 7
# An Error Occurred!!!
troublemaker
# [1] 4 7

huangapple
  • 本文由 发表于 2023年8月11日 01:02:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877886.html
匿名

发表评论

匿名网友

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

确定