Search code examples
rtextwidth

How to adjust text width in R?


Is there a function that does this?

input:

text <- "I have a little dog"

desired output:

some_function(text, max_width = 8)

#output: "I have a\nlittle dog"

some_function(text, max_width = 15)
  
#output: "I have a little\ndog"

Solution

  • stringr::str_wrap does this :

    text <- "I have a little dog"
    stringr::str_wrap(text, 15)
    #[1] "I have a little\ndog"
    

    Output may vary in certain cases because of the underlying algorithm it uses.

    stringr::str_wrap(text, 8)
    [1] "I have\na little\ndog"