From h.wickham at gmail.com  Thu Oct  4 05:10:00 2012
From: h.wickham at gmail.com (Hadley Wickham)
Date: Wed, 3 Oct 2012 22:10:00 -0500
Subject: [Rd] How to build a list with missing values? What is missing,
	anyway?
In-Reply-To: <CAJoaRhb8gq79aB3EXPeMa+n6eK6pj9NTFHFBJRUbP_=S1bbTaA@mail.gmail.com>
References: <CAJoaRhb8gq79aB3EXPeMa+n6eK6pj9NTFHFBJRUbP_=S1bbTaA@mail.gmail.com>
Message-ID: <CABdHhvE2DvG630dZCwuExkKuq1muUgB+gGKhpuDdAoW17bJo+A@mail.gmail.com>

On Wed, Oct 3, 2012 at 9:37 PM, Peter Meilstrup
<peter.meilstrup at gmail.com> wrote:
> 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=).

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

-- 
RStudio / Rice University
http://had.co.nz/



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