manu p
manu p

Reputation: 985

Avoid Words getting cut in R

I use the below code to split the sentences into many parts, but I see some issue here, a single word is getting into second line which looks wierd on the front end. Can we avoid this?

library(shiny)
HTML(paste0(gsub("(.{10})", "\\1\n", "I saw a beautiful moon tonight")))
I saw a be
autiful mo
on tonight

Expected Output (This should be dynamic. So the code should self identify meaningful words)

I saw a 
beautiful
moon tonight

Upvotes: 1

Views: 114

Answers (1)

r2evans
r2evans

Reputation: 160447

You can use the built-in R function strwrap:

strwrap("I saw a beautiful moon tonight", width=10)
# [1] "I saw a"   "beautiful" "moon"      "tonight"  

Upvotes: 1

Related Questions