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

Douglas Bates bates at stat.wisc.edu
Thu Dec 2 22:40:28 CET 2010

I currently use constructions like

	if (as<string>(lst.attr("class")) != "family")
	    throw std::runtime_error("glmFamily requires a list of (S3) class

Is there a cleaner way that I haven't discovered yet.

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.

More information about the Rcpp-devel mailing list