stringr
stringr copied to clipboard
Make `str_wrap()` insert e.g. `<br>` instead of `\n`?
Would it be worthwhile to have an argument in str_wrap that lets you choose other separators than \n? I usually use str_wrap() %>% str_replace_all("\n", "<br>") when working with {ggtext}, which provides simple Markdown and HTML rendering for ggplot2:
library(tidyverse)
library(ggtext)
long_label <-
"The series centers on the various incarnations of Link, a courageous young Hylian man, with pointy, elf-like ears; and Princess Zelda, a magical princess that is the mortal reincarnation of the goddess Hylia; as they fight to save the magical land of Hyrule from Ganon, an evil warlord turned demon king, who is the principal antagonist of the series."
lab_dat <- tibble(label = str_wrap(long_label, 50) %>% str_replace_all("\n", "<br>"),
mpg = 20,
cyl = 6)
ggplot(mtcars, aes(mpg, cyl, label = label)) +
geom_richtext(data = lab_dat)

Created on 2022-07-25 by the reprex package (v2.0.1)