views:

332

answers:

3

Here is a function I wrote to break a long string into lines not longer than a given length

strBreakInLines <- function(s, breakAt=90, prepend="") {
  words <- unlist(strsplit(s, " "))
  if (length(words)<2) return(s)
  wordLen <- unlist(Map(nchar, words))
  lineLen <- wordLen[1]
  res <- words[1]
  lineBreak <- paste("\n", prepend, sep="")
  for (i in 2:length(words)) {
    lineLen <- lineLen+wordLen[i]
    if (lineLen < breakAt) 
      res <- paste(res, words[i], sep=" ")
    else {
      res <- paste(res, words[i], sep=lineBreak)
      lineLen <- 0
    }
  }
  return(res)
}

It works for the problem I had; but I wonder if I can learn something here. Is there a shorter or more efficient solution, especially can I get rid of the for loop?

+3  A: 

How about this:

gsub('(.{1,90})(\\s|$)', '\\1\n', s)

It will break string "s" into lines with maximum 90 chars (excluding the line break character "\n", but including inter-word spaces), unless there is a word itself exceeding 90 chars, then that word itself will occupy a whole line.

By the way, your function seems broken --- you should replace

lineLen <- 0

with

lineLen <- wordLen[i]
xiechao
Great Solution! I feel I need to learn about regular expressions, Thanks for pointing out the mistake in my function, too.
Karsten W.
+1  A: 

You can look at e.g. the write.dcf() FUNCTION in R itself; it also uses a loop so nothing to be ashamed of here.

The first goal is to get it right --- see Chambers (2008).

Dirk Eddelbuettel
Inspecting write.dcf (and then formatDL) brought up the function strwrap which does exactly what my function tries to do.
Karsten W.
Perfect -- I knew there was something but I didn't immediately find it. I needed this once for CRANberries as well...
Dirk Eddelbuettel
A: 

CSS, Here is webmaster example in comments last

http://fun2mobo.awardspace.biz

Works I.e. n Opera and others too check pls

Sunny