Passing missing argument from function to function in R

前端 未结 4 1741
野性不改
野性不改 2020-12-31 04:13

I’ve learned that it’s common practice to use optional arguments in function and check them with missing() (e.g. as discussed in SO 22024082)

In this example round0

4条回答
  •  执笔经年
    2020-12-31 04:15

    rlang provides also a function missing_arg() that makes an argument be missing.

    foo = function(a, round0) {
      a = a * pi
      if(!missing(round0)) round(a)
      else a
    }
    
    bar = function(b) {
      if(b > 10) round1 <- TRUE else round1 <- rlang::missing_arg()
      foo(b, round1)
    }
    
    foo(9)
    #> [1] 28.27433
    bar(9)
    #> [1] 28.27433
    

    Created on 2020-12-02 by the reprex package (v0.3.0)

提交回复
热议问题