Core wrapping functionality that preserve white spaces. Only "\n"
is not supported
by core functionality stringi::stri_wrap()
. This is usually solved before hand by
matrix_form()
. If the width is smaller than any large word, these will be truncated
after width
characters. If the split leaves trailing groups of empty spaces,
they will be dropped.
Arguments
- str
character. String to be wrapped. If it is a character vector or a list, it will be looped as a list and returned with
unlist(use.names = FALSE)
.- width
numeric(1). Width, in characters, that the text should be wrapped at.
- collapse
character(1) or
NULL
. If the words that have been split should be pasted together with the collapse character. This is usually done internally with"\n"
to have the wrapping updated along with other internal values.
Value
A string if str
is one element and if collapse = NULL
. Otherwise, is
a list of elements (if length(str) > 1
) that can contain strings or vector of
characters (if collapse = NULL
).
Details
Word wrapping happens as with stringi::stri_wrap()
with the following exception: individual words which are longer
than max_width
are broken up in a way that fits with the rest of the
word wrapping.
Functions
wrap_txt()
: function that flattens the list of wrapped strings withunist(str, use.names = FALSE)
. This is deprecated, usewrap_string()
instead.
Examples
str <- list(
" , something really \\tnot very good", # \t needs to be escaped
" but I keep it12 "
)
wrap_string(str, 5, collapse = "\n")
#> [1] " ,\nsomet\nhing\nreall\ny \\t\nnot \nvery\ngood"
#> [2] " \nbut I\nkeep\nit12"
wrap_txt(str, 5, collapse = NULL)
#> [1] " ," "somet" "hing" "reall" "y \\t" "not " "very" "good"
#> [9] " " "but I" "keep" "it12"