Home > Mobile >  Avoid Words getting cut in R
Avoid Words getting cut in R

Time:12-26

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

CodePudding user response:

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"  
  • Related