Last active
August 24, 2018 12:25
-
-
Save AdeelK93/6b9b8fe70fd59e479a3e00f83dc814e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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