Had a quick question about tryCatch and my mental model of how a tryCatch condition would work.
Natively coming into R I would try to write a tryCatch like this:
test_func <- function(x) {
value <- tryCatch(
log(x),
error = function(e){
cat("That's not a number! Returning NA_int")
NA_integer_
}
)
return(value)
}
In your first attempt, the R interpreter was reading log(x) as an argument to the tryCatch function called new_value rather than reading it as an expr to evaluate, which is why your error function wasn't being triggered
you could theoretically wrap the expression in curly braces so that it's treated as an expression - ie
which will correctly trigger the error function - but the error function suffers from an environment issue where the new_value is not being assigned in the correct function environment
Saving a QA from Slack for later bookdowning Q