a logical indicating whether to copy all values or
(default) only those whose names do not begin with a dot.
sorted
a logical indicating whether the names of
the resulting list should be sorted (increasingly). Note that this
is somewhat costly, but may be useful for comparison of environments.
Details
Almost all lists in R internally are Generic Vectors, whereas
traditional dotted pair lists (as in LISP) remain available but
rarely seen by users (except as formals of functions).
The arguments to list or pairlist are of the form
value or tag = value. The functions return a list or
dotted pair list composed of its arguments with each value either
tagged or untagged, depending on how the argument was specified.
alist handles its arguments as if they described function
arguments. So the values are not evaluated, and tagged arguments with
no value are allowed whereas list simply ignores them.
alist is most often used in conjunction with formals.
as.list attempts to coerce its argument to a list. For
functions, this returns the concatenation of the list of formal
arguments and the function body. For expressions, the list of
constituent elements is returned. as.list is generic, and as
the default method calls as.vector(mode = "list") for a
non-list, methods for as.vector may be invoked. as.list
turns a factor into a list of one-element factors. Attributes may
be dropped unless the argument already is a list or expression. (This
is inconsistent with functions such as as.character
which always drop attributes, and is for efficiency since lists can be
expensive to copy.)
is.list returns TRUE if and only if its argument
is a listor a pairlist of length> 0.
is.pairlist returns TRUE if and only if the argument
is a pairlist or NULL (see below).
The "environment" method for as.list copies the
name-value pairs (for names not beginning with a dot) from an
environment to a named list. The user can request that all named
objects are copied. Unless sorted = TRUE, the list is in no
particular order (the order
depends on the order of creation of objects and whether the
environment is hashed). No enclosing environments are searched.
(Objects copied are duplicated so this can be an expensive operation.)
Note that there is an inverse operation, the
as.environment() method for list objects.
An empty pairlist, pairlist() is the same as
NULL. This is different from list().
as.pairlist is implemented as as.vector(x,
"pairlist"), and hence will dispatch methods for the generic function
as.vector. Lists are copied element-by-element into a pairlist
and the names of the list used as tags for the pairlist: the return
value for other types of argument is undocumented.
list, is.list and is.pairlist are
primitive functions.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
The New S Language.
Wadsworth & Brooks/Cole.
See Also
vector("list", length) for creation of a list with empty
components; c, for concatenation; formals.
unlist is an approximate inverse to as.list().
‘plotmath’ for the use of list in plot annotation.
Examples
require(graphics)
# create a plotting structure
pts <- list(x = cars[,1], y = cars[,2])
plot(pts)
is.pairlist(.Options) # a user-level pairlist
## "pre-allocate" an empty list of length 5
vector("list", 5)
# Argument lists
f <- function() x
# Note the specification of a "..." argument:
formals(f) <- al <- alist(x = , y = 2+3, ... = )
f
al
## environment->list coercion
e1 <- new.env()
e1$a <- 10
e1$b <- 20
as.list(e1)
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(base)
> png(filename="/home/ddbj/snapshot/RGM3/R_rel/result/base/list.Rd_%03d_medium.png", width=480, height=480)
> ### Name: list
> ### Title: Lists - Generic and Dotted Pairs
> ### Aliases: list pairlist alist as.list as.list.default as.list.data.frame
> ### as.list.environment as.list.factor as.list.function as.pairlist
> ### is.list is.pairlist
> ### Keywords: list manip
>
> ### ** Examples
>
> require(graphics)
>
> # create a plotting structure
> pts <- list(x = cars[,1], y = cars[,2])
> plot(pts)
>
> is.pairlist(.Options) # a user-level pairlist
[1] TRUE
>
> ## "pre-allocate" an empty list of length 5
> vector("list", 5)
[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
[[4]]
NULL
[[5]]
NULL
>
> # Argument lists
> f <- function() x
> # Note the specification of a "..." argument:
> formals(f) <- al <- alist(x = , y = 2+3, ... = )
> f
function (x, y = 2 + 3, ...)
x
> al
$x
$y
2 + 3
$...
>
> ## environment->list coercion
>
> e1 <- new.env()
> e1$a <- 10
> e1$b <- 20
> as.list(e1)
$a
[1] 10
$b
[1] 20
>
>
>
>
>
> dev.off()
null device
1
>