我对 R 很陌生,我对tryCatch
. 我的目标是对大型数据集进行预测。如果预测无法放入内存,我想通过拆分数据来规避问题。
现在,我的代码大致如下:
tryCatch({
large_vector = predict(model, large_data_frame)
}, error = function(e) { # I ran out of memory
for (i in seq(from = 1, to = dim(large_data_frame)[1], by = 1000)) {
small_vector = predict(model, large_data_frame[i:(i+step-1), ])
save(small_vector, tmpfile)
}
rm(large_data_frame) # free memory
large_vector = NULL
for (i in seq(from = 1, to = dim(large_data_frame)[1], by = 1000)) {
load(tmpfile)
unlink(tmpfile)
large_vector = c(large_vector, small_vector)
}
})
关键是,如果没有发生错误,large_vector
则按预期填充我的预测。如果发生错误,large_vector
似乎只存在于错误代码的命名空间中——这是有道理的,因为我将它声明为一个函数。出于同样的原因,我收到一条警告说large_data_frame
无法删除。
不幸的是,这种行为不是我想要的。我想large_vector
从我的错误函数中分配变量。我认为一种可能性是指定环境并使用分配。因此,我将在我的错误代码中使用以下语句:
rm(large_data_frame, envir = parent.env(environment()))
[...]
assign('large_vector', large_vector, parent.env(environment()))
但是,这个解决方案对我来说似乎很脏。我想知道是否有可能用“干净”的代码来实现我的目标?
[编辑] 似乎有些混乱,因为我把代码放在上面主要是为了说明问题,而不是给出一个工作示例。这是一个显示命名空间问题的最小示例:
# Example 1 : large_vector fits into memory
rm(large_vector)
tryCatch({
large_vector = rep(5, 1000)
}, error = function(e) {
# do stuff to build the vector
large_vector = rep(3, 1000)
})
print(large_vector) # all 5
# Example 2 : pretend large_vector does not fit into memory; solution using parent environment
rm(large_vector)
tryCatch({
stop(); # simulate error
}, error = function(e) {
# do stuff to build the vector
large_vector = rep(3, 1000)
assign('large_vector', large_vector, parent.env(environment()))
})
print(large_vector) # all 3
# Example 3 : pretend large_vector does not fit into memory; namespace issue
rm(large_vector)
tryCatch({
stop(); # simulate error
}, error = function(e) {
# do stuff to build the vector
large_vector = rep(3, 1000)
})
print(large_vector) # does not exist