Passing missing argument from function to function in R

前端 未结 4 1740
野性不改
野性不改 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:17

    You can create a missing object by using substitute() without argument.

    In your case we could make round1 a missing object in the else clause :

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

    Created on 2019-10-24 by the reprex package (v0.3.0)

提交回复
热议问题