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

November 8, 2011
By

(This article was first published on Isomorphismes, and kindly contributed to R-bloggers)

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.

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 ( ... ) {        if(class(try(      getSymbols(  "DZZ"  )        )) == "try-error") next    ................................      }

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.

=
for (tick in symbols[1:237]) {
#
#ERROR HANDLING
#
if(
inherits(
try( getSymbols(tick), silent=TRUE ),
"try-error"
)
) next;

print( tick ); 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.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...