Dear Martin, As it turns out, the test that I proposed (i.e., testing for NULL slotNames) sometimes fails. For example:
> library(car) > data(Prestige) > sum <- summary(lm(prestige ~ income + education, data=Prestige)) > slotNames(sum) character(0) The following, however, seems to work (at least as far as I've been able to ascertain): isS4object <- function(object) length(slotNames(object)) != 0 I hope that this is a more robust test. John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox -------------------------------- > -----Original Message----- > From: Martin Maechler [mailto:[EMAIL PROTECTED] > Sent: Friday, November 26, 2004 3:18 AM > To: John Fox > Cc: [EMAIL PROTECTED] > Subject: Re: [R] Testing for S4 objects > > >>>>> "JohnF" == John Fox <[EMAIL PROTECTED]> > >>>>> on Thu, 25 Nov 2004 22:28:50 -0500 writes: > > JohnF> Dear r-help list members, Is there a way to test > JohnF> whether an object is an S4 object? The best that I've > JohnF> been able to come up with is > > JohnF> isS4object <- function(object) > !(is.null(slotNames(object))) > > you can drop one pair of "(..)" to give > > isS4object <- function(object) !is.null(slotNames(object)) > > > JohnF> which assumes that an S4 object has at least one > JohnF> slot. I think this is safe, but perhaps I'm missing > JohnF> something. > > The question is a very good one -- that I have posed to > R-core a while ago myself. > > Inside utils:::str.default {which doesn't show the many > commments in the *source* of str.default()}, I have wanted a > way that even works when the 'methods' package is not > attached and use the more obscure > > #NOT yet:if(has.class <- !is.null(cl <- class(object))) > if(has.class <- !is.null(cl <- attr(object, "class")))# > S3 or S4 class > S4 <- !is.null(attr(cl, "package"))## <<<'kludge' FIXME! > ##or length(methods::getSlots(cl)) > 0 > > For the time being, I'd keep your function, but I don't think > we'd guarantee that it will remain the appropriate test in > all future. But till then many things will have happened (if > not all of them ;-). > > Martin Maechler, ETH Zurich > ______________________________________________ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
