[R] ifelse() applied on function

William Dunlap wdunlap at tibco.com
Mon Aug 12 05:29:39 CEST 2013


> ifelse(ChooseFn, lapply, sfLapply)(MyList, function(x) {
> ...........
> return(....))

Use 'if', not 'ifelse', and wrap it in parentheses to get the precendence right
  (if (ChooseFn) lapply else sfLapply)(MyList, function(x){...})

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Ron Michael
> Sent: Sunday, August 11, 2013 1:18 PM
> To: r-help at r-project.org
> Subject: [R] ifelse() applied on function
> 
> 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.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list