[R] 'save' method for S4 class
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.
Re: [R] 'save' method for S4 class
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. __ 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.
Re: [R] 'save' method for S4 class
On 07/18/2013 03:47 AM, Simon Zehnder 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)) The implementation offered by Christofer shows write.table, and the end result is a text file rather than a binary file expected from base::save. This makes it seem inappropriate to use 'save' in this context. Instead, it seems that what Cristofer wants to implement is functionality to support write.table. ?write.table says 'write.table' prints its required argument 'x' (after converting it to a data frame if it is not one nor a matrix) So implementing an S3 method as.data.frame.MyClass - function(x, row.names=NULL, optional=FALSE, ...) { x@x } is all that is needed, gaining lots of flexibility by re-using the code of write.table. myClass = new(MyClass, x=data.frame(x=1:3, y=3:1)) write.table(myClass, stdout()) In the case of a 'save' method producing binary output (but this is what save does already...), I think it's better practice to promote the non-generic 'save' to an S4 generic using it's existing arguments; in this case it makes sense to restrict dispatch to '...', so setGeneric(save, signature=...) The resulting generic is getGeneric(save) standardGeneric for save defined from package .GlobalEnv function (..., list = character(), file = stop('file' must be specified), ascii = FALSE, version = NULL, envir = parent.frame(), compress = !ascii, compression_level, eval.promises = TRUE, precheck = TRUE) standardGeneric(save) environment: 0x4a7b860 Methods may be defined for arguments: ... Use showMethods(save) for currently available ones. This means that a method might be defined as setMethod(save, MyClass, function(..., list = character(), file = stop('file' must be specified), ascii = FALSE, version = NULL, envir = parent.frame(), compress = !ascii, compression_level, eval.promises = TRUE, precheck = TRUE) { ## check non-sensical or unsupported user input for 'MyClass' if (!is.null(version)) stop(non-NULL 'version' not supported for 'MyClass') ## ... ## implement save on MyClass }) It might be that Christofer wants to implement a 'write.table-like' (text output) or a 'save-like' (binary output) function that really does not conform to the behavior of write.table (e.g., producing output that could not be input by read.table) or save. Then I think the better approach is to implement writeMyClass (for text output) or saveMyClass (for binary output). Martin 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. __ 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. -- Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793 __ 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.
Re: [R] 'save' method for S4 class
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.
Re: [R] 'save' method for S4 class
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.
Re: [R] 'save' method for S4 class
On 18.07.2013 20:23, Christofer Bogaso wrote: Hi Simon, Thanks for your pointer. However could you please explain what 'function(x, file_Path) standardGeneric(save)' means here? It is the definition of the generic function for save()? The underlying help files look quite rocket science for me! So start reading about S4 seems to be essential, if you ask for S4 methods. Otherwise we cannot help, given you do not even understand the code to define the generic function. Uwe Ligges 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. __ 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.