[BioC] Dynamic Function creation

Steve Lianoglou mailinglist.honeypot at gmail.com
Thu Mar 10 14:57:06 CET 2011


Hi,

This question is more appropriate for R-help, so if you want to follow
up, send the question there.

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
>



-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the Bioconductor mailing list