[R] Function objects as arguments of a function

Laurent Gautier laurent at cbs.dtu.dk
Sat May 25 18:52:53 CEST 2002


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list