R/xW.R

Defines functions xW

Documented in xW

xW <- function(x, w=90, indent=5) {

  n.lines <- (nchar(x) + (w-1)) %/% w

  spc <- paste(rep(" ", indent), collapse = "")

  b <- gregexpr(" ", x, fixed=TRUE)[[1]]

  y <- ""
  stop <- 0
  for (i in 1:n.lines) {
    start <- stop + 1
    if (i < n.lines) {
      k <- 1
      while (!any(b >= i*w-k)) k <- k+1 
      stop <- b[which(b>=(i*w-k))[1]]
    }
    else
      stop <- nchar(x)
    y <- paste(y, substr(x, start, stop), sep="")
    if (i < n.lines) y <- paste(y, "\n", spc, sep="") 
  }

  return(y)
}

Try the lessR package in your browser

Any scripts or data that you put into this service are public.

lessR documentation built on Nov. 12, 2023, 1:08 a.m.