I am completely new to R and am confused by the correct use of tryCatch . My goal is to make a forecast for a large data set. If predictions cannot fit into memory, I want to work around the problem by dividing my data.
My code now looks something like this:
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) } })
The fact is that if an error does not occur, the large_vector populated with my predictions, as expected. If an error occurs, large_vector seems to exist only in the error code namespace, which makes sense because I declared it as a function. For the same reason, I get a warning that large_data_frame cannot be deleted.
Unfortunately, this is not what I want. I would like to assign a large_vector variable from my error function. I decided that one of the possibilities was to specify the environment and use the assignment. Thus, I would use the following instructions in my error code:
rm(large_data_frame, envir = parent.env(environment())) [...] assign('large_vector', large_vector, parent.env(environment()))
However, this decision seems rather dirty to me. I wonder if there is an opportunity to achieve my goal with the help of "clean" code?
[EDIT] There seems to be some confusion because I put the code above to illustrate the problem, rather than give a working example. Here is a minimal example that shows a problem with namespace:
# 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