Skip to content

Instantly share code, notes, and snippets.

@AdeelK93
Last active August 24, 2018 12:25
Show Gist options
  • Save AdeelK93/6b9b8fe70fd59e479a3e00f83dc814e2 to your computer and use it in GitHub Desktop.
Save AdeelK93/6b9b8fe70fd59e479a3e00f83dc814e2 to your computer and use it in GitHub Desktop.
rainbowText <- function(text, colorFun = grDevices::rainbow, ...) {
text <- trimws(text)
# colorspace has some good fucntions that you can use here
colors <- colorFun(nchar(text), ...)
# wrap each letter in a differently colored span
html <- paste0(
Map(function(letter, color)
paste0("<span style='color:", color, "'>", letter, "</span>"),
strsplit(text, "")[[1]], colors
), collapse = ""
)
# create a tempfile and view in RStudio
tf <- tempfile(fileext = ".html")
writeLines(html, tf)
rstudioapi::viewer(tf)
}
rainbowText("hello world")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment