[R] Word-Wrapper Library/Package?

Leonard Mada |eo@m@d@ @end|ng |rom @yon|c@eu
Thu Sep 30 04:37:42 CEST 2021


Many thanks for the hint.


The function is actually a wrapper for:

stringi:::stri_wrap

I will need to have a closer look to this one (documentation and maybe 
also peek into the code):

ret <- .Call(C_stri_wrap, str, width, cost_exponent, indent,
         exdent, prefix, initial, whitespace_only, use_length,
         locale)


I thought at some point about the stringi package, but somehow 
overlooked it.


Sincerely,


Leonard


On 9/30/2021 1:09 AM, CALUM POLWART wrote:
> Have you looked at stringr::str_wrap or its parent function 
> stringi::stri_wrap ?
>
> It applies an algorithm for the wrap. But it doesn't vectorise the 
> lines they are returned with \n for new lines, but you could apply a 
> string split to that result...
>
>
> On 29 Sep 2021 04:57, Andrew Simmons <akwsimmo using gmail.com> wrote:
>
>     'strwrap' should wrap at the target column, so I think it's behaving
>     correctly. You could do + 1 if you're expecting it to wrap
>     immediately
>     after the target column.
>
>     As far as splitting while trying to minimize a penalty, I don't think
>     strwrap can do that, and I don't know of any packages that do such
>     a thing.
>     If such a thing exists in another language, there's probably an R
>     package
>     with a similar name containing ports of such functions, that might
>     be your
>     best bet. I hope this helps.
>
>     On Tue, Sep 28, 2021, 23:51 Leonard Mada <leo.mada using syonic.eu> wrote:
>
>     > 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>
>     > 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
>     >> # 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 mailing list -- To UNSUBSCRIBE and more, see
>     >> https://stat.ethz.ch/mailman/listinfo/r-help
>     >> PLEASE do read the posting guide
>     >> http://www.R-project.org/posting-guide.html
>     >> and provide commented, minimal, self-contained, reproducible code.
>     >>
>     >
>
>     [[alternative HTML version deleted]]
>
>     ______________________________________________
>     R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>     https://stat.ethz.ch/mailman/listinfo/r-help
>     PLEASE do read the posting guide
>     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