Unused arguments in R

后端 未结 5 1680
暖寄归人
暖寄归人 2020-12-25 12:22

In R, is it possible to have a the software ignore the fact that there are unused arguments defined when a module is run?

For example, I have a module <

5条回答
  •  长发绾君心
    2020-12-25 13:05

    I had the same problem as you. I had a long list of arguments, most of which were irrelevant. I didn't want to hard code them in. This is what I came up with

    library(magrittr)
    do_func_ignore_things <- function(data, what){
        acceptable_args <- data[names(data) %in% (formals(what) %>% names)]
        do.call(what, acceptable_args %>% as.list)
    }
    
    do_func_ignore_things(c(n = 3, hello = 12, mean = -10), "rnorm")
    # -9.230675 -10.503509 -10.927077
    

提交回复
热议问题