Slidecraft 101: Using asciicast

Using sass interpolation, functions and mixin for the most in your style sheets

slidecraft 101
quarto
Published

July 1, 2024

Hello and welcome back to my multi-part series about what I like to call slidecrafting; The art of putting together slides that are functional and aesthetically pleasing. I will be using quarto presentations. This is the eighth post, you can find all other posts in the slidecraft 101 project.

This post celebrates the asciicast R package to showcase rich output.

Without asciicast

When R code produces messages with colors or styling, they are typically lost when we write slides

qmd

Default asciicast

This is where the amazing asciicast R package comes into play. {asciicast} takes an R script and turns it into an asciinema cast. We will use the very convenient init_knitr_engine() function to make this as easy as possible.

We can add the {asciicast} {knitr} engine by placing the following chunk at the beginning of our document.

```{r}
asciicast::init_knitr_engine()
```

And then we toggle asciicast output by replacing

```{r}
library(tidymodels)
```

with

```{asciicast}
library(tidymodels)
```

With this change, we get the following output, nicely styled with colors and everything.

qmd

Note

The output of asciicast will by default fill the screen horizontally, but for some reason when setting format: revealjs: self-contained: true it doesn’t. All examples use this option for a better blogging experience. You just have to trust me that it looks better without the option.

Using startup to seed, load some packages

The full list of arguments to asciicast_init_knitr_engine() is as follows

formals(asciicast::init_knitr_engine)
$echo
[1] FALSE

$same_process
[1] TRUE

$timeout
[1] 10

$startup
NULL

$record_env
NULL

$echo_input
[1] TRUE

$interactive
[1] TRUE

I will talk about the 3 I use, which are same_process, echo, and echo_input. The two go together. same_process which defaults to TRUE, does what it says. It determines whether all the asciicast chunks should be in the same process or not. If you want to show things that only happen once a session, you might want to turn this off which is done this way

```{r}
asciicast::init_knitr_engine(
  same_process = FALSE
)
```

and gives the following results

qmd

You will notice that the code that is being run is included in the asciicast output. This is happening because the default arguments echo = FALSE and echo_input = TRUE. By swapping these options we get the code as normal quarto code, with the output as asciicast

```{r}
asciicast::init_knitr_engine(
  echo = TRUE,
  echo_input = FALSE
)
```

qmd

It wouldn’t make much sense to set both of these to TRUE as you would have duplication, but there are certainly cases where you want both of these to be FALSE as you just want the output.

Using theme argument

The output is styled a certain way, and we can change it. At the time of writing, the following 10 premade themes are available.

  • asciinema
  • tango
  • solarized-dark
  • solarized-light
  • seti
  • monokai
  • github-light
  • github-dark
  • pkgdown
  • readme

We can toggle any of these by setting the option asciicast_theme. The below example showcases the "solarized-light" theme.

```{r}
options(asciicast_theme = "solarized-light")
```

qmd

If none of these is what you need, you can use a completely custom theme. I suggest that you modify one of the existing theme

For this example, the github-light theme was modified by changing the background to pure white to match the background of the slide, thus having it blend into the slide.

```{r}
options(
  asciicast_theme = list(
    black         = c(grDevices::col2rgb("#073642")),
    red           = c(grDevices::col2rgb("#dc322f")),
    green         = c(grDevices::col2rgb("#859900")),
    yellow        = c(grDevices::col2rgb("#b58900")),
    blue          = c(grDevices::col2rgb("#268bd2")),
    magenta       = c(grDevices::col2rgb("#d33682")),
    cyan          = c(grDevices::col2rgb("#2aa198")),
    white         = c(grDevices::col2rgb("#eee8d5")),
    light_black   = c(grDevices::col2rgb("#002b36")),
    light_red     = c(grDevices::col2rgb("#cb4b16")),
    light_green   = c(grDevices::col2rgb("#586e75")),
    light_yellow  = c(grDevices::col2rgb("#657c83")),
    light_blue    = c(grDevices::col2rgb("#839496")),
    light_magenta = c(grDevices::col2rgb("#6c71c4")),
    light_cyan    = c(grDevices::col2rgb("#93a1a1")),
    light_white   = c(grDevices::col2rgb("#fdf6e3")),
    background    = c(grDevices::col2rgb("#ffffff")),
    cursor        = c(grDevices::col2rgb("#657b83")),
    bold          = c(grDevices::col2rgb("#657b83")),
    text          = c(grDevices::col2rgb("#657b83"))
  )
)
```

qmd

Roundup

I just recently learned about {asciicast} and I love it. If you know of any other cool tools or packages, please reach out and share them with me!