Skip to content

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

10 messages · Peter Meilstrup, Hadley Wickham, Josh O'Brien +3 more

#
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").
"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:
Error in list(the_missing_value) :  'the_missing_value' is missing
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
#
On Wed, Oct 3, 2012 at 9:37 PM, Peter Meilstrup
<peter.meilstrup at gmail.com> wrote:
Here's one approach:

args <- alist()
for (n in argnames) {
  args[[n]] <- bquote()
}

You have to be really careful dealing with the missing symbol, because
if R attempts to evaluate it, you get the missing argument error.

Hadley
#
Here's a one liner that'll do that for you:

argnames <- letters[1:3]
setNames(rep(list(bquote()), length(argnames)), argnames)

- Josh



--
View this message in context: http://r.789695.n4.nabble.com/How-to-build-a-list-with-missing-values-What-is-missing-anyway-tp4644957p4644965.html
Sent from the R devel mailing list archive at Nabble.com.
#
Why not just use the list constructor:

theList <- setNames(vector("list",3),letters[1:3])

## The list components are empty = NULL, not NA)

This also doesn't seem to be an R-devel topic.

-- Bert
On Wed, Oct 3, 2012 at 11:21 PM, Josh O'Brien <joshmobrien at gmail.com> wrote:

  
    
#
On Wed, Oct 3, 2012 at 11:21 PM, Josh O'Brien <joshmobrien at gmail.com> wrote:
Thanks.

Just so I have my mental model correct, I'm gathering that missing/``
is a symbol that the interpreter has a special rule for -- evaluating
it raises an error, as opposed to objects that evaluate to themselves
or variable names that evaluate to objects.

Does the same sort of thing explain the behavior of `...`? When the
interpreter comes across `...` in the arguments during evaluation of a
call, it trips a special argument-interpolating behavior?

Peter
#
The R Language definition manual explains all of this. Read it.

-- Bert

On Thu, Oct 4, 2012 at 3:53 PM, Peter Meilstrup
<peter.meilstrup at gmail.com> wrote:

  
    
#
On Thu, Oct 4, 2012 at 6:12 PM, Bert Gunter <gunter.berton at gene.com> wrote:
I always reread that before I post to this list.

The only relevant mention of "missing" in the R Language Definition
that I could find were in section 4.1.2 on page 25, and that section
did not answer anything about "missing" as a special symbol (e.g. that
my be put in a formal argument list.)

There is another mention of `...` in section 4.3.2 and it does not
explain some things, such as under what circumstances one would get a
"`...` used in incorrect context" error.

Peter
#
On Thu, Oct 4, 2012 at 6:31 PM, Peter Meilstrup
<peter.meilstrup at gmail.com> wrote:
Page 15, "NA handling" seems "relevant, " as does ?NA (at the command
prompt in R).
See also 2.5 in the Introduction to R tutorial.
See section 2.1.9 of the Language Definition manual and 10.4 of Intro to R.
How could it possibly know that?

-- Bert

  
    
#
On Oct 5, 2012, at 03:31 , Peter Meilstrup wrote:

            
It's not defined as an R object. We did consider doing so briefly, but the semantics would be weird, as you have experienced: An R object that can't be evaluated without triggering an error. 

It is something that is used by the internals, so that missing values propagate with lazy evaluation semantics. It happens to be implemented as the equivalent of as.name(""), but that's not to be relied upon. It leaks out into user space because arguments can have missing defaults, and there needs to be a way to manipulate argument lists. It also slips out in substitute() with no argument, and I suppose that too is at least semi-intentional. It is not completely obvious that the two are necessarily identical.

It's one of those things where you shouldn't rely on behavior outside of well-establihed idioms. If you do, and it breaks, you get to keep both pieces...

I suppose we could try documenting it better, but it would be at the risk of authorizing semantics that we might want to change (e.g., it is far from clear that we should be allowing direct assignment of missings to simple symbols).

("..." and friends is a somewhat different kettle of fish which I don't want to go into just now.)

-pd

  
    
2 days later
#
On Fri, Oct 5, 2012 at 4:07 PM, Bert Gunter <gunter.berton at gene.com> wrote:
By looking at the code that throws the error.

The code is in src/main/eval.c, and the error is thrown in several
places when the symbol ... appears in a call being evaluated, and the
value of the symbol is neither the special DOTSXP type nor NULL.  That
is, if you use ... in code and there isn't a ... formal argument in
scope.

   -thomas