listing {pkgutils} | R Documentation |
Create some kind of listing, used, e.g., in (error) messages or warnings. Alternatively, make an object ‘flat’, such as by creating a non-nested list from a list, or expand it after splitting certain components.
## S4 method for signature 'ANY' flatten(object, ...) ## S4 method for signature 'list' flatten(object, use.names = TRUE, ...) ## S4 method for signature 'ANY' listing(x, ...) ## S4 method for signature 'character' listing(x, header = NULL, footer = NULL, prepend = FALSE, style = "list", collapse = if (style == "sentence") "" else "\n", force.numbers = FALSE, last.sep = c("and", "both", "comma", "or", "two"), hf.collapse = collapse, ...) ## S4 method for signature 'factor' listing(x, ...) ## S4 method for signature 'list' listing(x, ...) ## S4 method for signature 'numeric' listing(x, ...) ## S4 method for signature 'character' unnest(object, sep, fixed = TRUE, ..., stringsAsFactors = FALSE) ## S4 method for signature 'data.frame' unnest(object, sep, col = names(object), fixed = TRUE, ..., stringsAsFactors = FALSE) ## S4 method for signature 'list' unnest(object, ..., stringsAsFactors = FALSE)
object |
Usually a list. The default method just
returns |
use.names |
Logical scalar passed to |
x |
For the default method, an object convertible
via |
header |
|
footer |
|
prepend |
Logical, numeric or character scalar. The two main uses are:
|
style |
Character scalar. The main options are:
Note that names and values of |
collapse |
Character scalar used to join the
resulting vector elements. It is by default also applied
for joining |
force.numbers |
Logical scalar. Always use numbers instead of the ‘names’ attribute? |
last.sep |
Character scalar indicating what should
be used as last separator if |
hf.collapse |
Character scalar or empty. If distinct
from |
... |
Optional other arguments passed to
|
sep |
Character scalar passed as |
col |
Character vector with the names of the columns
to be passed to |
fixed |
Logical scalar passed to |
stringsAsFactors |
Logical scalar passed to
|
Character scalar in the case of listing
, data
frame in the case of unnest
.
base::message base::warning base::stop base::formatDL
Other coding-functions: L
,
LL
, assert
,
case
, check
, collect
,
contains
, map_names
,
map_values
, must
,
set
, sql
,
## listing()
# default style
x <- structure(letters[1:5], names = LETTERS[1:5])
(y <- listing(x, "Five letters:", "...end here", 1))
## [1] "Five letters:\n A: a\n B: b\n C: c\n D: d\n E: e\n...end here"
stopifnot(length(y) == 1, y ==
"Five letters:\n A: a\n B: b\n C: c\n D: d\n E: e\n...end here")
# 'sentence' style
(y <- listing(letters[1:3], style = "sentence", last.sep = "both"))
## [1] "a, b, and c"
stopifnot(y == "a, b, and c", length(y) == 1)
(y <- listing(letters[1:3], style = I("sentence"), last.sep = "both"))
## [1] "1, 2, and 3"
stopifnot(y == "1, 2, and 3", length(y) == 1)
(y <- listing(letters[1:3], style = "sentence", prepend = TRUE))
## [1] "1: a, 2: b and 3: c"
stopifnot(y == "1: a, 2: b and 3: c", length(y) == 1)
(y <- listing(letters[1:3], style = I("sentence"), prepend = TRUE))
## [1] "a: 1, b: 2 and c: 3"
stopifnot(y == "a: 1, b: 2 and c: 3", length(y) == 1)
(y <- listing(letters[1:3], style = "sentence", prepend = "%s=>%s"))
## [1] "1=>a, 2=>b and 3=>c"
stopifnot(y == "1=>a, 2=>b and 3=>c", length(y) == 1)
(y <- listing(letters[1:3], style = "sentence", last.sep = "two"))
## [1] "a, b, or c"
stopifnot(y == "a, b, or c", length(y) == 1)
# with explicit sprintf template
(y <- listing(x, style = "%s, %s", collapse = "; ", prepend = "!"))
## [1] "!A, a; !B, b; !C, c; !D, d; !E, e"
stopifnot(y == "!A, a; !B, b; !C, c; !D, d; !E, e", length(y) == 1)
(y <- listing(x, style = I("%s, %s"), collapse = "; ", prepend = "!"))
## [1] "!a, A; !b, B; !c, C; !d, D; !e, E"
stopifnot(y == "!a, A; !b, B; !c, C; !d, D; !e, E", length(y) == 1)
# create m4 macro definitions
(y <- listing(x, style = "m4"))
## [1] "define(`A', ``a'')dnl\ndefine(`B', ``b'')dnl\ndefine(`C', ``c'')dnl\ndefine(`D', ``d'')dnl\ndefine(`E', ``e'')dnl"
stopifnot(grepl("^(define\\([^)]+\\)dnl\n?)+$", y), length(y) == 1)
# other 'x' arguments
stopifnot(listing(x) == listing(as.list(x)))
old.opt <- options(digits = 3)
stopifnot(listing(pi) == "1: 3.14") # controlled by getOption("digits")
options(old.opt)
## flatten()
x <- list(a = list(b = 1:5, c = letters[1:5]), d = LETTERS[1:3],
e = list(pi))
(y <- flatten(x)) # sublists removed, non-list elements kept
## $a.b
## [1] 1 2 3 4 5
##
## $a.c
## [1] "a" "b" "c" "d" "e"
##
## $d
## [1] "A" "B" "C"
##
## $e
## [1] 3.141593
stopifnot(is.list(y), length(y) == 4, !sapply(y, is.list))
# atomic objects are not modified by default
stopifnot(identical(letters, flatten(letters)))