Greetings, I am trying to figure out how to find the source code for R functions. I am specifically interested in finding the code for the "prcomp" function. I know that typing the function name without parenthesis will lead to the code (or to a .Internal or .FORTRAN or .C call). However, I don't really understand what is going on. For example, typing "mean" gives a "UseMethod" response, while typing "mean.default" give the actual code:> meanfunction (x, ...) UseMethod("mean") <environment: namespace:base>> mean.defaultfunction (x, trim = 0, na.rm = FALSE, ...) ---SNIP--- } <environment: namespace:base> Why is this? What does "mean.default" mean? I tried the same thing with "prcomp". With the stats package loaded, I cannot get to the source code for "prcomp".> require(stats)[1] TRUE> prcompfunction (x, ...) UseMethod("prcomp") <environment: namespace:stats>> prcomp.defaultError: object "prcomp.default" not found How do I find the prcomp code? Are there general rules for finding the source code for functions that I should know? Thanks in Advance, Edward J. Wolfrum, Ph.D. National Renewable Energy Laboratory Golden, Colorado
See chapter 5 on object oriented programming in the R language definition manual. Also the ?UseMethod man page. These will explain R's (S3) method dispatch procedures. Finally, search on CRAN for the R New newsletter article on namespaces, which is where you'll find info on why you can't "see" certain functions. See ?getAnywhere to learn how to see code for functions in namespaces. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Wolfrum, Ed > Sent: Tuesday, October 18, 2005 2:27 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Finding code for R functions > > Greetings, > > I am trying to figure out how to find the source code for R > functions. I > am specifically interested in finding the code for the "prcomp" > function. I know that typing the function name without > parenthesis will > lead to the code (or to a .Internal or .FORTRAN or .C call). > However, I > don't really understand what is going on. For example, typing "mean" > gives a "UseMethod" response, while typing "mean.default" give the > actual code: > > > mean > function (x, ...) > UseMethod("mean") > <environment: namespace:base> > > > mean.default > function (x, trim = 0, na.rm = FALSE, ...) > ---SNIP--- > } > <environment: namespace:base> > > Why is this? What does "mean.default" mean? I tried the same > thing with > "prcomp". With the stats package loaded, I cannot get to the > source code > for "prcomp". > > > require(stats) > [1] TRUE > > prcomp > function (x, ...) > UseMethod("prcomp") > <environment: namespace:stats> > > prcomp.default > Error: object "prcomp.default" not found > > How do I find the prcomp code? Are there general rules for finding the > source code for functions that I should know? > > Thanks in Advance, > > Edward J. Wolfrum, Ph.D. > National Renewable Energy Laboratory > Golden, Colorado > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Wolfrum, Ed wrote:> Greetings, > > I am trying to figure out how to find the source code for R functions. I > am specifically interested in finding the code for the "prcomp" > function. I know that typing the function name without parenthesis will > lead to the code (or to a .Internal or .FORTRAN or .C call). However, I > don't really understand what is going on. For example, typing "mean" > gives a "UseMethod" response, while typing "mean.default" give the > actual code: > > >>mean > > function (x, ...) > UseMethod("mean") > <environment: namespace:base> > >>mean.default > > function (x, trim = 0, na.rm = FALSE, ...) > ---SNIP--- > } > <environment: namespace:base> > > Why is this? What does "mean.default" mean? I tried the same thing with > "prcomp". With the stats package loaded, I cannot get to the source code > for "prcomp". > > >>require(stats) > > [1] TRUE > >>prcomp > > function (x, ...) > UseMethod("prcomp") > <environment: namespace:stats>That is the source for prcomp. It's a one-liner, that says to call the prcomp method for whatever type of x you're passing in. As Bert said, you need to read up on methods.> >>prcomp.default > > Error: object "prcomp.default" not foundNotice that prcomp lives in "namespace:stats"? That's the first place to look for prcomp methods. Besides getAnywhere which Bert suggested, you can try stats:::prcomp.default to see the method. (Methods aren't necessarily in the same namespace as the generic, but that's the most common place for them.) Duncan Murdoch> > How do I find the prcomp code? Are there general rules for finding the > source code for functions that I should know? > > Thanks in Advance, > > Edward J. Wolfrum, Ph.D. > National Renewable Energy Laboratory > Golden, Colorado > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
If you're really interested in reading the source for functions and aren't interested in tracking down various methods (possibly hidden in namespaces) at the R prompt, I think it's much easier to download the R source code from CRAN and go through the original source files. -roger Wolfrum, Ed wrote:> Greetings, > > I am trying to figure out how to find the source code for R functions. I > am specifically interested in finding the code for the "prcomp" > function. I know that typing the function name without parenthesis will > lead to the code (or to a .Internal or .FORTRAN or .C call). However, I > don't really understand what is going on. For example, typing "mean" > gives a "UseMethod" response, while typing "mean.default" give the > actual code: > > >>mean > > function (x, ...) > UseMethod("mean") > <environment: namespace:base> > >>mean.default > > function (x, trim = 0, na.rm = FALSE, ...) > ---SNIP--- > } > <environment: namespace:base> > > Why is this? What does "mean.default" mean? I tried the same thing with > "prcomp". With the stats package loaded, I cannot get to the source code > for "prcomp". > > >>require(stats) > > [1] TRUE > >>prcomp > > function (x, ...) > UseMethod("prcomp") > <environment: namespace:stats> > >>prcomp.default > > Error: object "prcomp.default" not found > > How do I find the prcomp code? Are there general rules for finding the > source code for functions that I should know? > > Thanks in Advance, > > Edward J. Wolfrum, Ph.D. > National Renewable Energy Laboratory > Golden, Colorado > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Roger D. Peng http://www.biostat.jhsph.edu/~rpeng/