Hi, I'm trying to understand R's object oriented abilities. I have read (in a very recent email to r-help) that R's methods package does not support private methods. However, I also looked at the source for the function "is": > is function (object, class2) { cl <- .class1(object) if (missing(class2)) return(extends(cl)) if (.identC(cl, class2) || .identC(class2, "ANY")) return(TRUE) ... The first line of the function, the assignment to cl, seems to call a function called ".class1". I was unable to see the source for ".class1": > .class1 Error: Object ".class1" not found It seems to me that either (a) I don't understand what I'm doing, or (b) ".class1" is a very private method. The help system finds nothing about ".class1". Could someone please help me to understand this. Most helpful of all would be pointers on where I could find more information. (So far I have found only two documents on R's object oriented abilities: "A guide to using S4 Objects" and "A draft of the R language definition".) Thanks, Matthew
.class1 is an 'internal' method in the namespace of the package 'methods'. It can be accessed with the ::: operator. A good start for documentation may be Vol 3/1 of R-News.>methods:::.class1function (x) class(x)[[1]] <environment: namespace:methods> --Matt -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Matthew Walker Sent: Friday, August 13, 2004 17:19 PM To: r-help at stat.math.ethz.ch Subject: [R] Private methods Hi, I'm trying to understand R's object oriented abilities. I have read (in a very recent email to r-help) that R's methods package does not support private methods. However, I also looked at the source for the function "is": > is function (object, class2) { cl <- .class1(object) if (missing(class2)) return(extends(cl)) if (.identC(cl, class2) || .identC(class2, "ANY")) return(TRUE) ... The first line of the function, the assignment to cl, seems to call a function called ".class1". I was unable to see the source for ".class1": > .class1 Error: Object ".class1" not found It seems to me that either (a) I don't understand what I'm doing, or (b) ".class1" is a very private method. The help system finds nothing about ".class1". Could someone please help me to understand this. Most helpful of all would be pointers on where I could find more information. (So far I have found only two documents on R's object oriented abilities: "A guide to using S4 Objects" and "A draft of the R language definition".) Thanks, Matthew ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Sat, 14 Aug 2004, Matthew Walker wrote:> I have read (in a very recent email to r-help) that R's methods package > does not support private methods. However, I also looked at the source > for the function "is": > > > is > function (object, class2) > { > cl <- .class1(object) > if (missing(class2)) > return(extends(cl)) > if (.identC(cl, class2) || .identC(class2, "ANY")) > return(TRUE) > ... > > The first line of the function, the assignment to cl, seems to call a > function called ".class1". I was unable to see the source for ".class1": > > > .class1 > Error: Object ".class1" not found > > It seems to me that either (a) I don't understand what I'm doing, or (b) > ".class1" is a very private method. The help system finds nothing about > ".class1". >R does have a way to hide functions so that they won't be accidentally accessed, but these functions are private to a package, not to a class. In a package that has a namespace, functions are only visible outside the package if they are explicitly exported. This can be overriden with the ::: operator> methods:::.class1function (x) class(x)[[1]] <environment: namespace:methods> but should you should resist the temptation to do this. You may think this sounds like a way to define private methods. The problem is that they are too private. Subclasses defined outside the package don't inherit any access to these functions, so they can't really be regarded as class methods. -thomas