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

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Mon Aug 8 15:56:03 CEST 2022


On Mon, 8 Aug 2022 15:10:37 +0200
Jan van der Laan <rhelp using eoos.dds.nl> wrote:

> I guess another solution would be:
> 
> foo <- function(x, schema) {
>    if (missing(schema)) schema <- schema(x)
> }
> 
> But then it is not clear for the user from the interface that the 
> 'schema' argument can be omitted.

Here's a workaround that should work inside a package:

function(x, schema = mypackagename::schema(x))

For inline functions, this workaround is also possible, but I can only
think of an extremely ugly variant:

schema <- function(x) { force(x); 'schema' }
foo <- function(
 x, schema = get('schema', parent.env(environment()))(x)
) { force(schema); 'foo' }
foo(1) # works

-- 
Best regards,
Ivan



More information about the R-package-devel mailing list