How to crash R? - 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.

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 load dyn.load("C:/.../Rtools/bin/cygwin1.dll").

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.

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

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.

I'll do this in plain C because my C++-foo isn't Dirkian:
Create a C file, segv.c:
#include <signal.h>
void crashme(){raise(SIGSEGV);}
Compile it at the command line (windows users will have to work this out for themselves):
R CMD SHLIB segv.c
In R, load and run:
dyn.load("segv.so") # or possibly .dll for Windows users
.C("crashme")
Producing a segfault:
> .C("crashme")
*** caught segfault ***
address 0x1d9e, cause 'unknown'
Traceback:
1: .C("crashme")
Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection: 1
aborting ...
Segmentation fault
This is the same behaviour as the one Thomas references in the graphics system bug report which I have filed and might get fixed one day. However this two-liner will always raise a segfault...
Maybe Dirk can one-line-Rcpp-ise it?

If you want to crash your R, try this
lapply("", function(x) eval(sys.call(1)))
(Save everything before running because this immediately results in "R Session Aborted")
Edit: This works for me on Windows 10.

Related

M1 Silicon gfortran c++ in R install issues

I bought an M1 Silicon with Monterey back in January and --- after a three+ month pause -- am trying again to get Rcpp running so I can install RStan. Following these instructions in response to (I'd like to just comment on this other issue, but being new to posting on Stack Overflow it tells me that I cannot and what I am about to post is certainly NOT an answer):
Configuring compilers on Mac M1 (Big Sur, Monterey) for Rcpp and other tools
While step 3 (installing gfortran) looks to go well, when I ask about gfortran things seem less hopeful:
-bash: gfortran: command not found
Or maybe this is not how to ask if all went well with the installation?
No matter, I charge on to step 4 installing from openmp-13.0.0-darwin21-Release.tar.gz based on:
Apple clang version 13.1.6 (clang-1316.0.21.2.3)
Target: arm64-apple-darwin21.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
I confirm that I have the files where expected, then create the .R directory and the Makevars file (in nano) then try to run the R and get ....
+ #endif
+ }
+ ')
/Users/lizzie/.R/Makevars:6: *** missing separator. Stop.
Error in Rcpp::sourceCpp(code = "\n#include <RcppArmadillo.h>\n#ifdef _OPENMP\n# include <omp.h>\n#endif\n\n// [[Rcpp::depends(RcppArmadillo)]]\n// [[Rcpp::export]]\nvoid omp_test()\n{\n#ifdef _OPENMP\n Rprintf(\"OpenMP threads available: %d\\n\", omp_get_max_threads());\n#else\n Rprintf(\"OpenMP not supported\\n\");\n#endif\n}\n") :
Error 1 occurred building shared library.
WARNING: The tools required to build C++ code for R were not found.
Please install Command Line Tools for XCode (or equivalent).
Any thoughts or ideas much appreciated. Again, I am new to posting to StackOverflow so apologize as I am sure I am doing this wrong.

JEdit plugin error while loading Isabelle

I am working with a Windows 10 device and after some work with Isabelle I get the following error:
The following plugin could not be loaded:
C:\Users\PC\Desktop\Isabelle2018\src\Tools\jEdit\dist\jars\Isabelle-jEdit.jar:
Cannot start:
*** [line 1 of "preferences"] error: bad input
I note this problem appears in this version and in a previous Windows 8 version when not switching off properly the machine.
In the Isabelle list I got an answer to this problem:
The error indicates that the $ISABELLE_HOME_USER/etc/preferences file is in a bad state: the file is written each time Isabelle/jEdit shuts down, and switching off the computer in the middle might have corrupted it.
You can try to repair or delete that file. The location of $ISABELLE_HOME_USER on Windows is usually something like C:\Users\my_name.isabelle\Isabelle2018.
I think this problem may be encountered by other people in the community and this answer may save some time to them.

RStudio : Rook does not work?

