R-Ladies styled code gifs with xaringan and flipbookr

The code behind my @WeAreRLadies curating week

Shannon Pileggi
03-08-2021

TL; DR

I curated the @WeAreRLadies twitter account from Feb 15 - 20, 2021. During my curation, I shared brief R code gifs created with xaringan, flipbookr, a bit of custom CSS, and the ScreenToGif recorder. Full code is available in the rladies-flipbookr repo; here is an example gif:

Walrus operator gif. You can read more about this topic at https://www.tidyverse.org/blog/2020/02/glue-strings-and-tidy-eval/.

Figure 1: Walrus operator gif. You can read more about this topic at https://www.tidyverse.org/blog/2020/02/glue-strings-and-tidy-eval/.

Getting started

This material was developed using:

Software / package Version
R 4.0.3
RStudio 1.4.1103
tidyverse 1.3.0
xaringan 0.19
flipbookr 0.1.0

Creating slides

The flipbookr template makes it easy to quickly create content! I used the classic flipbook, with defaults to walk through the code pipeline (slides 3-4 in the template).

To create a flipbook slide that advances code line by line, create a named R code chunk with the include = FALSE option, and then present the code chunk with flipbookr::chunk_reveal. The order of defining the chunk versus revealing the chunk does not matter in your RMarkdown document. Longer content was broken into multiple code chunks.

`r chunk_reveal("walrus", title = "## Walrus operator")`

```{r walrus, include = FALSE}
library(tidyverse)
library(rlang)
library(glue)

suffix <- "A" 

dat <- tibble(x = 1:5) %>%
  mutate(
    "x_{suffix}" := x
  ) 

dat %>% 
  dplyr::select(
    glue::glue("x_{suffix}")
    ) 
```

Styling slides

I first styled the content used Alison Hill’s R-Ladies xaringan theme by declaring R-Ladies CSS options in the yaml parameters:

css: ["default", "rladies", "rladies-fonts"]

resulting in

Flipbookr walrus operator content styled with R-Ladies xaringan theme.

Figure 2: Flipbookr walrus operator content styled with R-Ladies xaringan theme.

However, the highlighting shade was a tad too dark for me, and I was concerned that the font size would be too small to be legible in a gif on twitter. I updated the CSS with bits lifted from xaringan templates and some modifications on font size and color kindly developed by Silvia Canelón.

Flipbookr walrus operator content styled with R-Ladies xaringan theme adjusted with lighter highlight shade and larger font size.

Figure 3: Flipbookr walrus operator content styled with R-Ladies xaringan theme adjusted with lighter highlight shade and larger font size.

The changes are subtle, but I hope they improve readability! The increased font size did create a challenge for code formatting and for the amount of code that could be shown on a single slide.

Silvia showed me how to modify the CSS through (1) inclusion of a CSS code chunk, and (2) calling a separate CSS sheet. For option (2), change the yaml to point to your custom CSS:

css: ["default", "default-fonts", "css/rladies-adjusted.css"]

You can see the full code for the default xaringan theme and the two ways of adjusting the CSS for all the gifs I created in the shannonpileggi/rladies-flipbookr github repository.

If you have configured a GitHub personal access token, you can fork and clone this repo with:

usethis::create_from_github("shannonpileggi/rladies-flipbookr")

Recording the gif

I recorded the gifs by manually advancing the slides with the screen recorder ScreenToGif (for Windows OS). Bryan Shalloway has some code to automatically create gifs from xaringan slides, but I didn’t go that route this time - maybe next!

Code gifs shared

Here are the R code gifs that I shared on twitter during my @WeAreRLadies curating week:

  1. walrus operator

  2. mutate_at + vars + matches

  3. mutate + across + matches

  4. haven + gtsummary

  5. named color vectors + scale_color_manual

Other options

During my curating week, I learned about the carbonate package! This also seems like a fantastic option for aesthetically appealing code sharing.

Accessibility

Visual code sharing through gifs and images can be a great way to share content! However, please remember to share the content with everyone. Gifs and images on their own are not accessible to members of our community that use assistive technology to read digital content. In order to make gifs and images accessible to screen readers, you can include descriptions or share code in otherwise accessible formats like GitHub gists. If I miss making any of my content accessible, please let me know!

Acknowledgements

I have to give Silvia Canelón (@spcanelon) a huge shout out here. My curating week started on a Monday, and on Monday night I was still trying to figure out how I wanted to style my code. We chatted on R-Ladies slack, and she quickly helped me make CSS adjustments to the default xaringan R-Ladies theme. After the curating week, Silvia helped me fine tune the bits to share that are now in the rladies-flipbookr repo. Additionally, she advises me on inclusivity and accessibility best practices, which I am still striving to meet. Thank you, Silvia! 💜 🙏

Lastly, thanks to the friendly RStudio Community forum for helping me figure out both the in line verbatim I used in this post and the dplyr::select() portion of the walrus operator gif.

Outtakes

I experimented with, and actually recorded, some alternative formats before I landed on flipbookr. Here are two gifs that didn’t make the cut! 🙈

Walrus operator gif recorded as a scrolling RMarkdown document.

Figure 4: Walrus operator gif recorded as a scrolling RMarkdown document.

Walrus operator gif recorded as code submitted to console.

Figure 5: Walrus operator gif recorded as code submitted to console.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Pileggi (2021, March 8). PIPING HOT DATA: R-Ladies styled code gifs with xaringan and flipbookr. Retrieved from https://www.pipinghotdata.com/posts/2021-03-08-r-ladies-styled-code-gifs-with-xaringan-and-flipbookr/

BibTeX citation

@misc{pileggi2021r-ladies,
  author = {Pileggi, Shannon},
  title = {PIPING HOT DATA: R-Ladies styled code gifs with xaringan and flipbookr},
  url = {https://www.pipinghotdata.com/posts/2021-03-08-r-ladies-styled-code-gifs-with-xaringan-and-flipbookr/},
  year = {2021}
}