When programming in R, one may print some messages such as usage information. When it comes, the base function strwrap() becomes very convenient. It can wrap strings into equal-length lines and add indentation/prefixes to lines. Here I will demonstrate some features.
First, let’s create a sample string by reading the first 5 lines of the ‘THANKS’ document in R documentation.
# read 5 lines from the 'THANKS' document
x <- paste(readLines(file.path(R.home("doc"), "THANKS"),n=5), collapse = "\n")
print(x)
[1] "R would not be what it is today without the invaluable help of these\npeople outside of the R core team, who contributed by donating code, bug\nfixes and documentation:\n\nValerio Aimale, Suharto Anggono, Thomas Baier, Henrik Bengtsson, Roger Bivand,"
Let’s try formatting the string to lines of 30-character long, as shown below:
writeLines(strwrap(x, width=30))
R would not be what it is
today without the invaluable
help of these people outside
of the R core team, who
contributed by donating code,
bug fixes and documentation:
Valerio Aimale, Suharto
Anggono, Thomas Baier, Henrik
Bengtsson, Roger Bivand,
Now, let’s indent the 1st line of each paragraph by using the paramter indent.
writeLines(strwrap(x, 30, indent = 5))
R would not be what it
is today without the
invaluable help of these
people outside of the R core
team, who contributed by
donating code, bug fixes and
documentation:
Valerio Aimale, Suharto
Anggono, Thomas Baier, Henrik
Bengtsson, Roger Bivand,
We can indent each line except the first line with the parameter exdent:
writeLines(strwrap(x, 30, exdent = 5))
R would not be what it is
today without the
invaluable help of these
people outside of the R
core team, who
contributed by donating
code, bug fixes and
documentation:
Valerio Aimale, Suharto
Anggono, Thomas Baier,
Henrik Bengtsson, Roger
Bivand,
Now let’s add a prefix to the start of the first paragraph using the parameter initial:
writeLines(strwrap(x, 30, initial = "THANKS: "))
THANKS: R would not be what
it is today without the
invaluable help of these
people outside of the R core
team, who contributed by
donating code, bug fixes and
documentation:
Valerio Aimale, Suharto
Anggono, Thomas Baier, Henrik
Bengtsson, Roger Bivand,
We can add prefixes to the other lines using prefix:
writeLines(strwrap(x, 30, initial = "THANKS: ", prefix = " > "))
THANKS: R would not be what
> it is today without the
> invaluable help of these
> people outside of the R
> core team, who contributed
> by donating code, bug
> fixes and documentation:
>
> Valerio Aimale, Suharto
> Anggono, Thomas Baier,
> Henrik Bengtsson, Roger
> Bivand,
All done. Very useful function.
Note:
any whitespaces in input strings are converted to spaces before formatting, but double whitespaces are treated differently (see the strwrap documentation for details).
when more than one string is given as inputs, in default the result is to concatenate the strings into one text. If one wants to keep each string’s result separate, use the parameter simplify=F.
Have R fun!
Last modified on 2019-09-26