multicolor your RMarkdown HTML documents!

library(multicolor)
multi_color(things$buffalo, 
            colors = palettes$grandbudapest, 
            type = "rmd", 
            add_leading_newline = TRUE)

[1]
 —-——–– 
%s 
 —-——–
    \
      \
        \
                   ̲.-‘‘‘‘’-,̲
         ̲,.,̲ ,-’‘           ‘’-.,̲
       /)     (                   ’‘‘-.
      ((      ) )                      ‘\
        \)    (̲/                        )\
        |       /)           ’    ,’    / \
        ‘\    ^’            ’     (    /  ))
          |      ̲/\ ,     /    ,,‘\   (  "‘
          \Y,   |   \  \  | ‘‘‘‘| / \̲ \
            ‘)̲/      \  \  )    ( >  ( >
                       \( \(     |/   |/
          mic & dwb  /̲(/̲(    /̲(  /̲(
    


with type = "rmd".


Chunk Output

multi_color(
  
  " Brutus is just as smart as Caesar, \n people totally like Brutus just as much as they like Caesar, \n and when did it become okay for one person \n to be the boss of everybody because \n that's not what Rome is about! \n We should totally just STAB CAESAR!",
  
  colors = c(wesanderson::wes_palettes$GrandBudapest2, wesanderson::wes_palettes$GrandBudapest2),
  
  type = "rmd",
  add_leading_newline = TRUE
  
)

[1]
 Brutus is just as smart as Caesar, 
 people totally like Brutus just as much as they like Caesar, 
 and when did it become okay for one person 
 to be the boss of everybody because 
 that’s not what Rome is about! 
 We should totally just STAB CAESAR!


You can keep the output monospace by wrapping a chunk in, e.g.,

<div style='font-family: Monaco; font-size = 0.8em'></div>.


Or use a knitr hook such as

knitr::knit_hooks$set(
  mc_rmd = function(before, options, envir) {
    if (before) {
       return(paste("<div style='font-family: Monaco;'>"))
    } else {
       return(paste("</div>"))
    }
  }
)

which can be specified in the chunk options with mc_rmd=TRUE.


Header Output

is also fair game,



Deets

In multi_color, setting type equal to "rmd" uses the fansi package to produce HTML.


multi_color(type = "rmd")
#> [1] <span style='color: #FF0000;'>he</span><span style='color: #FFAF00;'>ll</span><span style='color: #FFFF00;'>o&nbsp;</span><span style='color: #00FF00;'>wo</span><span style='color: #0000FF;'>rl</span><span style='color: #AF00FF;'>d!</span>


To display the colored text, the knitr option that should be applied to the chunk or document is the results = "asis" option.

You can do this document-wide with:

knitr::opts_chunk$set(results = "asis")

The chunk containing this option can be hidden with the echo = FALSE option. More on knitr options.

Another global option that should be set is:

options(crayon.enabled = TRUE)