The Fun of Error Trapping: R Package Edition

For the last month or so I’ve been working on an R package to make accessing the Adobe (Omniture) Digital Marketing Suite Reporting API easier.  As part of this development effort, I’m at the point where I’m intentionally introducing errors into my function inputs, trying to guess some of the ways useRs might incorrectly input arguments into each function.  Imagine my surprise when I saw this:

> result <- content(json)
Loading required package: XML
Error in parser(content, …) : could not find function “htmlTreeParse”
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
there is no package called ‘XML’

The main idea behind the functions I’ve written is making REST calls to the Omniture API, which done correctly return valid JSON. From there, each JSON string is converted from binary or whatever formatting they come back as using the content function from the httr package. Without specifying any arguments to the content function, the function tries to guess at the proper translation method.

The guessing is all fine and good until you don’t pass a valid JSON string!  In this case, the error message is guessing that it might be XML (the returned error is actually HTML), tries to load the XML package…then says it can’t load the XML package. A two-for-one error!

Maybe it’s just me, but I’m finding this hilarious after a long day of programming. Maybe it’s because I’m not longer intimidated by an error like this, and as such, I’ve gotten over the steep learning curve of R.

Note:  Hadley, if you read this, I’m not saying your httr package has any sort of bug or anything. Just that I found this particular error amusing.


  1. randyzwitch says:

    @skardhamar Ha! I’m pretty much done, having some company folks do some internal testing starting today.

  2. hadleywickham says:

    If you haven’t realised it already, the best way to avoid this error is to tell content() what type to use.  But it may be picking XML because that’s what the server says it is.

    • @hadleywickham Definitely. I now am specifying the content type, as well as checking the status of the response to make sure it is equal to 200. So no one else should see this error.
      Although it’s been a ton of work, trying to create an R package worthy of CRAN has been a great learning experience on the difference between ad hoc analytical programming and actual software engineering (if you can call it that).

%d bloggers like this: