[R] Default argument not passed to subfunction when argument name matches default expression

Philippe Grosjean phgrosjean at sciviews.org
Mon Mar 31 16:40:55 CEST 2014


Hello,

I have difficulties to understand this one:

foo <- function (y = 2) {
    bar <- function (y = y) y^2
    bar()
}
foo()
#! Error in y^2 : 'y' is missing
foo(3)
#! Error in y^2 : 'y' is missing

Note that this one works:

foo <- function (y = 2) {
    bar <- function (y = y) y^2
    bar(y) # Not using default value for y= argument
}
foo()
#! [1] 4
foo(3)
#! [1] 9

… as well as this one:

foo <- function (y = 2) {
    bar <- function (Y = y) Y^2
    bar() # Default, but different names for argument Y= and value 'y'
 }
foo()
#! [1] 4
foo(3)
#! [1] 9

Best,

PhG

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base 


More information about the R-help mailing list