Hi Christofer,

R's S4 class system builds on generics, that is one class defines the raw frame 
of a function and maybe an own distinct implementation and other classes, that 
(usually) inherit from this class, set their special implementations of it. 
Consider a generic function in R as being an abstract function, just telling 
inheriting classes how to build such a function, i.e. what to put in. 
Furthermore it enables the dispatches like callNextMethod, which calls the 
method from the parent (be aware if you have more than one direct parent!). 
Under the hood, R must know from the input arguments what function should be 
called. If you put in an B class that inherits from an A class it must know, 
what to do with a user-defined "save" function when the input argument is for 
example an object of class "B". If you put in an "A" class object, it should 
know how to proceed this object and so on. So the generic method gives a kind 
of internal map for R how to call this function in which contexts.

The standardGeneric() function: We must provide a name and a definition of the 
generic function. In most cases you just want to tell, what arguments the 
function should expect. With the function standardGeneric("name") R creates a 
generic which can then be dispatched in form of distinct methods. So if you 
call "save" R sees, that it is a generic function and checks which of the 
distinct functions to call, evaluating the arguments. If it finds no specific 
function for the arguments at hand, it calls a default function. If, as in your 
case, the function name is already taken, R sets, when you use setGeneric, the 
usual "save" function as the default method, getting called when nothing else 
fits ... resulting in either an error because of mismatching function arguments 
or in saving in a way you do not want to. Solution is here to define inside the 
setGeneric also the default function via the argument useAsDefault = function() 
{}. 

So generic function (setGeneric): What should be done?
Distinct function (setMethod): How should it be done?


You find a very good introduction to the S4 class system here: 
http://cran.r-project.org/doc/contrib/Genolini-S4tutorialV0-5en.pdf

Deep down information can be found for example in the books: "Programming with 
Data" and "Software for Data Analysis and Computing with R", both from John 
Chambers. 


Best

Simon



On Jul 18, 2013, at 8:23 PM, Christofer Bogaso <bogaso.christo...@gmail.com> 
wrote:

> Hi Simon,
> 
> Thanks for your pointer.
> 
> However could you please explain what 'function(x, file_Path) 
> standardGeneric("save")' means here? The underlying help files look quite 
> rocket science for me!
> 
> Thanks for your time.
> 
> Thanks and regards,
> 
> 
> On Thu, Jul 18, 2013 at 4:32 PM, Simon Zehnder <szehn...@uni-bonn.de> wrote:
> Hi Christopher,
> 
> I think, that "save" is no generic function like "plot", "show", etc. So at 
> first you have to determine a generic.
> 
> setGeneric("save", function(x, file_Path) standardGeneric("save"))
> 
> Now your definition via setMethod.
> 
> 
> Best
> 
> Simon
> 
> 
> 
> On Jul 18, 2013, at 12:09 PM, Christofer Bogaso <bogaso.christo...@gmail.com> 
> wrote:
> 
> > Hello again,
> >
> > I am trying to define the 'save' method for my S4 class as below:
> >
> > setClass("MyClass", representation(
> >               Slot1 = "data.frame"
> >       ))
> >
> > setMethod("save", "MyClass", definition = function(x, file_Path) {
> >
> >               write.table(x@Slot1, file = file_Path, append = FALSE, quote 
> > = TRUE,
> > sep = ",",
> >                                               eol = "\n", na = "NA", dec = 
> > ".", row.names = FALSE,
> >                                               col.names = TRUE, qmethod = 
> > c("escape", "double"),
> >                                               fileEncoding = "")
> >       })
> >
> > However while doing this I am getting following error:
> >
> > Error in conformMethod(signature, mnames, fnames, f, fdef, definition) :
> >  in method for ‘save’ with signature ‘list="MyClass"’: formal
> > arguments (list = "MyClass", file = "MyClass", ascii = "MyClass",
> > version = "MyClass", envir = "MyClass", compress = "MyClass",
> > compression_level = "MyClass", eval.promises = "MyClass", precheck =
> > "MyClass") omitted in the method definition cannot be in the signature
> >
> >
> > Can somebody point me what will be the correct approach to define
> > 'save' method for S4 class?
> >
> > Thanks and regards,
> >
> > ______________________________________________
> > R-help@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> 
> 


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to