Error Handling in Lyx & Sweave: using Quantmod (and R, of course)

I do reports for clients with LyX and Sweave. It took me an extremely long time to get them working, but now that they’re working I can do more in an hour and thus charge more per hour. (Which is, like, the point.)

If you’re not familiar, here’s a rundown:

  • LaTeX is the standard writing tool for mathematicians. I started using it for business reports some time ago because I can write plain text more quickly than a Word doc.

    Even though I have to add some “code” like section{Executive Summary}, it ends up being way quicker than debating with myself about what style looks right in Word. There are a few other pluses, like: being able to comment out alternative versions of a sentence; adding arbitrary whitespace to drafts; utilising include{ section_4.txt } to organise long documents; writing in simple-font view and proofreading in pretty view. 

  • LyX is like a more user-friendly LaTeX. I used to write business reports in LaTeX, but LyX is a bit quicker and there’s less chance of an accidental { or forcing me to bug-hunt in a report.
  • R is a statistical programming language.
  • Sweave allows you to type R code directly into your LaTeX documents. So after I do a line of useful analysis in R, I paste that line into LyX and now the client’s report has the same table, graphic, or stem-and-leaf plot that I’m looking at in the R window.
  • All of the above are free.

Make sense? Onto the little problem that I solved. Current useRs, start reading here.

The Problem

Using Jeffrey Ryan’s quantmod package to grab public data about securities, you sometimes get errors. Maybe internet’s down when you’re compiling the LyX document, maybe the client’s letter code doesn’t appear on Google or Yahoo Finance, etc. Errors abound wherever computers do.

If you get errors — or even warnings — at the R commandline, then the LyX document will not compile. And then you do not make money all the while you spend fixing this problem. Sad face.

The Solution

The answer is to learn basic error handling in R. The first function to learn about is try. That’s try as in

for ( ... ) {
getSymbols( "DZZ" )
)) == "try-error")

The squigglies around { next } may be omitted for easy reading. The lesson one learns from this line of code is that the try function returns an object of class try-error if what you try( to do ) fails.

But catching errors is not enough. You must catch warnings as well if’n you want yer little varmint to gin’r ate a bona fide doc-u-ment. How do you catch warnings? Well how about just suppressing them. That happened for me when I switched on the silent=TRUE flag.

One other probable suggestion is to use inherits( ). Why? Because try might return multiple values, only one of which is try-error. (Another example of multiple class-types: quantmod’s OHLC data are of class xts and of class zoo.) Then the class would not be equal to try-error, but it would inherit try-error.

<<echo=TRUE, fig=TRUE>>=
for (tick in symbols[1:237]) {
		try( getSymbols(tick), silent=TRUE ),
	) next;	

print( tick );
if( get(tick))) { next; }     #more error catching
else plot( get( tick));

Why plot( get(tick) ) and print(tick)? An example value of tick in symbols[...] might be "DZZ". You cannot plot the character string "DZZ". get("DZZ") points to the referent — the object named "DZZ", which is one of quantmod’s OHLC data structures. Then plot calls plot.xts and shows you (and your client) something.

Hope this helps.


Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: