Site icon R-bloggers

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

[This article was first published on Isomorphismes, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

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.

To leave a comment for the author, please follow the link and comment on their blog: Isomorphismes.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.