Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to active R taskcallback after error

Tags:

r

i would like to know how to have a callback after an error.

I tried this, but it doesn't work with error :

addTaskCallback(
  function(expr, value, ok, visible) {
    print("ok")
    TRUE
  }
)
getTaskCallbackNames()

print(1) #ok 
ls() #ok
dont_exist() # the taskcallback isn't activated

EDIT:

GOT IT!

if (!require(devtools)){install.packages("devtools")}
devtools::install_github("ThinkRstat/fcuk")
library(fcuk)
sl()
iri
view
mea
like image 308
Vincent Guyader Avatar asked Jun 13 '17 07:06

Vincent Guyader


People also ask

What happens when a task throws an exception?

If Bar throws an exception, it will be thrown right at the point where you call it.

How to handle Task exception?

Exceptions are propagated when you use one of the static or instance Task. Wait methods, and you handle them by enclosing the call in a try / catch statement. If a task is the parent of attached child tasks, or if you are waiting on multiple tasks, multiple exceptions could be thrown.

How to throw exception in Task c#?

try { t. Start(); await t; } catch (Exception e) { // When awating on the task, the exception itself is thrown. // in this case a regular Exception. } } In TPL, When throwing an exception inside a Task, it's wrapped with an AggregateException.

How do I run a task in C#?

To start a task in C#, follow any of the below given ways. Use a delegate to start a task. Task t = new Task(delegate { PrintMessage(); }); t. Start();


1 Answers

R provides some method to add handlers for errors and warnings. You can use something like

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

to add a callback function for error. I didn't find much documentation for this, but you can see the source for withCallingHandlers and tryCatch to see how to use it.

Edit:

And I also find one method to have a callback after an error, but not in a pure R way. It relies on Rstudio's error callback mechanism:

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

f <- function(){
    print("ok")
}

options(error = f)

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

like image 94
Consistency Avatar answered Oct 01 '22 06:10

Consistency