[Rd] How to build a list with missing values? What is missing, anyway?

Peter Meilstrup peter.meilstrup at gmail.com
Thu Oct 4 04:37:24 CEST 2012


This is tangentially related to Hadley's question.

Suppose I'm building a function programmatically; I have assembled an
expression for the body and I know the names of the arguments it wants
to take.

Suppose I have some convenience function such that writing
    make_function(alist(a=, b=), quote(a+b), environment())
is equivalent to writing
    function(a,b) a+b

So how do I make the list that goes in the first argument? Suppose I
start with a character vector of argument names. What I need is a list
with names and quoted values, but the values are "empty".

Say I have argnames <- c("a", "b", "c").
>From that I want to construct the equivalent of alist(a=, b=, c=).

"missing" seems different from NULL; if I do

arglist <- rep(list(NULL), length(argnames))
names(arglist) <- argnames
make_function(arglist, quote(a+b+c))

I get a function where NULL is the default value for each argument,
rather than a function with no default values.

It seems I can assign a "missing value" to a variable by

the_missing_value <- formals(function(x) NULL)[[1]]

or by

the_missing_value = quote(expr=)

However this backfires when I try to use it:

> arglist <- rep(list(the_missing_value), length(argnames))
Error in list(the_missing_value) :  'the_missing_value' is missing

> arglist <- rep(list(NULL), 3)
> arglist[] <- the_missing_value
Error: argument "the_missing_value" is missing, with no default

Besides which, roxygen2 (and other package tools) choked on
encountering a missing value assigned to a name in my package.

So, what's going on with missing? Missing and '...' are the two pieces
of R's semantics I'm most fuzzy on.

(Somewhere along this experimentation I also managed to assign a value
to the empty name, and could not figure out how to remove it...)

Peter



More information about the R-devel mailing list