[Rd] how to prevent default argument values from being found

William Dunlap wdunlap at tibco.com
Tue Oct 15 01:30:30 CEST 2013


Here is a problem I ran across in RStudio, which uses masks a number of standard R functions with versions that do a bit more than the original does.  The RStudio people have figured out a way to prevent default values of arguments from being found when an argument is missing from a call!

junk1 <- function(...) {
    fun <- function(pkgs, lib = NULL)
    {
        list(missing=missing(lib),
             lib=try(lib, silent = TRUE),
             pkgs = pkgs)
    }
    hook <- function(FUN, pkgs, lib, ...)
    {
        FUN(pkgs, lib, ...)
    }
    hook(fun, ...) 
}

When we run this with one argument, it reports that fun's 'lib' argument is missing
but it dies saying there is no default value.  But there is a default value: NULL.
(If I leave out the try() I get the same problem.)

> junk1("aPackage")
$missing
[1] TRUE

$lib
[1] "Error in try(lib, silent = TRUE) : \n  argument \"lib\" is missing, with no default\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in doTryCatch(return(expr), name, parentenv, handler): argument "lib" is missing, with no default>

$pkgs
[1] "aPackage"

If we change that hook function so it does not assume that the 'pkgs' and 'libs' arguments are
passed in then default argument evaluation works as expected.

junk2 <- function (...)
{
    fun <- function(pkgs, lib = NULL) {
        list(missing = missing(lib), lib = try(lib, silent = TRUE),
            pkgs = pkgs)
    }
    hook <- function(FUN, ...) {
        FUN(...)
    }
    hook(fun, ...)
}
> junk2("aPackage")
$missing
[1] TRUE

$lib
NULL

$pkgs
[1] "aPackage"

It is curious that one can circumvent normal R argument processing.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com



More information about the R-devel mailing list