Skip to content

This is a wrapper around stringi::stri_wrap() which implements the Knuth-Plass paragraph wrapping algorithm.

Usage

str_wrap(string, width = 80, indent = 0, exdent = 0)

Arguments

string

character vector of strings to reformat.

width

positive integer giving target line width in characters. A width less than or equal to 1 will put each word on its own line.

indent

non-negative integer giving indentation of first line in each paragraph

exdent

non-negative integer giving indentation of following lines in each paragraph

Value

A character vector of re-wrapped strings.

Examples

thanks_path <- file.path(R.home("doc"), "THANKS")
thanks <- str_c(readLines(thanks_path), collapse = "\n")
thanks <- word(thanks, 1, 3, fixed("\n\n"))
cat(str_wrap(thanks), "\n")
#> R would not be what it is today without the invaluable help of these people
#> outside of the (former and current) R Core team, who contributed by donating
#> code, bug fixes and documentation: Valerio Aimale, Suharto Anggono, Thomas
#> Baier, Gabe Becker, Henrik Bengtsson, Roger Bivand, Ben Bolker, David Brahm,
#> G"oran Brostr"om, Patrick Burns, Vince Carey, Saikat DebRoy, Matt Dowle, Brian
#> D'Urso, Lyndon Drake, Dirk Eddelbuettel, Claus Ekstrom, Sebastian Fischmeister,
#> John Fox, Paul Gilbert, Yu Gong, Gabor Grothendieck, Frank E Harrell Jr, Peter
#> M. Haverty, Torsten Hothorn, Robert King, Kjetil Kjernsmo, Roger Koenker,
#> Philippe Lambert, Jan de Leeuw, Jim Lindsey, Patrick Lindsey, Catherine Loader,
#> Gordon Maclean, Arni Magnusson, John Maindonald, David Meyer, Ei-ji Nakama,
#> Jens Oehlschl"agel, Steve Oncley, Richard O'Keefe, Hubert Palme, Roger D. Peng,
#> Jose' C. Pinheiro, Tony Plate, Anthony Rossini, Jonathan Rougier, Petr Savicky,
#> Guenther Sawitzki, Marc Schwartz, Arun Srinivasan, Detlef Steuer, Bill Simpson,
#> Gordon Smyth, Adrian Trapletti, Terry Therneau, Rolf Turner, Bill Venables,
#> Gregory R. Warnes, Andreas Weingessel, Morten Welinder, James Wettenhall, Simon
#> Wood, and Achim Zeileis. Others have written code that has been adopted by R and
#> is acknowledged in the code files, including 
cat(str_wrap(thanks, width = 40), "\n")
#> R would not be what it is today without
#> the invaluable help of these people
#> outside of the (former and current) R
#> Core team, who contributed by donating
#> code, bug fixes and documentation:
#> Valerio Aimale, Suharto Anggono, Thomas
#> Baier, Gabe Becker, Henrik Bengtsson,
#> Roger Bivand, Ben Bolker, David Brahm,
#> G"oran Brostr"om, Patrick Burns, Vince
#> Carey, Saikat DebRoy, Matt Dowle,
#> Brian D'Urso, Lyndon Drake, Dirk
#> Eddelbuettel, Claus Ekstrom, Sebastian
#> Fischmeister, John Fox, Paul Gilbert,
#> Yu Gong, Gabor Grothendieck, Frank E
#> Harrell Jr, Peter M. Haverty, Torsten
#> Hothorn, Robert King, Kjetil Kjernsmo,
#> Roger Koenker, Philippe Lambert, Jan
#> de Leeuw, Jim Lindsey, Patrick Lindsey,
#> Catherine Loader, Gordon Maclean,
#> Arni Magnusson, John Maindonald,
#> David Meyer, Ei-ji Nakama, Jens
#> Oehlschl"agel, Steve Oncley, Richard
#> O'Keefe, Hubert Palme, Roger D. Peng,
#> Jose' C. Pinheiro, Tony Plate, Anthony
#> Rossini, Jonathan Rougier, Petr Savicky,
#> Guenther Sawitzki, Marc Schwartz, Arun
#> Srinivasan, Detlef Steuer, Bill Simpson,
#> Gordon Smyth, Adrian Trapletti, Terry
#> Therneau, Rolf Turner, Bill Venables,
#> Gregory R. Warnes, Andreas Weingessel,
#> Morten Welinder, James Wettenhall, Simon
#> Wood, and Achim Zeileis. Others have
#> written code that has been adopted by R
#> and is acknowledged in the code files,
#> including 
cat(str_wrap(thanks, width = 60, indent = 2), "\n")
#>   R would not be what it is today without the invaluable
#> help of these people outside of the (former and current)
#> R Core team, who contributed by donating code, bug fixes
#> and documentation: Valerio Aimale, Suharto Anggono, Thomas
#> Baier, Gabe Becker, Henrik Bengtsson, Roger Bivand, Ben
#> Bolker, David Brahm, G"oran Brostr"om, Patrick Burns,
#> Vince Carey, Saikat DebRoy, Matt Dowle, Brian D'Urso,
#> Lyndon Drake, Dirk Eddelbuettel, Claus Ekstrom, Sebastian
#> Fischmeister, John Fox, Paul Gilbert, Yu Gong, Gabor
#> Grothendieck, Frank E Harrell Jr, Peter M. Haverty,
#> Torsten Hothorn, Robert King, Kjetil Kjernsmo, Roger
#> Koenker, Philippe Lambert, Jan de Leeuw, Jim Lindsey,
#> Patrick Lindsey, Catherine Loader, Gordon Maclean, Arni
#> Magnusson, John Maindonald, David Meyer, Ei-ji Nakama,
#> Jens Oehlschl"agel, Steve Oncley, Richard O'Keefe, Hubert
#> Palme, Roger D. Peng, Jose' C. Pinheiro, Tony Plate, Anthony
#> Rossini, Jonathan Rougier, Petr Savicky, Guenther Sawitzki,
#> Marc Schwartz, Arun Srinivasan, Detlef Steuer, Bill Simpson,
#> Gordon Smyth, Adrian Trapletti, Terry Therneau, Rolf Turner,
#> Bill Venables, Gregory R. Warnes, Andreas Weingessel, Morten
#> Welinder, James Wettenhall, Simon Wood, and Achim Zeileis.
#> Others have written code that has been adopted by R and is
#> acknowledged in the code files, including 
cat(str_wrap(thanks, width = 60, exdent = 2), "\n")
#> R would not be what it is today without the invaluable help
#>   of these people outside of the (former and current) R
#>   Core team, who contributed by donating code, bug fixes
#>   and documentation: Valerio Aimale, Suharto Anggono, Thomas
#>   Baier, Gabe Becker, Henrik Bengtsson, Roger Bivand, Ben
#>   Bolker, David Brahm, G"oran Brostr"om, Patrick Burns,
#>   Vince Carey, Saikat DebRoy, Matt Dowle, Brian D'Urso,
#>   Lyndon Drake, Dirk Eddelbuettel, Claus Ekstrom, Sebastian
#>   Fischmeister, John Fox, Paul Gilbert, Yu Gong, Gabor
#>   Grothendieck, Frank E Harrell Jr, Peter M. Haverty,
#>   Torsten Hothorn, Robert King, Kjetil Kjernsmo, Roger
#>   Koenker, Philippe Lambert, Jan de Leeuw, Jim Lindsey,
#>   Patrick Lindsey, Catherine Loader, Gordon Maclean, Arni
#>   Magnusson, John Maindonald, David Meyer, Ei-ji Nakama,
#>   Jens Oehlschl"agel, Steve Oncley, Richard O'Keefe, Hubert
#>   Palme, Roger D. Peng, Jose' C. Pinheiro, Tony Plate,
#>   Anthony Rossini, Jonathan Rougier, Petr Savicky, Guenther
#>   Sawitzki, Marc Schwartz, Arun Srinivasan, Detlef Steuer,
#>   Bill Simpson, Gordon Smyth, Adrian Trapletti, Terry
#>   Therneau, Rolf Turner, Bill Venables, Gregory R. Warnes,
#>   Andreas Weingessel, Morten Welinder, James Wettenhall,
#>   Simon Wood, and Achim Zeileis. Others have written code
#>   that has been adopted by R and is acknowledged in the code
#>   files, including 
cat(str_wrap(thanks, width = 0, exdent = 2), "\n")
#> R
#>   would
#>   not
#>   be
#>   what
#>   it
#>   is
#>   today
#>   without
#>   the
#>   invaluable
#>   help
#>   of
#>   these
#>   people
#>   outside
#>   of
#>   the
#>   (former
#>   and
#>   current)
#>   R
#>   Core
#>   team,
#>   who
#>   contributed
#>   by
#>   donating
#>   code,
#>   bug
#>   fixes
#>   and
#>   documentation:
#>   Valerio
#>   Aimale,
#>   Suharto
#>   Anggono,
#>   Thomas
#>   Baier,
#>   Gabe
#>   Becker,
#>   Henrik
#>   Bengtsson,
#>   Roger
#>   Bivand,
#>   Ben
#>   Bolker,
#>   David
#>   Brahm,
#>   G"oran
#>   Brostr"om,
#>   Patrick
#>   Burns,
#>   Vince
#>   Carey,
#>   Saikat
#>   DebRoy,
#>   Matt
#>   Dowle,
#>   Brian
#>   D'Urso,
#>   Lyndon
#>   Drake,
#>   Dirk
#>   Eddelbuettel,
#>   Claus
#>   Ekstrom,
#>   Sebastian
#>   Fischmeister,
#>   John
#>   Fox,
#>   Paul
#>   Gilbert,
#>   Yu
#>   Gong,
#>   Gabor
#>   Grothendieck,
#>   Frank
#>   E
#>   Harrell
#>   Jr,
#>   Peter
#>   M.
#>   Haverty,
#>   Torsten
#>   Hothorn,
#>   Robert
#>   King,
#>   Kjetil
#>   Kjernsmo,
#>   Roger
#>   Koenker,
#>   Philippe
#>   Lambert,
#>   Jan
#>   de
#>   Leeuw,
#>   Jim
#>   Lindsey,
#>   Patrick
#>   Lindsey,
#>   Catherine
#>   Loader,
#>   Gordon
#>   Maclean,
#>   Arni
#>   Magnusson,
#>   John
#>   Maindonald,
#>   David
#>   Meyer,
#>   Ei-
#>   ji
#>   Nakama,
#>   Jens
#>   Oehlschl"agel,
#>   Steve
#>   Oncley,
#>   Richard
#>   O'Keefe,
#>   Hubert
#>   Palme,
#>   Roger
#>   D.
#>   Peng,
#>   Jose'
#>   C.
#>   Pinheiro,
#>   Tony
#>   Plate,
#>   Anthony
#>   Rossini,
#>   Jonathan
#>   Rougier,
#>   Petr
#>   Savicky,
#>   Guenther
#>   Sawitzki,
#>   Marc
#>   Schwartz,
#>   Arun
#>   Srinivasan,
#>   Detlef
#>   Steuer,
#>   Bill
#>   Simpson,
#>   Gordon
#>   Smyth,
#>   Adrian
#>   Trapletti,
#>   Terry
#>   Therneau,
#>   Rolf
#>   Turner,
#>   Bill
#>   Venables,
#>   Gregory
#>   R.
#>   Warnes,
#>   Andreas
#>   Weingessel,
#>   Morten
#>   Welinder,
#>   James
#>   Wettenhall,
#>   Simon
#>   Wood,
#>   and
#>   Achim
#>   Zeileis.
#>   Others
#>   have
#>   written
#>   code
#>   that
#>   has
#>   been
#>   adopted
#>   by
#>   R
#>   and
#>   is
#>   acknowledged
#>   in
#>   the
#>   code
#>   files,
#>   including