I would like to build a simple webserver using Rook, however I am having strange errors when trying it in R-Studio:
The code
library(Rook)
s <- Rhttpd$new()
s$start()
print(s)
returns the rather useless error
"Error in listenPort > 0 :
comparison (6) is possible only for atomic and list types".
When trying the same code in a simple R-Console,everything works - so I would like to understand why that happens and how I can fix it.
RStudio is Version 0.99.484 and R is R 3.2.2
I've experienced same thing.
TLDR: This pull request solves the problem: https://github.com/jeffreyhorner/Rook/pull/31
RStudio is treated in different way and Rook port is same as tools:::httpdPort value. The problem is that in current Rook master tools:::httpdPort is assigned directly. It's a function that's why we need to evaluate it first.
If you want to have it solved right now, without waiting for merge into master: install devtools and load package from my fork #github.
install.packages("devtools")
library(devtools)
install_github("filipstachura/Rook")

R CMD check stops at "checking Rd cross-references"

I am using devtools::check(document=FALSE,args=c('--no-multiarch')) to perform the checks for my package.
When the above command performs R --vanilla CMD check "C:\sometmpfolder/mypackage.tar.gz" --timings --no-multiarch,
it stops at
* checking Rd cross-references ...
i.e., this is the last line in mypackage.Rcheck/00check.log. There is no error message - the check seems to freeze.
I use roxygen2 to create my documentation. The cross references that I use are of the form \link{somefunction} and \link[somepkg]{somefunction}.
I am running R 3.1.2 on a Win 7 machine.
But I think that I have seen a similar behaviour with earlier versions of R and without roxygen2 as well.
Do you guys know why the check stops at the cross-references?
It turned out that an incorrect statement of the form
\link[somepkg]{somefunction}
was the problem. In my case I used the incorrect command \link[reshape]{melt} although it should be \link[reshape2]{melt}.
This caused the Rd cross-reference check to take forever (actually, it finished after nearly one hour). After correcting to \link[reshape2]{melt} the Rd cross-reference check only took a minute or so.

Running command had status 1

I've tried to run command in R 2.15.2
rsaga.geoprocessor(lib="ta_channels", module=0, param=list(ELEVATION="DEMflt.sgrd", CHNLNTWRK=paste("channels", i, ".sgrd", sep=""), CHNLROUTE="channel_route.sgrd", SHAPES="channels.shp", INIT_GRID="DEMflt.sgrd", DIV_CELLS=3, MINLEN=40), show.output.on.console=FALSE)
and I'm constantly getting this warning:
Warning message:
running command '"C:/Users/Nenad/Documents/R/win-library/2.15/RSAGA/SAGA-GIS/saga_cmd.exe" ta_preprocessor 2 -DEM "DEM1.sgrd" -RESULT "DEMflt.sgrd" -MINSLOPE "0.05"' had status 1
I use windows 8 and also tried to ran R as admin.
Any idea what is the problem? Thanks!
Idk how actual it is, but I've been struggling with "had status 1" warning a lot. Especially it got really annoying when I tried to use seasonal package to conduct the X13-ARIMA-SEATS seasonal decomposition of time series. The seasonal::seas command just didn't work, because in the code of this command there is a stop condition when running a certain stuff with cmd.exe returns non-zero status. While, as it was mentioned before, 'status 1' doesn't prevent command execution, in case of seasonal package it does.
The problem in my case was caused by some mistake in Windows Registry (Win 10), that in turn caused warning System cannot find the path specified when launching CMD.exe or PowerShell, which caused warning inside R as well. So to fix it:
Press Win+R -> regedit
In HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor folder clean the value of Autorun record (it should be empty)
Do the same for Autorun in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
After these actions annoying warning with "has status 1" has gone and seasonal::seas started to work perfectly.
Hope it will be usefull for somebody.
Running system commands from R can be really tricky. In my experience, as long as the exit code is not 127 then the command did run, and you could use the intern=TRUE switch in the system command for a more verbose output. If you run the command again, the warning message could contain a errmsg attribute as well for some more info. hth

Resources