Peter Meilstrup
2012-Oct-04 02:37 UTC
[Rd] How to build a list with missing values? What is missing, anyway?
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
Hadley Wickham
2012-Oct-04 03:10 UTC
[Rd] How to build a list with missing values? What is missing, anyway?
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/
Josh O'Brien
2012-Oct-04 06:21 UTC
[Rd] How to build a list with missing values? What is missing, anyway?
>Say I have argnames <- c("a", "b", "c"). >From that I want to construct the equivalent of alist(a=, b=, c=).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.