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

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

Extract in which sequence a loop created an error

问题

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

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

现在是循环部分:

  1. for (i in 1:length(stuff)) {
  2. print(i)
  3. tryCatch(expr = print(log(stuff[[i]])),
  4. error = function(e) {
  5. message('An Error Occurred!!!');
  6. troublemaker <- c(troublemaker, i)
  7. }
  8. )
  9. }

我在不同的示例中看到过这个方法,但对我来说没有起作用。我甚至重新安装了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:

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

And now the loop:

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

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.)

  1. stuff &lt;- list(12, 9, 2, &quot;cat&quot;, 25, 10, &quot;horse&quot;)
  2. troublemaker &lt;- c()
  3. for (i in 1:length(stuff)) {
  4. print(i)
  5. tryCatch(expr = print(log(stuff[[i]])),
  6. error = function(e) {
  7. message(&#39;An Error Occurred!!!&#39;);
  8. troublemaker &lt;&lt;- c(troublemaker, i)
  9. }
  10. )
  11. }
  12. # [1] 1
  13. # [1] 2.484907
  14. # [1] 2
  15. # [1] 2.197225
  16. # [1] 3
  17. # [1] 0.6931472
  18. # [1] 4
  19. # An Error Occurred!!!
  20. # [1] 5
  21. # [1] 3.218876
  22. # [1] 6
  23. # [1] 2.302585
  24. # [1] 7
  25. # An Error Occurred!!!
  26. troublemaker
  27. # [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.)

  1. stuff &lt;- list(12, 9, 2, &quot;cat&quot;, 25, 10, &quot;horse&quot;)
  2. troublemaker &lt;- c()
  3. for (i in 1:length(stuff)) {
  4. print(i)
  5. tryCatch(expr = print(log(stuff[[i]])),
  6. error = function(e) {
  7. message(&#39;An Error Occurred!!!&#39;);
  8. troublemaker &lt;&lt;- c(troublemaker, i)
  9. }
  10. )
  11. }
  12. # [1] 1
  13. # [1] 2.484907
  14. # [1] 2
  15. # [1] 2.197225
  16. # [1] 3
  17. # [1] 0.6931472
  18. # [1] 4
  19. # An Error Occurred!!!
  20. # [1] 5
  21. # [1] 3.218876
  22. # [1] 6
  23. # [1] 2.302585
  24. # [1] 7
  25. # An Error Occurred!!!
  26. troublemaker
  27. # [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:

确定