Title: | Human-Friendly Text from Unknown Strings |
---|---|
Description: | Simple functions for joining strings. Construct human-friendly messages whose elements aren't known in advance, like in stop, warning, or message, from clean code. |
Authors: | James Dunham [aut, cre] |
Maintainer: | James Dunham <[email protected]> |
License: | GPL (>= 3.2) |
Version: | 1.0.0 |
Built: | 2025-02-28 06:02:27 UTC |
Source: | https://github.com/jamesdunham/concatenate |
cc
collapses text into a comma-separated list (the colloquial kind of
list). cc_or
and cc_and
insert "or
" and "or
"
before the last element.
cc(...) ## S4 method for signature 'data.frame' cc(...) cc_or(..., oxford = FALSE) cc_and(..., oxford = FALSE)
cc(...) ## S4 method for signature 'data.frame' cc(...) cc_or(..., oxford = FALSE) cc_and(..., oxford = FALSE)
... |
Character vectors or a |
oxford |
Whether to use the Oxford comma. |
The data.frame
method is dispatched when the first argument in
...
is a data.frame
. It operates row-wise. If there are
subsequent arguments to cc
they are be ignored.
A length-one character vector in which each element in ...
is
separated by a comma (and a space).
cn
for cc
with (grammatical) number awareness
(like ngettext
) and substitution (like sprintf
)
cc("hello", "world") a <- "one thing" b <- "another" cc_or(a, b) a <- "this" b <- c("that", "the other") cc_and(a, b)
cc("hello", "world") a <- "one thing" b <- "another" cc_or(a, b) a <- "this" b <- c("that", "the other") cc_and(a, b)
cn
combines grammatical number awareness as in ngettext
with sprintf
-like substitution for comma-concatenated text.
cn(object, singular, plural = singular) cn_and(object, singular, plural = singular) cn_or(object, singular, plural = singular) ## S4 method for signature 'data.frame' cn(object, singular, plural = singular) ## S4 method for signature 'data.frame' cn_and(object, singular, plural = singular) ## S4 method for signature 'data.frame' cn_or(object, singular, plural = singular)
cn(object, singular, plural = singular) cn_and(object, singular, plural = singular) cn_or(object, singular, plural = singular) ## S4 method for signature 'data.frame' cn(object, singular, plural = singular) ## S4 method for signature 'data.frame' cn_and(object, singular, plural = singular) ## S4 method for signature 'data.frame' cn_or(object, singular, plural = singular)
object |
An n-vector, or |
singular |
The string to return if n = 1. |
plural |
The string to return if n is in 0, 2, 3, 4, ... |
Like ngettext
, this function returns one string to be used with a
singular referent and another with a plural referent. cn
chooses
between the two based on the length of its first argument, object
, or
if object
is a data.frame
, its row count.
Two substitions are made sprintf
-style. "%n
" is replaced with
the number of object
, and "%c
" is replaced with the
comma-concatenated values of object
, as in cc
.
cn_and
uses cc_and
instead of cc
; cn_or
uses cc_or
.
Each function in concatenate returns a comma-separated string. (A length-one
character vector.) They can be used to construct human-friendly messages
whose elements aren't known in advance, like calls to message
,
warning
or stop
, from clean code.
The workhorse function is cc
. cn
combines it with
grammatical number awareness, as in ngettext
, and
sprintf
-like substitution.
%c%
:binary infix operator for strings.
% c %
:like %c%
but with a space between its
inputs.
%,%
, %or%
, %and%
:x %c% y x % c % y x %,% y x %or% y x %and% y
x %c% y x % c % y x %,% y x %or% y x %and% y
x , y
|
Character vectors. |
v <- "important value" v %c% "!" message("Two" % c % "words")
v <- "important value" v %c% "!" message("Two" % c % "words")