[R] Private methods

Thomas Lumley tlumley at u.washington.edu
Sat Aug 14 04:23:48 CEST 2004

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

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:::.class1
function (x)
<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.


More information about the R-help mailing list