How to activate R taskcallback after an error

I would like to know how to call back after an error.

I tried this, but it does not work with an error:

addTaskCallback( function(expr, value, ok, visible) { print("ok") TRUE } ) getTaskCallbackNames() print(1) #ok ls() #ok dont_exist() # the taskcallback isn't activated 

EDIT:

GET IT!

 if (!require(devtools)){install.packages("devtools")} devtools::install_github("ThinkRstat/fcuk") library(fcuk) sl() iri view mea 
+6
r
source share
1 answer

R provides some method of adding handlers for errors and warnings. You can use something like

 .Internal(.addCondHands("error", list(error = function(e) {print("ok")}), .GlobalEnv, NULL, TRUE)) 

add callback function for error. I did not find much documentation for this, but you can see the source for withCallingHandlers and tryCatch to find out how to use it.

Edit:

And also I found that one method has a callback after an error, but not in a pure R-way. It relies on the Rstudio error callback mechanism:

If you use Rstudio, you will find the global "error" option that Rstudio uses as an error callback function. You can see this: getOption("error") and change it as follows:

 f <- function(){ print("ok") } options(error = f) 

And if you want to collect the last error message, you can use geterrmessage() , which is built into R.

+2
source share

All Articles