Change default arguments of an R function at runtime

后端 未结 4 2193
离开以前
离开以前 2021-02-19 22:59

Is it possible to change the default values of formal parameters in an R function at runtime?

Let\'s assume, we have the function

f <- function(x=1) {         


        
4条回答
  •  后悔当初
    2021-02-19 23:40

    As the Defaults package is no longer available from CRAN, you can use default.

    As an example:

    x <- list(a = 1, b = 2, c = 3)
    default::default(unlist) <- list(use.names = FALSE)
    unlist(x)
    #> [1] 1 2 3
    
    unlist <- default::reset_default(unlist)
    unlist(x)
    #> a b c 
    #> 1 2 3
    

    Created on 2019-03-22 by the reprex package (v0.2.0.9000).

提交回复
热议问题