9

In this Answer, an alist() is proposed as an easy means of creating a list with empty elements. A use case for this would be constructing a list suitable for a call to [ arranged via do.call(). For example:

x <- matrix(1:6, ncol = 2)
do.call(`[`, alist(x, , 2)) ## extract column 2 of x

[1] 4 5 6

The particular Question prompting the alist() Answer required the setting of the empty arguments dynamically on basis of an object shortdim.

If one knew how many dimensions were present one could do

al <- alist( , , ) ## 3 arguments for a 2-d object
al[[1]] <- x
shortdim <- 1
al[[shortdim + 1]] <- 1:2 ## elements 1 & 2 of dim shortdim, plus all other dims
do.call(`[`, al) 

> do.call(`[`, al) 
     [,1] [,2]
[1,]    1    4
[2,]    2    5
> x[1:2, ]         ## equivalent too
     [,1] [,2]
[1,]    1    4
[2,]    2    5

A list of dynamic length can be created by vector(), eg

ll <- vector(mode = "list", length = length(dim(x)) + 1)

But an alist can't be made in that way

> vector(mode = "alist", length = length(dim(x)) + 1)
Error in vector(mode = "alist", length = length(dim(x)) + 1) : 
  vector: cannot make a vector of mode 'alist'.

Is there a way to create an alist of dynamic length that can be filled in later where required?

Community
  • 1
  • 1
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • 1
    Annoyingly, `alist()` doesn't create a list of length 1, but `alist(,)` creates a list of length 2.... – Hong Ooi Jul 19 '13 at 17:29
  • 4
    Related (and contains the answer) [Select along one of n dimensions in array](http://stackoverflow.com/a/14502298/271616) – Joshua Ulrich Jul 19 '13 at 17:58
  • If either @JoshuaUlrich or @JoshO'Brien want to post the `rep(list(bquote()), n)` idiom as an Answer I am inclined to accept that as an `alist` is actually nothing of the sort, it is a `"list"`, which is what this produces too. If you don't want to I'll post it as an Answer. – Gavin Simpson Jul 19 '13 at 18:56
  • 1
    @GavinSimpson -- Done. When I have more time, I'll probably further populate it with links to the various R-devel threads and SO questions that address this topic, since it's sort of interesting and mostly undocumented. (Or, anyone else, feel free to add those to my answer.) – Josh O'Brien Jul 19 '13 at 21:19

2 Answers2

16

OK, I'll bite. I would probably use list(bquote()) to construct a one element list containing the empty symbol, and rep that out to the desired length.

n <- 2
rep(list(bquote()), n)
# [[1]]
# 
# 
# [[2]]
# 
# 

As a bonus, here is a collection of 5 ways to create/access the empty symbol that is needed as the content of each list element:

bquote()
# 
substitute()
# 
quote(expr= )
# 
formals(function(x) {})$x
# 
alist(,)[[1]]
# 
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • If using rlang, `expr()` works too, e.g. `rep(list(expr()), n)` – alistaire Mar 25 '18 at 21:48
  • `expression(,)[[1]]` works too. However I don't see why `as.symbol("")` is forbidden, since `is.symbol(bquote())` is true and `as.character(bquote())` is `""`. –  Aug 09 '21 at 19:32
  • I'm guessing that "" cannot be entered into the symbol table whereas `is.symbol` is evaluating whether the argument of `quote(bquote())` is a valid R expression. The error message for `s.symbol("")` indicates that it tests for zero-length. Trying `as.symbol(bquote())` gives no error at a console input line. Perhaps a naked language object gets a temporary name while the ambiguities are being resolved. – IRTFM Feb 17 '23 at 16:28
5

Thanks to Ferdinand Kraft:

# no. of elements in the alist
n <- 5
a <- rep(alist(,)[1], n)
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187