Hello, I have questions while reviewing "chron" package(e.g.,chron.R). 1. What is the differences between 3 kinds of function definition ? 1) "name" <- function(... 2) 'name' <- function(... 3) name <- function(... Do you know Why author used various kinds of definitions ? Is there no functional differences between them ? 2. I don't understand the meaning of the below code: as.chron <- function(x, ...) UseMethod("as.chron") as.chron.default <- function (x, format, ...) { .... Could you let me know what is role of UseMethod("as.chron") ? 3. In the following code, "chron" <- function(dates. = NULL, times. = NULL, format = c(dates = "m/d/y", times = "h:m:s"), out.format, origin.) 'dates', 'origin' and 'times' has dot(.) as at the end of word. Do you know the meaning of dots ? 4. Could you introduce me some books on S/R language, especially for understanding of generic functions ? Some interesting packages such as quantmod, xts, zoo, etc., are seems to be coded using generic functions(UseMethod(..)). But I can't easily understand the code. Thanks in advance, [[alternative HTML version deleted]]
Hello, Inline. Em 27-11-2012 18:06, ??? escreveu:> Hello, > > > I have questions while reviewing "chron" package(e.g.,chron.R). > > > 1. What is the differences between 3 kinds of function definition ? > 1) "name" <- function(... > 2) 'name' <- function(... > 3) name <- function(... > Do you know Why author used various kinds of definitions ? > Is there no functional differences between them ?Not that I know of.> > > 2. I don't understand the meaning of the below code: > > as.chron <- function(x, ...) UseMethod("as.chron") > as.chron.default <- function (x, format, ...) > { .... > > > Could you let me know what is role of UseMethod("as.chron") ?It creates a generic function named as.chron. The next instruction creates the default method for that function.> > > 3. In the following code, > > > > "chron" <- > function(dates. = NULL, times. = NULL, > format = c(dates = "m/d/y", times = "h:m:s"), > out.format, origin.) > > > 'dates', 'origin' and 'times' has dot(.) as at the end of word. > Do you know the meaning of dots ?I am not sure but I believe the programmer is trying to have arguments with names not conflicting with classes "dates" and "times", or other objects or arguments.> > > 4. Could you introduce me some books on S/R language, especially for understanding of generic functions ? > Some interesting packages such as quantmod, xts, zoo, etc., are seems to be coded using generic functions(UseMethod(..)). > But I can't easily understand the code.Try this. f <- function(x) UseMethod("f") f.default <- function(x) print("'x' is not of class 'matrix'") f.matrix <- function(x) print("'x' is of class 'matrix'") f(1) f("a") f(matrix(1, ncol=1)) f(list(1, 2, 3)) The S3 object oriented programming system is documented in many places, it dispatches based on the class of the first argument so in the third case it's f.matrix that is called and in the others, f.default. But you need only to call f(args) (no suffix). Common examples are print(), summary() and plot(). Any of these functions is generic and methods for each class can and are written to them. You can see which methods there are by running the command methods(print) # 174 in R 2.15.2, new session The syntax for these methods is "function.class". Then you can call "function" that the S3 system will find the appropriate method, if any, or will default to "function.default". (Like in as.chron.default) There are free books on CRAN, read them. And google "S3 R Programming". Hope this helps, Rui Barradas> > > Thanks in advance, > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
1. I am not aware of a difference, and don't know why the various forms were used. 2. That handles identifying the correct function to call based on the types of arguments supplied when the function was called. Read about the S4 object-oriented programming features to learn about method dispatch. 3. It has no syntactic effect. Using periods in parameter names may make it less likely to be the same as names defined by the user, but only if the user cooperates by avoiding that naming convention. 4. There is some quite good documentation supplied with R. You can also look on CRAN (http://www.r-project.org/doc/bib/R-publications.html). Please read the posting guide, and post in plain text. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. "???" <birdfire94 at naver.com> wrote:>Hello, > > >I have questions while reviewing "chron" package(e.g.,chron.R). > > >1. What is the differences between 3 kinds of function definition ? > 1) "name" <- function(... > 2) 'name' <- function(... > 3) name <- function(... >Do you know Why author used various kinds of definitions ? >Is there no functional differences between them ? > > >2. I don't understand the meaning of the below code: > >as.chron <- function(x, ...) UseMethod("as.chron") >as.chron.default <- function (x, format, ...) >{ .... > > >Could you let me know what is role of UseMethod("as.chron") ? > > >3. In the following code, > > > >"chron" <- >function(dates. = NULL, times. = NULL, > format = c(dates = "m/d/y", times = "h:m:s"), > out.format, origin.) > > >'dates', 'origin' and 'times' has dot(.) as at the end of word. >Do you know the meaning of dots ? > > >4. Could you introduce me some books on S/R language, especially for >understanding of generic functions ? >Some interesting packages such as quantmod, xts, zoo, etc., are seems >to be coded using generic functions(UseMethod(..)). >But I can't easily understand the code. > > >Thanks in advance, > > > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at 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.