Skip to content

[R-pkg-devel] Using function with same name as argument as default argument

1 message · John Fox

#
Dear Jan,

Here's yet another solution that takes advantage of partial matching:

 > foo <- function(x, schema. = schema(x)) {
+   if (is.null(schema.)) stop("schema missing")
+   x
+ }

 > schema <- function(x) attr(x, "schema")

 > y <- 1
 > z <- 2
 > attr(y, "schema") <- "bar"

 > foo(y)
[1] 1
attr(,"schema")
[1] "bar"

 > foo(z)
Error in foo(z) : schema missing

 > foo(z, schema="baz")
[1] 2


I hope this helps,
  John
On 2022-08-08 9:10 a.m., Jan van der Laan wrote: