ansi_collapse {cli} | R Documentation |
Collapse a vector into a string scalar
Description
Features:
custom separator (
sep
),custom separator for length-two input (
sep2
),custom last separator (
last
),adds ellipsis to truncated strings,
uses Unicode ellipsis character on UTF-8 console,
can collapse "from both ends", with
style = "both-ends"
,can consider a limit for the display width of the result, in characters,
handles ANSI control sequences correctly when measuring display width.
Usage
ansi_collapse(
x,
sep = ", ",
sep2 = " and ",
last = ", and ",
trunc = Inf,
width = Inf,
ellipsis = symbol$ellipsis,
style = c("both-ends", "head")
)
Arguments
x |
Character vector, or an object with an |
sep |
Separator. A character string. |
sep2 |
Separator for the special case that |
last |
Last separator, if there is no truncation. E.g. use
|
trunc |
Maximum number of elements to show. For |
width |
Limit for the display width of the result, in characters.
This is a hard limit, and the output will never exceed it.
This argument is not implemented for the |
ellipsis |
Character string to use at the place of the truncation. By default, the Unicode ellipsis character is used if the console is UTF-8, and three dots otherwise. |
style |
Truncation style:
|
Value
Character scalar. It is NA_character_
if any elements in x
are NA
.
See Also
glue_collapse
in the glue package inspired this function.
Examples
ansi_collapse(letters)
# truncate
ansi_collapse(letters, trunc = 5)
# head style
ansi_collapse(letters, trunc = 5, style = "head")