combine_words: Combine multiple words into a single string

View source: R/utils.R

combine_wordsR Documentation

Combine multiple words into a single string

Description

When a value from an inline R expression is a character vector of multiple elements, we may want to combine them into a phrase like ‘⁠a and b⁠’, or a, b, and c. That is what this a helper function does.

Usage

combine_words(
  words,
  sep = ", ",
  and = " and ",
  before = "",
  after = before,
  oxford_comma = TRUE
)

Arguments

words

A character vector.

sep

Separator to be inserted between words.

and

Character string to be prepended to the last word.

before, after

A character string to be added before/after each word.

oxford_comma

Whether to insert the separator between the last two elements in the list.

Details

If the length of the input words is smaller than or equal to 1, words is returned. When words is of length 2, the first word and second word are combined using the and string, or if blank, sep if is used. When the length is greater than 2, sep is used to separate all words, and the and string is prepended to the last word.

Value

A character string marked by xfun::raw_string().

Examples

combine_words("a")
combine_words(c("a", "b"))
combine_words(c("a", "b", "c"))
combine_words(c("a", "b", "c"), sep = " / ", and = "")
combine_words(c("a", "b", "c"), and = "")
combine_words(c("a", "b", "c"), before = "\"", after = "\"")
combine_words(c("a", "b", "c"), before = "\"", after = "\"", oxford_comma = FALSE)

yihui/knitr documentation built on Nov. 24, 2024, 5:10 a.m.