weave: Weave Inlay Values into a String

View source: R/weave.R

weaveR Documentation

Weave Inlay Values into a String

Description

A reformulation of sprintf relying on a template containing escape switch sequences which specify formatting. The location of escape switch sequences specify where to insert inlay arguments and the content of escape switch sequences specify how to format arguments to be inlaid, and come in four families as shown in the table below:

  Switch   Switch Type   Sample   Text of Result
  Value   (and Formatting)   ... Arg   with Sample Arg
  '0'   decimal (0 decimal points)   pi   '3'
  '1'   decimal (1 decimal point)   pi   '3.1'
  '2'   decimal (2 decimal points)   pi   '3.14'
  '3'   decimal (3 decimal points)   pi   '3.141'
  '4'   decimal (4 decimal points)   pi   '3.1415'
  '5'   decimal (5 decimal points)   pi   '3.14159'
  '6'   decimal (6 decimal points)   pi   '3.141592'
  '7'   decimal (7 decimal points)   pi   '3.1415926'
  '8'   decimal (8 decimal points)   pi   '3.14159265'
  '9'   decimal (9 decimal points)   pi   '3.141592653'
  'q'   quote (single straight)   'x'   'x'
  'Q'   quote (double straight)   'x'   "x"
  't'   quote (single typeset)   'x'   ⁠‘x’⁠
  'T'   quote (double typeset)   'x'   ⁠“x”⁠
  'l'   list (simple list)   1:3   ⁠1, 2, 3⁠
  '|'   list (Oxford 'or')   1:3   ⁠1, 2, or 3⁠
  '&'   list (Oxford 'and')   1:3   ⁠1, 2, and 3⁠
  'a'   list (Oxford 'all of')   1:3   ⁠any of 1, 2, or 3⁠
  'A'   list (Oxford 'any of')   1:3   ⁠all of 1, 2, and 3⁠
  'e'   list (Oxford 'either/or')   1:3   ⁠either 1, 2, or 3⁠
  'n'   list (Oxford 'neither/nor')   1:3   ⁠neither 1, 2, nor 3⁠
  'c'   list (c(.) statement)   1:3   'c(1, 2, 3)'
  'b'   paren (brace-enclosed)   1   '{1}'
  'p'   paren (paren-enclosed)   2   '(2)'
  'r'   paren (round-enclosed)   2   '(2)'
  's'   paren (square-enclosed)   3   '[3]'

NOTE that 'p' and 'r' are synonyms for round parentheses.

Escape Switch Sequences

Escape switch sequences are formatted as '{@}', '{@w}', '{@wx}', '{@wxy}', or ⁠'{@wxyz}⁠ where w, x, y, and z are formatting switches from the four formatting switch families. Note that the first switch encountered from a given family is the switch that is applied. That is, if there are multiple switches from any given family, only the first is recognized and applied. Any others are ignored.

Escape switch sequences in format '{@}' mean 'insert atomic scalar ... arg as is'.

Escape switch sequences in format '{@w}' mean 'insert atomic vec ... arg after applying switch w.'

Escape switch sequences in format '{@wx}' mean 'insert atomic vec ... arg after applying switches w and x.'

Escape switch sequences in format '{@wxy}' mean 'insert atomic vec ... arg after applying switches w, x, and y.'

Escape switch sequences in format '{@wxyz}' mean 'insert atomic vec ... arg after applying switches w, x, y, and z.'

Ordering of switches

Ordering of switches within an escape switch sequence is arbitrary. Regardless of order in escape switches, any decimal switches are applied first, followed by any quote switches, followed by any list switches, followed by any paren.

Usage

weave(template, ...)

Arguments

...

Arbitrary number of unnamed atomic scalar/vector arguments to be inserted into tmp with formatting specified in inlay escape seqs. The n-th argument in ... corresponds to the n-th inlay escape seq in tmp. The number of inlay escape seqs in tmp must be equal to the number of ... arguments. See details.

tmp

A complete character scalar template with embedded escape seqs. See details.

Value

A character scalar.

See Also

Other strings: blank(), chn(), delim(), fsub(), gr, ipat(), makestr(), markdown_help(), maxnch(), ox(), ox_vals(), pgrid_help(), revstr(), spaces(), ss_help(), tocase()

Examples

egWeave <- function() {

  cat("\n\n", uj::weave('{@}', FALSE))
  cat("\n\n", uj::weave('{@}', 42))
  cat("\n\n", uj::weave('{@b}', 4:7))
  cat("\n\n", uj::weave('{@q}', 'foo::bar'))
  cat("\n\n", uj::weave('{@0}', pi))
  cat("\n\n", uj::weave('{@c2}', c(pi, exp(1), 42)))
  cat("\n\n", uj::weave('{@Q6}', pi))
  cat("\n\n", uj::weave('{@et3}', c(pi, exp(1))))
  cat("\n\n", uj::weave('{@aq}', c('me', 'myself', 'I')))
  cat("\n\n", uj::weave('{@A2tb}', c(pi, exp(1), 42)))

  template <- paste0('Once upon a time, I came across {@} {@r0} little pigs. They looked highly {@&}. ',
                     'After some discrete inquiry, I learned their names were {@T&}. I went to their ' ,
                     'houses, which were made of {@|}. I said {@t}. But ever so rudely, they replied ' ,
                     '{@Q}. So, I {@&}. Then I fried them up nicely crisp, and I ate my piggy {@9b}.' )

  cat("\n\n")

  cat(
    uj::weave(
      template,
      "three",
      pi,
      c("tasty", "well-fed", "naive"),
      c("Lasagna", "Ziti", "Linguini"),
      c("straw", "sticks", "bricks"),
      "little pig, little pig, let me come in",
      "not by the hair of my chinny chin chin",
      c("huffed", "puffed", "blew their houses down"),
      pi
    )
  )

}

egWeave()

j-martineau/uj documentation built on Sept. 14, 2024, 4:40 a.m.