[R] ifelse() applied on function

Steve Lianoglou lianoglou.steve at gene.com
Sun Aug 11 22:39:55 CEST 2013


Hi,

On Sun, Aug 11, 2013 at 1:18 PM, Ron Michael <ron_michael70 at yahoo.com> wrote:
> Hi,
>
> How can I apply ifelse function to chose appropriate function?
>
> My goal is user will chose lapply() function if "ChooseFn = T" otherwise to chose sfLapply() from snowfall package.
>
> I am basically trying to avoid repeatation to write all internal steps, if user choses lapply or sfLapply. For example, I am wondering if there is any posibility to write something like:
>
> ifelse(ChooseFn, lapply, sfLapply)(MyList, function(x) {
>
> ...........
> ..........
> return(....))
>
> Really appreciate if someone helps me out.

How about something like:

loop <- if (ChooseFn) lapply else sfLapply

result <- loop(MyList, function(x) {
  ## ...
})

Should work as long as `sfLapply` has same function signature as lapply.

HTH,

-steve

-- 
Steve Lianoglou
Computational Biologist
Bioinformatics and Computational Biology
Genentech



More information about the R-help mailing list