function within a function body?
2
0
Entering edit mode
@shen-weicheng-296
Last seen 10.1 years ago
Hi all: In studying the code for bioconductor, I found something that I don't understand. The function implementation that I can not understand is "maLowessLines." The function is listed below. The syntex that I don't understand is "function(x, y, z)". It appears that this is a function within a function (maLowessLines). What is the name of this function? Where does it get the values of x, y, z? Any hints and suggestions will be greatly appreciated. Thanks! ====================================================================== ====== =================================== maLowessLines <- function (subset = TRUE, f = 0.3, col = 2, lty = 1, lwd = 2.5, ...) { function(x, y, z) { subset <- maNum2Logic(length(x), subset) g <- unique(z[subset]) if (length(col) < length(g)) col <- rep(col[1], length(g)) if (length(lty) < length(g)) lty <- rep(lty[1], length(g)) for (i in (1:length(g))) { which <- z[subset] == g[i] xx <- x[subset][which] yy <- y[subset][which] ind <- is.na(xx) | is.na(yy) | is.infinite(xx) | is.infinite(yy) fit <- lowess(xx[!ind], yy[!ind], f = f) lines(fit, col = col[i], lty = lty[i], lwd = lwd, ...) } } } Weicheng Shen, Ph.D. Senior Image Analyst Science Applications International Corporation 1710 SAIC Drive Mail Stop 2-6-9 McLean, VA 22102 (703) 676-4189
• 742 views
ADD COMMENT
0
Entering edit mode
rgentleman ★ 5.5k
@rgentleman-7725
Last seen 9.4 years ago
United States
Hi, Functions do not have to have names. The name is merely a symbol that is used to identify and manipulate the function, but you can (and we often do) have anonymous functions. For example: (function(x,y){x+y})(4,6) if you type this at the R prompt you will get the value 10 returned. The value of x is 4, of y is 6. The first statement (in parentheses) defines a function. The second set of parentheses indicated that you want to apply that function to the arguments given (4 and 6). There are a number of resources on S language programming available to help with understanding the language (some free, others not). Robert On Fri, May 16, 2003 at 09:48:38AM -0400, SHEN, WEICHENG . wrote: > > Hi all: > > In studying the code for bioconductor, I found something that I don't > understand. The function implementation that I can not understand is > "maLowessLines." The function is listed below. The syntex that I don't > understand is "function(x, y, z)". It appears that this is a function within > a function (maLowessLines). What is the name of this function? Where does it > get the values of x, y, z? Any hints and suggestions will be greatly > appreciated. Thanks! > > ==================================================================== ======== > =================================== > > > maLowessLines <- function (subset = TRUE, f = 0.3, col = 2, lty = 1, lwd = > 2.5, > ...) > { > function(x, y, z) { > subset <- maNum2Logic(length(x), subset) > g <- unique(z[subset]) > if (length(col) < length(g)) > col <- rep(col[1], length(g)) > if (length(lty) < length(g)) > lty <- rep(lty[1], length(g)) > for (i in (1:length(g))) { > which <- z[subset] == g[i] > xx <- x[subset][which] > yy <- y[subset][which] > ind <- is.na(xx) | is.na(yy) | is.infinite(xx) | > is.infinite(yy) > fit <- lowess(xx[!ind], yy[!ind], f = f) > lines(fit, col = col[i], lty = lty[i], lwd = lwd, > ...) > } > } > } > > Weicheng Shen, Ph.D. > Senior Image Analyst > Science Applications International Corporation > 1710 SAIC Drive > Mail Stop 2-6-9 > McLean, VA 22102 > (703) 676-4189 > > _______________________________________________ > Bioconductor mailing list > Bioconductor@stat.math.ethz.ch > https://www.stat.math.ethz.ch/mailman/listinfo/bioconductor -- +--------------------------------------------------------------------- ------+ | Robert Gentleman phone : (617) 632-5250 | | Associate Professor fax: (617) 632-2444 | | Department of Biostatistics office: M1B20 | | Harvard School of Public Health email: rgentlem@jimmy.harvard.edu | +--------------------------------------------------------------------- ------+
ADD COMMENT
0
Entering edit mode
@wolfgang-huber-3550
Last seen 5 weeks ago
EMBL European Molecular Biology Laborat…
Hi Weicheng, ..to follow up on Robert's email: the scoping rules are different from what many people are used from other languages like C or Matlab, and are nicely described in "Lexical Scope and Statistical Computing" by R. Gentleman and R. Ihaka, available from http://cran.r-project.org/doc/misc/ Best regards Wolfgang ------------------------------------- Wolfgang Huber Division of Molecular Genome Analysis German Cancer Research Center Heidelberg, Germany Phone: +49 6221 424709 Fax: +49 6221 42524709 Http: www.dkfz.de/mga/whuber
ADD COMMENT

Login before adding your answer.

Traffic: 463 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6