[R] Change Function based on ifelse() condtion

Christofer Bogaso
Sun, 04 Mar 2018 16:52:07 -0000

@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.
>
>