Unused arguments in R

后端 未结 5 1681
暖寄归人
暖寄归人 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条回答
  •  梦毁少年i
    2020-12-25 13:14

    The R.utils package has a function called doCall which is like do.call, but it does not return an error if unused arguments are passed.

    multiply <- function(a, b) a * b
    
    # these will fail
    multiply(a = 20, b = 30, c = 10)
    # Error in multiply(a = 20, b = 30, c = 10) : unused argument (c = 10)
    do.call(multiply, list(a = 20, b = 30, c = 10))
    # Error in (function (a, b)  : unused argument (c = 10)
    
    # R.utils::doCall will work
    R.utils::doCall(multiply, args = list(a = 20, b = 30, c = 10))
    # [1] 600
    # it also does not require the arguments to be passed as a list
    R.utils::doCall(multiply, a = 20, b = 30, c = 10)
    # [1] 600
    

提交回复
热议问题