jeromyanglim / learning_r

assorted notes to self while learning R
1 stars 0 forks source link

How to automatically break a caption over multiple lines using R? #5

Closed jeromyanglim closed 12 years ago

jeromyanglim commented 12 years ago

I have a set of survey items which I want to show as titles to individual graphs. I want the titles to automatically wrap over multiple lines as required.

What is a good strategy for implementing a form of word wrapping in R?

jeromyanglim commented 12 years ago

This thread on word wrapping on R Help assisted

Take this example string:

x <- 'This is a long sentence that needs to be broken over multiple lines'

Then apply the following command

paste0(strwrap(x, width=30), collapse='\n')

It produces:

"This is a long sentence that\nneeds to be broken over\nmultiple lines"