Reputation: 1321
When I execute:
my_env <- new.env(parent = emptyenv())
test <- purrr::safely(get("meta", envir = my_env))
I get the following error:
Error in get("meta") : object 'meta' not found
The error is correct in the sense of that the meta
variable is not defined in the environment but my line of thinking was that safely
would return a NULL
in that case.
I can get around the error by using checking using exists
first but I was curious about why safely
fails. Am I wrong in thinking of safely
as the equivalent of a try-catch
?
Upvotes: 0
Views: 31
Reputation: 263481
You are misinterpreting the actions of the safely
function. It was actually succeeding. If you had examined the value of test
, you should have seen:
> test
[1] "Error in get(\"meta\", env = my_env) : object 'meta' not found\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in get("meta", env = my_env): object 'meta' not found
To suppress error messages from being visible at the console you can either turn off reporting with options(show.error.messages = FALSE)
or you can redirect the destination of stderr()
.
Upvotes: 1