[Rcpp-devel] idiom for determining the (S3) class of an R object?

Dirk Eddelbuettel edd at debian.org
Thu Dec 2 22:51:43 CET 2010


On 2 December 2010 at 15:40, Douglas Bates wrote:
| I currently use constructions like
| 
| 	if (as<string>(lst.attr("class")) != "family")
| 	    throw std::runtime_error("glmFamily requires a list of (S3) class
| \"family\"");
| 
| Is there a cleaner way that I haven't discovered yet.

Seems correct as the reverse of how we set classes via attr.
 
| This method is error-prone because the S3 class of an object is a
| CharacterVector whose length can be greater than 1, so the check
| should compare against the individual strings in that vector until it
| finds a match or runs out of elements.
| 
| It is not a great challenge to write a short function that will return
| a boolean given an object and a target class as a std::string, but I
| would prefer not to reinvent the wheel unless I need to.

That looks like a useful helper function to have :)

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list