operator_dollar: C-Style Formatting with sprintf as a Binary Operator

Description Usage Arguments Details Value Author(s) See Also Examples

Description

Provides access to base R's sprintf in form of a binary operator in a way similar to Python's % overloaded for strings.

Usage

1
2
3
e1 %s$% e2

e1 %stri$% e2

Arguments

e1

format strings, see sprintf for syntax

e2

a list of atomic vectors to be passed to sprintf or a single atomic vector

Details

Vectorized over e1 and e2.

e1 %s$% atomic_vector is equivalent to e1 %s$% list(atomic_vector).

Note that sprintf takes field width in bytes, not Unicode code points. See Examples for a workaround.

Value

Returns a character vector

Author(s)

Marek Gagolewski and other contributors

See Also

The official online manual of stringi at https://stringi.gagolewski.com/

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
"value='%d'" %s$% 3
"value='%d'" %s$% 1:3
"%s='%d'" %s$% list("value", 3)
"%s='%d'" %s$% list("value", 1:3)
"%s='%d'" %s$% list(c("a", "b", "c"), 1)
"%s='%d'" %s$% list(c("a", "b", "c"), 1:3)

# sprintf field width:
x <- c("abcd", "\u00DF\u00B5\U0001F970", "abcdef")
cat(sprintf("%s%6s%s", "-", x, "-"), sep="\n")
cat(sprintf("%s%s%s", "-", stringi::stri_pad(x, 6), "-"), sep="\n")

stringi documentation built on May 17, 2021, 9:07 a.m.