1#' Wrap strings into nicely formatted paragraphs. 2#' 3#' This is a wrapper around [stringi::stri_wrap()] which implements 4#' the Knuth-Plass paragraph wrapping algorithm. 5#' 6#' @param string character vector of strings to reformat. 7#' @param width positive integer giving target line width in characters. A 8#' width less than or equal to 1 will put each word on its own line. 9#' @param indent non-negative integer giving indentation of first line in 10#' each paragraph 11#' @param exdent non-negative integer giving indentation of following lines in 12#' each paragraph 13#' @return A character vector of re-wrapped strings. 14#' @export 15#' @examples 16#' thanks_path <- file.path(R.home("doc"), "THANKS") 17#' thanks <- str_c(readLines(thanks_path), collapse = "\n") 18#' thanks <- word(thanks, 1, 3, fixed("\n\n")) 19#' cat(str_wrap(thanks), "\n") 20#' cat(str_wrap(thanks, width = 40), "\n") 21#' cat(str_wrap(thanks, width = 60, indent = 2), "\n") 22#' cat(str_wrap(thanks, width = 60, exdent = 2), "\n") 23#' cat(str_wrap(thanks, width = 0, exdent = 2), "\n") 24str_wrap <- function(string, width = 80, indent = 0, exdent = 0) { 25 if (width <= 0) width <- 1 26 27 out <- stri_wrap(string, width = width, indent = indent, exdent = exdent, 28 simplify = FALSE) 29 vapply(out, str_c, collapse = "\n", character(1)) 30} 31