Add Multiple Colors to your Console & RMarkdown Output

Add multiple colors to text that is printed to the console.


Travis buildstatus AppVeyor buildstatus Coveragestatus CRAN_Status_Badge rstudio mirrordownloads

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.

News

multicolor 0.1.3

  • An RMarkdown vignette was added
  • Support for multicolored cowsay animals in RMarkdown was added
  • Several different palettes for the logo on startup
  • Out-of-the-box palette options are exported
  • Added examples of reshaping text functions to README

multicolor 0.1.2

  • Support for multicolor in RMarkdown documents is added with type = "rmd"
  • Bug fix for overflow of the last color of a line when no color is applied to the next line (#12)
  • A colorful logo is messaged on startup

multicolor 0.1.1

  • crawl() allows for applying multi_color with pauses in between characters or lines
    • Added @cortinah as package author
  • The recycle_chars parameter allows for the option of applying each color to a single character or to an equal-sized portion of the input txt

multicolor 0.1.0

  • First version of multicolor!

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("multicolor")

0.1.3 by Amanda Dobbyn, 8 months ago


http://github.com/aedobbyn/multicolor/


Report a bug at https://github.com/aedobbyn/multicolor/issues/


Browse source code at https://github.com/cran/multicolor


Authors: Amanda Dobbyn [aut, cre] , Hernando Cortina [aut]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports cowsay, crayon, dplyr, glue, magrittr, purrr, stringi, stringr, tibble, tidyr

Suggests covr, fansi, knitr, rmarkdown, testthat, viridisLite, wesanderson


Imported by sinx.

Suggested by cowsay.


See at CRAN