The knitr would always evaluate the R code before formatting the output, so just wondering how can I know whether the R code evaluation has error. Thanks
Asked
Active
Viewed 39 times
1 Answers
1
Basically it boils down to three lines of code in the evaluate package. The key is withCallingHandlers()
, which can be used to capture errors, messages, and warnings, etc. A minimal example:
withCallingHandlers(1 + 'a', error = function(e) {
cat('An error occurred! The error object is:\n')
str(e)
})
If you don't want the error to halt R, you can wrap the code in try()
.

Yihui Xie
- 28,913
- 23
- 193
- 419