[R] Namespace support?

Henrik Bengtsson hb at maths.lth.se
Wed Jan 8 04:36:03 CET 2003


I don't know why you are writing your own as.data.frame(), but if you
are using it only on certain types of objects, then I would recommend
you to define a *class specific* function. Using the S3/UseMethod
approach (different from S4/methods) this could look like:

  x <- list(a=..., b=..., c=...)  # Your data structure
  class(x) <- c("MyClass")        # Specifies that x is of class
"MyClass"
  
Then you can define an as.data.frame() that is only used on this class
and will leave all other data types unaffected:

  as.data.frame.MyClass <- function(x, row.names=NULL, optional=FALSE) {
    ...  # Do what you want here
  }

Note the naming rule: <function>.<Class> <- function(<object>, ...)

When calling as.data.frame(x), the generic function as.data.frame() (the
one you've currently overwritten) will call the correct as.data.frame()
depending on the class of x, i.e. in this case as.data.frame.MyClass(x).
This method dispatching is done by the UseMethod() call.

Cheers

Henrik Bengtsson


> -----Original Message-----
> From: r-help-admin at stat.math.ethz.ch 
> [mailto:r-help-admin at stat.math.ethz.ch] On Behalf Of Nunoo, Paa K
> Sent: den 8 januari 2003 09:23
> To: 'r-help at stat.math.ethz.ch'
> Subject: [R] Namespace support?
> 
> 
> Hi,
> I have created an R package in which I implement my own version of the

> "as.dataframe" function. However when I load up my package into R, it 
> seems to mask the original version of "as.data.frame". Is there to 
> limit/control the scope of the "as.data.frame" function defined in the

> my package so that it does not interfere with the original function? 
> Maybe by using namespaces in R?
> 
> thanks
> PK
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list 
> http://www.stat.math.ethz.ch/mailman/listinfo/> r-help
> 
>




More information about the R-help mailing list