Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying errors with sweave

I'm writing some R notes with Sweave and would like to show common errors. For example,

<<echo=TRUE, eval=TRUE>>=
x = 5
#Case matters!
x*X
@

However when sweaving, the document won't compile due to the R error. Is there any way to make sweave compile and show the (nicely formated) error?

like image 542
csgillespie Avatar asked Jun 28 '10 09:06

csgillespie


3 Answers

This is a non-issue with knitr, the "next generation Sweave", if I may say so. It displays errors and warnings by default, which was difficult or impossible in Sweave, along with a plethora of other nice features (like syntax coloring, PGF integration and plot animation, for starters). It is developed and maintained actively, too.

Sweave code must be converted once using the function Sweave2knitr provided by the same package.

like image 166
krlmlr Avatar answered Sep 30 '22 16:09

krlmlr


As Shane suggests, use

<<echo=TRUE,eval=FALSE>> 

for the code that will error, but you want to display, and then again with

<<echo=FALSE,eval=TRUE,results=verbatim>> 

but with the same code wrapped in a try.

There's an example here: http://tolstoy.newcastle.edu.au/R/help/05/09/11690.html

like image 20
mdsumner Avatar answered Sep 30 '22 16:09

mdsumner


Wrap your error in a try() command. Then it will keep running:

> {print(1); try(x*X); print(2)}
[1] 1
Error in try(x * X) : object 'X' not found
[1] 2
like image 44
Shane Avatar answered Sep 30 '22 16:09

Shane