2

I have a variable declaration inside of an error function of a tryCatch function. But when the error is triggered, the variable declaration is not performed, even though the error message is successfully output.

tryCatch({ var_binned_names = arules::discretize(Xt,
                                              method = arules_method,
                                              breaks = num_breaks,
                                              ordered = TRUE)
        }, error = function(error) {
          cat("Error: could not discretize numeric", numeric_i, "", name, "\n")
          cat("Unique values:", length(unique(Xt)), "\n")
          var_binned_names = Xt
        })

One would expect that, when var_binned_names is failed to be assigned due to an error with discretize, that it would get assigned Xt in the error function. However, what happens is that it's not defined.

Hayden Y.
  • 448
  • 2
  • 8
tyur43
  • 21
  • 1
  • 1
    Because the variable is local? https://stackoverflow.com/questions/10904124/global-and-local-variables-in-r – user202729 Aug 19 '19 at 06:27
  • @user202729 thanks for your comment. is it the case that if the discretize function had run successfully and var_binned_names had been declared in the tryCatch function, this would then be a global assignment? – tyur43 Aug 19 '19 at 06:31
  • 1
    maybe move `var_binned_names = ` outside of `tryCatch`? – chinsoon12 Aug 19 '19 at 06:37

3 Answers3

1

Use this approach:

Xt <- 1

res <- tryCatch({ stop("some error")
}, error = function(error) {
  cat("some error message")
  Xt
})

res
#[1] 1

tryCatch has a return value. It is the return value of the expression or (if a condition such as an error is triggered) the return value of the handler.

Roland
  • 127,288
  • 10
  • 191
  • 288
0

The function has its own environment, so an assignment inside the function will not be made to the global environment. You can return the value and assign it outside the function or if you want to make an assignment to the parent environment inside of a function, then use <<- instead of <- or =.

shs
  • 3,683
  • 1
  • 6
  • 34
0

I have been facing this issue too. Here is my explanation of how I decoded this situation.

(a) I have faced this issue where assignment statements (written inside the error block of the tryCatch) do not show up in the environment. but contrastingly,

(b) this does work and gives me z = 7 in the environment panel when I execute this simple code below

x = 5 tryCatch( expr = { x/y }, error = { z = 7 print("Error")} ) but here, this error is generated: attempt to apply non-function

(c) going back, the z becomes unavailable again from the environment if I edit the above code to (which is basically situation 'a') : .... error = function(e) { z = 7 print("Error") }. Note that it does "print"

seems (from a & c) if you used error = function(e) { } then assignments are "hidden" in that they are not available in the environment - this makes sense because, in R, any assignments inside a function are hidden - ok. But if you either use <<- , as suggested above, or remove the "= function(e)" then it does become available - although in the latter, a new error is generated! this was quite a ride for me but I think its worth sharing stories to enlightenment as it might help others!

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 12 '23 at 07:24