Hi Michael, It looks like you're having fun, I hope I don't spoil it for you:
title <- "some variety of words that are descriptive" paste(strwrap(title, width=16), collapse="\n") -Ista On Fri, Jan 22, 2010 at 9:07 PM, Michael Pearmain <mpearm...@google.com> wrote: > Hi All, > > I'm trying to write a function to automatically split long strings so they > will appear nicely in a chart i'm trying to create, > > Say i have a string > > title <- "some variety of words that are descriptive" > > In this instance i want to place carriage return where there is a space just > prior to a specified number of characters (in this case 15) > > title.length <- nchar(title) > no.splits <- floor(title.length / 15) > space.title <- c(gregexpr("[[:space:]]", title)[[1]]) > > space.title # This tells me the position of all spaces in the title > [1] 5 13 16 22 27 31 >> no.splits # This tells me how many carriage returns i will need > [1] 2 >> title.length # this tells me teh total length of the title string > [1] 42 > > I can then check to see where the last value is for each string i.e. where i > should make the break with (no.splits * characters (i.e 15) > 15 < space.title ##(15 * 1 split) > [1] FALSE FALSE TRUE TRUE TRUE TRUE >> 30 < space.title ## (15 *2 splits) > [1] FALSE FALSE FALSE FALSE FALSE TRUE >> > > (I'm guessing i need to create some loop or apply here) > > So i know i need to do a sub at positions 13 and 27 of "" to "\n" > > So my final output would appear as > title <- "some variety\nof words that are\ndescriptive" > > But i'm getting stuck as to find a way to work out the the positions 13, 27 > dynamically and returning them to use later > Can anyone offer any advise? > > Thanks All. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > 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. > -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org ______________________________________________ R-help@r-project.org mailing list 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.