append_newline_if_needed: Make sure a string ends with a newline.

View source: R/text_formatting.R

append_newline_if_neededR Documentation

Make sure a string ends with a newline.

Description

Append newlines to the end of strings.

Usage

append_newline_if_needed(
  txt,
  start_par = FALSE,
  extra_lines = 0,
  collapse = NULL
)

Arguments

txt

A vector of character strings.

start_par

Add a newline at the beginning of the string. If there is more than one string in txt, start_par should have length 1 or the length of txt.

extra_lines

The number of blank lines to follow the string. If there is more than one string in txt, extra_lines should have length 1 or the length of txt.

collapse

Collapse all strings in txt into a single string after appending newlines. This can bee logical, in which case the collapsed strings are separated by an empty string, or it can be a character string.

Details

Take a character vector, ensure that each element ends in a newline, Optionally add an extra newline to the beginning of each element, add extra blank lines at the end, and optionally concatenate the elements together into a single string, separated by an optional separator in addition to the new lines.

Value

A vector of character strings.

Examples

append_newline_if_needed("foo")
append_newline_if_needed("foo", start_par = TRUE)
append_newline_if_needed("foo", extra_lines = 3)
append_newline_if_needed(month.abb, start_par = c(TRUE, rep(FALSE, 11)),
  collapse = TRUE)
append_newline_if_needed(month.abb, collapse = "...")
append_newline_if_needed(c("Months:", month.abb, extra_lines = 0,
  collapse = "* "))


jonathan-g/semestr documentation built on Jan. 23, 2025, 3:10 p.m.