[R] Change Function based on ifelse() condtion

Bert Gunter bgunter.4567 at gmail.com
Sun Mar 4 19:21:11 CET 2018


... and note also:

> f <- function(...)list(...) <- list(...)  ## you cannot have list(...) on
LHS

> f(x=1,y="a")
Error in list(...) <- list(...) : '...' used in an incorrect context


In addition to Eric's suggestions, maybe something like this construction
is useful:

Lapply_me = function(X = X, FUN = FUN, ..., Apply_MC = FALSE) {
   if (Apply_MC) {
      return(mclapply(X, FUN, ...))
   } else {
      if (any(names(list(...)) == 'mc.cores')) {
         l = list(...)[!names(list(...)) %in% 'mc.cores']
      }
      return(do.call(lapply,c(list(X,FUN),l)))
   }
}

> Lapply_me (X=1:5,FUN=sum,mc.cores=4)
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5

Though the program logic needs fixing: if none of the names in list(...)
are "mc.cores," l is undefined!

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Sun, Mar 4, 2018 at 9:58 AM, Eric Berger <ericjberger at gmail.com> wrote:

> The reason that it works for Apply_MC=TRUE is that in that case you call
> mclapply(X,FUN,...) and
> the mclapply() function strips off the mc.cores argument from the "..."
> list before calling FUN, so FUN is being called with zero arguments,
> exactly as it is declared.
>
> A quick workaround is to change the line
>
> Lapply_me(as.list(1:4), function(xx) {
>
> to
>
> Lapply_me(as.list(1:4), function(xx,dummyList) {
>
> HTH,
> Eric
>
>
> On Sun, Mar 4, 2018 at 7:21 PM, Christofer Bogaso <
> bogaso.christofer at gmail.com> wrote:
>
> > Below is my full implementation (tried to make it simple as for
> > demonstration)
> >
> > Lapply_me = function(X = X, FUN = FUN, Apply_MC = FALSE, ...) {
> > if (Apply_MC) {
> > return(mclapply(X, FUN, ...))
> > } else {
> > if (any(names(list(...)) == 'mc.cores')) {
> > myList = list(...)[!names(list(...)) %in% 'mc.cores']
> > }
> > return(lapply(X, FUN, myList))
> > }
> > }
> >
> >
> > Lapply_me(as.list(1:4), function(xx) {
> > if (xx == 1) return('a')
> > if (xx == 2) return('b')
> > if (xx == 3) return('c')
> > if (xx == 4) return('d')
> > }, Apply_MC = FALSE, mc.cores = 2)
> >
> > Error message :
> >
> > Error in FUN(X[[i]], ...) : unused argument (list())
> >
> > Kindly note that, with Apply_MC = TRUE, it is working perfectly.
> >
> > On Sun, Mar 4, 2018 at 10:45 PM, Eric Berger <ericjberger at gmail.com>
> > wrote:
> > > That's fine. The issue is how you called Lapply_me(). What did you pass
> > as
> > > the argument to FUN?
> > > And if you did not pass anything that how is FUN declared?
> > > You have not shown that in your email.
> > >
> > >
> > >
> > >
> > > On Sun, Mar 4, 2018 at 7:11 PM, Christofer Bogaso
> > > <bogaso.christofer at gmail.com> wrote:
> > >>
> > >> My modified function looks below :
> > >>
> > >> Lapply_me = function(X = X, FUN = FUN, Apply_MC = FALSE, ...) {
> > >> if (Apply_MC) {
> > >> return(mclapply(X, FUN, ...))
> > >> } else {
> > >> if (any(names(list(...)) == 'mc.cores')) {
> > >> myList = list(...)[!names(list(...)) %in% 'mc.cores']
> > >> }
> > >> return(lapply(X, FUN, myList))
> > >> }
> > >> }
> > >>
> > >> Here, I am not passing ... anymore rather passing myList
> > >>
> > >> On Sun, Mar 4, 2018 at 10:37 PM, Eric Berger <ericjberger at gmail.com>
> > >> wrote:
> > >> > Hi Christofer,
> > >> > Before you made the change that I suggested, your program was
> stopping
> > >> > at
> > >> > the statement: list(...) = list(..) .etc
> > >> > This means that it never tried to execute the statement:
> > >> > return(lapply(X,FUN,...))
> > >> > Now that you have made the change, it gets past the first statement
> > and
> > >> > tries to execute the statement: return(lapply(X,FUN,...)).
> > >> > That attempt is generating the error message because whatever you
> are
> > >> > passing in as the FUN argument is not expecting extra arguments.
> > >> >
> > >> > HTH,
> > >> > Eric
> > >> >
> > >> >
> > >> > On Sun, Mar 4, 2018 at 6:52 PM, Christofer Bogaso
> > >> > <bogaso.christofer at gmail.com> wrote:
> > >> >>
> > >> >> @Eric - with this approach I am getting below error :
> > >> >>
> > >> >> Error in FUN(X[[i]], ...) : unused argument (list())
> > >> >>
> > >> >> On Sun, Mar 4, 2018 at 10:18 PM, Eric Berger <
> ericjberger at gmail.com>
> > >> >> wrote:
> > >> >> > Hi Christofer,
> > >> >> > You cannot assign to list(...). You can do the following
> > >> >> >
> > >> >> > myList <- list(...)[!names(list(...)) %in% 'mc.cores']
> > >> >> >
> > >> >> > HTH,
> > >> >> > Eric
> > >> >> >
> > >> >> > On Sun, Mar 4, 2018 at 6:38 PM, Christofer Bogaso
> > >> >> > <bogaso.christofer at gmail.com> wrote:
> > >> >> >>
> > >> >> >> Hi,
> > >> >> >>
> > >> >> >> As an example, I want to create below kind of custom Function
> > which
> > >> >> >> either be mclapply pr lapply
> > >> >> >>
> > >> >> >> Lapply_me = function(X = X, FUN = FUN, ..., Apply_MC = FALSE) {
> > >> >> >> if (Apply_MC) {
> > >> >> >> return(mclapply(X, FUN, ...))
> > >> >> >> } else {
> > >> >> >> if (any(names(list(...)) == 'mc.cores')) {
> > >> >> >> list(...) = list(...)[!names(list(...)) %in% 'mc.cores']
> > >> >> >> }
> > >> >> >> return(lapply(X, FUN, ...))
> > >> >> >> }
> > >> >> >> }
> > >> >> >>
> > >> >> >> However when Apply_MC = FALSE it generates below error saying :
> > >> >> >>
> > >> >> >>   '...' used in an incorrect context
> > >> >> >>
> > >> >> >>
> > >> >> >> Appreciate if you can help me with the correct approach. Thanks,
> > >> >> >>
> > >> >> >>
> > >> >> >> On Sun, Mar 4, 2018 at 9:34 PM, Duncan Murdoch
> > >> >> >> <murdoch.duncan at gmail.com>
> > >> >> >> wrote:
> > >> >> >> > On 04/03/2018 10:39 AM, Christofer Bogaso wrote:
> > >> >> >> >>
> > >> >> >> >> Hi again,
> > >> >> >> >>
> > >> >> >> >> I am looking for some way to alternately use 2 related
> > functions,
> > >> >> >> >> based on some ifelse() condition.
> > >> >> >> >>
> > >> >> >> >> For example, I have 2 functions mclapply() and lapply()
> > >> >> >> >>
> > >> >> >> >> However, mclapply() function has one extra parameter
> 'mc.cores'
> > >> >> >> >> which
> > >> >> >> >> lapply doesnt not have.
> > >> >> >> >>
> > >> >> >> >> I know when mc.cores = 1, these 2 functions are essentially
> > same,
> > >> >> >> >> however I am looking for more general way to control them
> > within
> > >> >> >> >> ifelse() constion
> > >> >> >> >>
> > >> >> >> >> Can someone please help me how can I use them within ifelse()
> > >> >> >> >> condition.
> > >> >> >> >
> > >> >> >> >
> > >> >> >> > Don't.  ifelse() usually evaluates *both* the true and false
> > >> >> >> > values,
> > >> >> >> > and
> > >> >> >> > then selects entries from each.  Just use an if statement.
> > >> >> >> >
> > >> >> >> > Duncan Murdoch
> > >> >> >>
> > >> >> >> ______________________________________________
> > >> >> >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more,
> see
> > >> >> >> 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.
> > >> >> >
> > >> >> >
> > >> >
> > >> >
> > >
> > >
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list