Function generation; change defaults of other functions (partial)

独自空忆成欢 提交于 2019-12-04 07:22:51

partial is good for fixing certain parameter values, but if you want to change defaults, you might consider a different strategy. This would work

.paste <- paste
formals(.paste)$collapse <- "_FOO_"

This changes the parameters to the function

args(.paste)
# function (..., sep = " ", collapse = "_FOO_") 
# NULL

Then you can do

.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)
# [1] "1" "2" "3" "4" "5"

This is a canned function taking @MrFlick's great response and putting it into a function for future searchers:

hijack <- function(FUN, ...){

    .FUN <- FUN

    args <- list(...)
    invisible(lapply(seq_along(args), function(i) {
        formals(.FUN)[[names(args)[i]]] <<- args[[i]]
    }))
    .FUN
}

# Now Try It

.paste <- hijack(paste, collapse = "_FOO_")

.paste(1:5)
.paste(1:5, LETTERS[1:5], sep="_BAR_")
.paste(1:5, collapse=NULL)

Yielding

> .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)
[1] "1" "2" "3" "4" "5"

You could just write a simple wrapper

.paste <- function(..., collapse = "_FOO_"){paste(..., collapse = collapse)}

which gives

> .paste <- function(..., collapse = "_FOO_"){paste(..., collapse = collapse)}
> .paste(1:5)
[1] "1_FOO_2_FOO_3_FOO_4_FOO_5"
> .paste(1:5, collapse = NULL)
[1] "1" "2" "3" "4" "5"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!