variable scope in R tryCatch block: is <<- necessary to change local variable defined before tryCatch?
问题 Consider the following code: test1 <- "a" test2 <- "a" tryCatch(stop(), error= function(err){ print(test1) print(test2) test1 <- "b" test2 <<- "b" }) Result: print(test1) [1] "a" print(test2) [1] "b" The value of variable test1 is visible within the tryCatch block, but changing it with "<-" operator does not affect its value outside the tryCatch block. If a new value is assigned with <<- it has the desired effect. Why? Is using the <<- operator within the tryCatch block a recommended way to