Using “…” and “replicate”

前端 未结 4 1016
庸人自扰
庸人自扰 2020-12-20 11:47

In the documentation of sapply and replicate there is a warning regarding using ...

Now, I can accept it as such, but would li

4条回答
  •  伪装坚强ぢ
    2020-12-20 12:10

    There actually is a way to do this:

    # Simple function:
    ff <- function(a,b) print(a+b)
    
    # This will NOT work:
    testf <- function(...) {
      replicate(expr = ff(...), n = 5)
    }
    testf(45,56) # argument "b" is missing, with no default
    
    # This will:
    testf <- function(...) {
      args <- as.list(substitute(list(...)))[-1L]
      replicate(expr = do.call(ff, args), n = 5)
    }
    testf(45,56) # 101
    

提交回复
热议问题