Skip to content

Add multiple colors to your R console and RMarkdown output 🎨

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

aedobbyn/multicolor

Repository files navigation

Travis build status AppVeyor build status Coverage status CRAN_Status_Badge rstudio mirror downloads

multicolor 🎨 egret

Apply multiple colors to your messages, warnings, and errors. Built on the crayon package. Pairs nicely with cowsay. The blog post has a bit of backstory and walkthrough of how it works.

✨ New ✨: multicolor 0.1.2 now supports RMarkdown with type = "rmd". See package vignette vignette("rmd", "multicolor") for examples.

whale


Installation

Stable, from CRAN:

install.packages("multicolor")

or the development version from GitHub:

devtools::install_github("aedobbyn/multicolor")

Note: colors cannot be applied in the R GUI and certain other environments. RStudio or any terminal should work fine1. RMarkdown is also in play 👍.


Usage

library(multicolor)

Supply a character vector of colors to colors. This defaults to "rainbow", i.e., c("red", "orange", "yellow", "green", "blue", "purple").

multi_color("Hello world")

hello_world

If you want to unveil your creation slowly (as @cortinah’s whale gif above), use crawl. You can vary the speed with the pause argument.

chicken


Options

The text supplied will be divided into even(ish) chunks of those colors when recycle_chars is false. If it’s true, each color will apply to a single character, and the colors vector will be recycled over the length of the input string supplied.

Any character vector of R colors or hex values are fair game.

hello_world

The default direction is vertical, but horizontal is also an option by setting the direction param to “horizontal”.

The default type messages the result. If you want the bare string back with color encodings attached, use type = "string", which looks like:

multi_color("Why are avocado pits so big?",
  type = "string")

avocado_q

So you can ask important questions ☝️and answer them colorfully 👇:

multi_color("The wild avocado grows in subtropical jungles, so the new sprout has to get several feet tall before it can share sunlight (to make food) with its neighbors. Until it grows out of their shadows, it relies on nutrients in the seed, so it'd better be big.",
            sample(colors(), 
                   sample(10, 1)))

avocado_a


ASCII art with cowsay

All cowsay animals are exported in multicolor::things, but to get the animals to speak, you need cowsay.

library(cowsay)

say(what = "holygrail", 
    by = "yoda",
    what_color = "olivedrab",
    by_color = colors()[which(grepl("green", colors()))])

yoda

Error in style:

my_error <- multi_color("An unknown error has occurred.", 
                        type = "string")

stop(my_error)

error

And with character:

my_msg <- 
  say(what = "Error: something went horribly wrong",
    by = "rms",
    what_color = viridisLite::magma(5)[3],
    by_color = viridisLite::magma(10),
    type = "string")

e <- simpleError(my_msg)
tryCatch(log("foo"), error = function(e) message(my_msg))

rms

Or just send messages to your users that they’ll want to read.

this_variable <- "foo"
this_option <- "bar"

say(what = 
      glue::glue("Aha, I see you set {this_variable} to {this_option}. Excellent choice."),
    by = "owl",
    what_color = c("seagreen3", "turquoise3", "seagreen3"),
    by_color = c("turquoise3", "seagreen3", "turquoise3"))

foo_to_bar


Reshaping Text

multicolor includes functions to reshape text strings to create visually interesting designs. center_string centers text strings within the console, while triangle_string can create upward and downward pointing triangles of varying width. These can be combined with multi_color to create visuals such as these:

triangle_string(ipsum, step = 4, maxlen = 11, display = TRUE) %>%
    center_string() %>%
    multi_color(direction = "horizontal", viridis::plasma(n = 6, direction = -1, begin = 0.3))

triangle_up

triangle_string(ipsum, step = -4, maxlen = 56, display = TRUE) %>%
    center_string() %>%
    multi_color(direction = "horizontal", viridis::inferno(n = 8, direction = -1, begin = 0.3))

triangle_down


That’s it! PRs & bug reports v welcome. 🎨


1 If coloring isn’t possible, you’ll get a warning on load and every time multi_color evaluates. The type argument will auto-set to "string". To check how these environments are determined, see multicolor:::use_color. If using multicolor in another package, you might consider inserting a replacement for the case when this evaluates to FALSE.

About

Add multiple colors to your R console and RMarkdown output 🎨

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages