[BioC] Dynamic Function creation

Hervé Pagès hpages at fhcrc.org
Thu Mar 10 22:13:34 CET 2011


Hi Davide,

On 03/10/2011 05:57 AM, Steve Lianoglou wrote:
> Hi,
>
> This question is more appropriate for R-help, so if you want to follow
> up, send the question there.

Also if you follow Steve's suggestion, try to change the subject. I
don't think this has to do with "dynamic" function creation or
"anonymous" functions.

Cheers,
H.

>
> On Thu, Mar 10, 2011 at 6:36 AM, Davide Rambaldi
> <davide.rambaldi at ifom-ieo-campus.it>  wrote:
>> Ehm sorry I made a mistake in my example:
>>
>> a<- function(parS,xx) { parS$a^2 + parS$M }
>> b<- function(parS,xx) { parS$b^2 + parS$M }
>>
>> c<- a + b
>>
>> function(parS,xx) {  (parS$a^2 + parS$M) + (parS$b^2 + parS$M ) }
>
> I guess it depends on how general you want it to be, but you could do
> it something like this. I'm redefining your base functions to accept
> dots `...` (and forget about your param container parS(?)):
>
> ## The following is untested
> f.a<- function(a, M, ...) { a^2 + M }
> f.b<- function(b, M, ...) { b^2 + M }
>
> chain<- function(func.list, ..., .chainWith="+") {
>    chainWith<- getFunction(.chainWith)
>    result<- func.list[[1]](...)
>    if (length(result)>  1) {
>      for (fn in func.list[-1]) {
>        result<- chainWith(result, fn(...))
>      }
>    }
>    result
> }
>
> And call it like this:
>
> R>  chain(list(a.f, b.f), a, b, M)
>
> You can make `chain` take a list of `.chainWith` functions if you
> don't want to combine the result of all functions with the same
> operator.
>
> If you're feeling extra adventurous, maybe you can define
> "+.function", "*.function", etc. that will enable you to combine
> functions in a more natural looking way that would look more like your
> origina `c<- a + b` type of thing, but I reckon that could get pretty
> tricky.
>
> -steve
>
>
>> Regards
>>
>> On Mar 10, 2011, at 12:23 PM, Davide Rambaldi wrote:
>>
>>> Hello, may be is not the correct place to ask this but: it is possible to create functions in R dynamically?
>>>
>>> (May be the correct name is anonymous functions ....)
>>>
>>> I want something like:
>>>
>>> a<- function(parS,xx) { parS$a^2 + parS$M }
>>> b<- function(parS,xx) { parS$b^2 + parS$M }
>>>
>>> c<- a + b
>>>
>>> where c is:
>>>
>>> function(parS,xx) {  parS$a^2*exp(-((xx-parS$M)^2)/(2*parS$S^2))  + parS$b^2*exp(-((xx-(parS$M - parS$D))^2)/(2*parS$S^2)) }
>>>
>>> Regards
>>>
>>>
>>> Davide R.
>>>
>>> _______________________________________________
>>> Bioconductor mailing list
>>> Bioconductor at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/bioconductor
>>> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor
>>
>> Davide Rambaldi, Bioinformatics PostDoc.
>> -----------------------------------------------------
>> IFOM-IEO Campus
>> Via Adamello 16, Milano
>> I-20139 Italy
>> [t] +39 02574303870
>> [e] davide.rambaldi at ifom-ieo-campus.it
>>
>> _______________________________________________
>> Bioconductor mailing list
>> Bioconductor at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/bioconductor
>> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor
>>
>
>
>


-- 
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M2-B876
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the Bioconductor mailing list