How to crash R?
Is there a simple way to trigger a crash in R? This is for testing purposes only, to see how a certain program that uses R in the background reacts to a crash and help determine if some rare problems are due to crashes or not.
Solution 1:
The easiest way is to call C
-code. C
provides a standard function abort()
[1] that does what you want. You need to call: .Call("abort")
.
As @Phillip pointed out you may need to load libc
via:
on Linux,
dyn.load("/lib/x86_64-linux-gnu/libc.so.6")
before issuing.Call("abort")
. The path may of course vary depending on your system.on OS X,
dyn.load("/usr/lib/libc.dylib")
on Windows (I just tested it on XP as I could not get hold of a newer version.) you will need to install
Rtools
[2]. After that you should loaddyn.load("C:/.../Rtools/bin/cygwin1.dll")
.
Solution 2:
There is an entire package on GitHub dedicated to this:
crash
R package that purposely crash an R session. WARNING: intended for test.
How to install a package from github is covered in other questions.
Solution 3:
I'm going to steal an idea from @Spacedman, but I'm giving him full conceptual credit by copying from his Twitter feed:
Segfault #rstats in one easy step:
options(device=function(){});plot(1)
reported Danger, will crash your R session. — Barry Rowlingson (@geospacedman) July 16, 2014
Solution 4:
As mentioned in a comment to your question, the minimal approach is a simple call to the system function abort()
. One way to do this in one line is to
R> Rcpp::cppFunction('int crashMe(int ignored) { ::abort(); }');
R> crashMe(123)
Aborted (core dumped)
$
or you can use the inline package:
R> library(inline)
R> crashMe <- cfunction(body="::abort();")
R> crashMe()
Aborted (core dumped)
$
You can of course also do this outside of Rcpp or inline, but then you need to deal with the system-dependent ways of compiling, linking and loading.