英文:
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 <- c(12, 9, 2, "cat", 25, 10, "horse")
troublemaker <- c()
And now the loop:
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)
}
)
}
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 <- c(troublemaker, i)
is within that function? I also tried error = troublemaker <- 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 <- c(troublemaker, i)
modifies troublemaker
within the function's scope but not in the global environment.
However, global assignment <<-
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 <- list(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)
}
)
}
# [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 <- 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 <<-
may make sense. (Note that I've also changed your stuff
vector to a list
as mentioned in comments.)
stuff <- list(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)
}
)
}
# [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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论