[R] Word-Wrapper Library/Package?

Leonard Mada |eo@m@d@ @end|ng |rom @yon|c@eu
Wed Sep 29 05:51:11 CEST 2021


Thank you Andrew.


I will explore this function more, although I am struggling to get it to 
work properly:

strwrap("Abc. B. Defg", 7)
# [1] "Abc." "B."   "Defg"

# both "Abc. B." and "B. Defg" are 7 characters long.

strwrap(paste0(rep("ab", 7), collapse=""), 7)
# [1] "ababababababab"


Can I set an absolute maximum width?

It would be nice to have an algorithm that computes a penalty for the 
split and selects the split with the smallest penalty (when no obvious 
split is possible).


Sincerely,


Leonard



On 9/29/2021 6:30 AM, Andrew Simmons wrote:
> I think what you're looking for is 'strwrap', it's in package base.
>
> On Tue, Sep 28, 2021, 22:26 Leonard Mada via R-help 
> <r-help using r-project.org <mailto:r-help using r-project.org>> wrote:
>
>     Dear R-Users,
>
>
>     Does anyone know any package or library that implements functions for
>     word wrapping?
>
>
>     I did implement a very rudimentary one (Github link below), but would
>     like to avoid to reinvent the wheel. Considering that
>     word-wrapping is a
>     very common task, it should be available even in base R (e.g. in a
>     "format" module/package).
>
>
>     Sincerely,
>
>
>     Leonard
>
>     =======
>
>     The latest versions of the functions are on Github:
>
>     https://github.com/discoleo/R/blob/master/Stat/Tools.CRAN.R
>     <https://github.com/discoleo/R/blob/master/Stat/Tools.CRAN.R>
>     # Note:
>     # - the function implementing word wrapping: split.N.line(...);
>     # - for the example below: the functions defined in Tools.CRAN.R are
>     required;
>
>
>     Examples:
>     ### Search CRAN
>     library(pkgsearch)
>
>     searchCran = function(s, from=1, len=60, len.print=20, extend="*",
>              sep=" ", sep.h="-") {
>          if( ! is.null(extend)) s = paste0(s, extend);
>          x = advanced_search(s, size=len, from=from);
>          if(length(x$package_data) == 0) {
>              cat("No packages found!", sep="\n");
>          } else {
>              scroll.pkg(x, len=len.print, sep=sep, sep.h=sep.h);
>          }
>          invisible(x)
>     }
>
>     # with nice formatting & printing:
>     x = searchCran("text", from=60, sep.h="-")
>
>     scroll.pkg(x, start=20, len=21, sep.h = "-*")
>     # test of sep.h=NULL vs ...
>
>
>     Notes:
>
>     1.) split.N.line:
>
>     - was implemented to output a pre-specified number of lines (kind of
>     "maxLines"), but this is not required from an actual word-wrapper;
>
>     - it was an initial design decision when implementing the
>     format.lines()
>     function; but I plan to implement a 1-pass exact algorithm during the
>     next few days anyway;
>
>     2.) Refactoring
>
>     - I will also move the formatting code to a new file: probably
>     Tools.Formatting.R;
>
>     - the same applies for the formatting code for ftable (currently
>     in file
>     Tools.Data.R);
>
>     3.) Package gridtext
>
>     - seems to have some word-wrapping functionality, but does not
>     seem to
>     expose it;
>
>     - I am also currently focused on character-based word wrapping
>     (e.g. for
>     RConsole);
>
>
>
>             [[alternative HTML version deleted]]
>
>     ______________________________________________
>     R-help using r-project.org <mailto:R-help using r-project.org> mailing list --
>     To UNSUBSCRIBE and more, see
>     https://stat.ethz.ch/mailman/listinfo/r-help
>     <https://stat.ethz.ch/mailman/listinfo/r-help>
>     PLEASE do read the posting guide
>     http://www.R-project.org/posting-guide.html
>     <http://www.R-project.org/posting-guide.html>
>     and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list