Hello, R users. In C (or C++) language, a function can be used as an argument of another function as follows: // function used as an argument void foo(int x) { ... } // function using a function as an argument void bar(void (*func)(int ), int arg1, int arg2) { .... } // The function 'bar' will be called as follows int main() { .... bar(foo, arg4foo, other_arg); ... } In R, can function objects be used as arguments of a function? Thanks in advance from Japan ##################################### # Tsumiyama Isao (Mitsubishi Pharma Corp.) # Tel:047-471-7721 # PHS:070-5659-0544 # e-mail:tsumi at leo.auric.ne.jp # tsumiyama at pdx.ne.jp(PHS) ##################################### -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Yes! And it's much more straightforward and clear in R (in my opinion). -roger _______________________________ UCLA Department of Statistics rpeng at stat.ucla.edu http://www.stat.ucla.edu/~rpeng On Sat, 25 May 2002, Tsumiyama Isao wrote:> Hello, R users. > > In C (or C++) language, a function can be used as > an argument of another function as follows: > > // function used as an argument > void foo(int x) > { > ... > } > > // function using a function as an argument > void bar(void (*func)(int ), int arg1, int arg2) > { > .... > } > > // The function 'bar' will be called as follows > int main() > { > .... > > bar(foo, arg4foo, other_arg); > > ... > } > > In R, can function objects be used as arguments of > a function? > > Thanks in advance from Japan > > ##################################### > # Tsumiyama Isao (Mitsubishi Pharma Corp.) > # Tel:047-471-7721 > # PHS:070-5659-0544 > # e-mail:tsumi at leo.auric.ne.jp > # tsumiyama at pdx.ne.jp(PHS) > ##################################### > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Sat, May 25, 2002 at 10:59:10PM +0900, Tsumiyama Isao wrote:> Hello, R users. > > In C (or C++) language, a function can be used as > an argument of another function as follows: > > // function used as an argument > void foo(int x) > { > ... > } > > // function using a function as an argument > void bar(void (*func)(int ), int arg1, int arg2) > { > .... > } > > // The function 'bar' will be called as follows > int main() > { > .... > > bar(foo, arg4foo, other_arg); > > ... > } > > In R, can function objects be used as arguments of > a function? > > Thanks in advance from Japan > > ##################################### > # Tsumiyama Isao (Mitsubishi Pharma Corp.) > # Tel:047-471-7721 > # PHS:070-5659-0544 > # e-mail:tsumi at leo.auric.ne.jp > # tsumiyama at pdx.ne.jp(PHS) > #####################################Dear Tsumiyama Isao, Functions can be passed as arguments. The function 'apply' or 'by' demonstrates how it is done (these are only examples). You may want to refer to documentation for them (try 'help(apply)' or 'help(by)'). You can also have functions returning functions. Using closures it can be very powerful. An example of what can be the interest of this is found in the previous issue of Rnews (around page 12 if I recollect correctly... I won't name the package or the project it is related to, it would be a coarse way of advertising ;) ). Hopin' it helps, Laurent -------------------------------------------------------------- Laurent Gautier CBS, Building 208, DTU PhD. Student D-2800 Lyngby,Denmark tel: +45 45 25 24 85 http://www.cbs.dtu.dk/laurent -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._