Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign up| --- | |
| output: rmarkdown::html_vignette | |
| vignette: > | |
| %\VignetteIndexEntry{rmd} | |
| %\VignetteEngine{knitr::rmarkdown} | |
| %\VignetteEncoding{UTF-8} | |
| --- | |
| ```{r setup, include=FALSE} | |
| library(multicolor) | |
| options(crayon.enabled=TRUE) | |
| knitr::opts_chunk$set( | |
| collapse = TRUE, | |
| comment = "#>", | |
| echo = TRUE | |
| ) | |
| knitr::knit_hooks$set( | |
| mc_rmd = function(before, options, envir) { | |
| if (before) { | |
| return(paste("<div style='font-family: Monaco;'>")) | |
| } else { | |
| return(paste("</div>")) | |
| } | |
| } | |
| ) | |
| ``` | |
| # `r multicolor::multi_color('multicolor', type = "rmd")` your RMarkdown HTML documents! | |
| ```{r, results='asis'} | |
| library(multicolor) | |
| ``` | |
| ```{r, results='asis', mc_rmd=TRUE} | |
| multi_color(things$buffalo, | |
| colors = palettes$grandbudapest, | |
| type = "rmd", | |
| add_leading_newline = TRUE) | |
| ``` | |
| <br> | |
| with `type = "rmd"`. | |
| <br> | |
| ## Chunk Output | |
| ```{r, results="asis", mc_rmd=TRUE} | |
| 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 | |
| ) | |
| ``` | |
| <br> | |
| You can keep the output monospace by wrapping a chunk in, e.g., | |
| `<div style='font-family: Monaco; font-size = 0.8em'></div>`. | |
| <br> | |
| Or use a [`knitr` hook](https://yihui.name/knitr/hooks/) such as | |
| ```{r, eval=FALSE} | |
| 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`. | |
| <br> | |
| ## `r multi_color("Header Output", colors = wesanderson::wes_palettes$Darjeeling1, type = "rmd", add_leading_newline = FALSE)` | |
| is also fair game, | |
| * `r multi_color("as well as", colors = wesanderson::wes_palettes$Darjeeling1, type = "rmd", add_leading_newline = FALSE)` | |
| * `r multi_color("anything else you might want", colors = wesanderson::wes_palettes$Darjeeling1, type = "rmd", add_leading_newline = FALSE)` | |
| <br> | |
| <br> | |
| ## Deets | |
| In `multi_color`, setting `type` equal to `"rmd"` uses the [`fansi`](https://github.com/brodieG/fansi) package to produce HTML. | |
| <br> | |
| ```{r message=FALSE} | |
| multi_color(type = "rmd") | |
| ``` | |
| <br> | |
| 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: | |
| ```{r eval=FALSE} | |
| knitr::opts_chunk$set(results = "asis") | |
| ``` | |
| The chunk containing this option can be hidden with the `echo = FALSE` option. More on [`knitr` options](https://yihui.name/knitr/options/). | |
| Another global option that should be set is: | |
| ```{r, eval=FALSE} | |
| options(crayon.enabled = TRUE) | |
| ``` | |
| <br> |