I have the need for a function generator that takes another function and any arguments of that function and sets new defaults. I thought @hadley's pryr::partial
was that magic function. It does exactly what I want except you can't then change that new default. So here I can change sep
in my new paste
function but not the new default of collapse = "_BAR_"
. How can I make partial
perform this way (i.e., default to collapse = "_BAR_"
but enable setting it to collapse = NULL
if desired)? If this is not possible with partial
is there a way to rewrite the code for partial
to do this: https://github.com/hadley/pryr/blob/master/R/partial.r
library(pryr)
.paste <- pryr::partial(paste, collapse = "_FOO_")
.paste(1:5)
.paste(1:5, LETTERS[1:5], sep="_BAR_")
.paste(1:5, collapse=NULL)
> .paste(1:5)
[1] "1_FOO_2_FOO_3_FOO_4_FOO_5"
> .paste(1:5, LETTERS[1:5], sep="_BAR_")
[1] "1_BAR_A_FOO_2_BAR_B_FOO_3_BAR_C_FOO_4_BAR_D_FOO_5_BAR_E"
> .paste(1:5, collapse=NULL)
Error in paste(collapse = "_FOO_", ...) :
formal argument "collapse" matched by multiple actual arguments
You could just write a simple wrapper
which gives
This is a canned function taking @MrFlick's great response and putting it into a function for future searchers:
# Now Try It
Yielding
partial
is good for fixing certain parameter values, but if you want to change defaults, you might consider a different strategy. This would workThis changes the parameters to the function
Then you can do