How to catch the keyboard interruption in R?

Is there any way to catch the keyboard interruption in R?

I wanna handle, with a tryCatch, for example, the keyboard interruption (ESC or Ctrl-C), in order to clean up what is running before the process is done.

In Python we have the KeyboardInterruption exception, that can be catch in a try-block.
Does R have a similar way to handle user interruptions?

Maybe interrupt?

tryCatch(for (i in 1:1e6) mean(rcauchy(1:1e6)), interrupt = function(e) "foo")
3 Likes

@hughparsonage Thanks a lot!
I've never imagined that would be so simple.