Create a GIF of code and its output


May 10, 2022

We can create a GIF of code and its output using asciicast.

Install the development version:

remotes::install_github('r-lib/asciicast', ref = remotes::github_pull(24)) 

Create a file called nzchar.R with specifications for the GIF and the code to run.

#' Title: Using nzchar
#' Columns: 60
#' Rows: 18

# This is not empty

# This is empty

# This returns TRUE

# This returns FALSE

Create another file that creates the GIF:

#' Title: Using nzchar

src <- "nzchar.R"
cast <- asciicast::record(src)

# <<
# `cast` is an `asciicast` object, which has some metadata and the
# recording itself:
# <<


# <<
# You can write `cast` to a GIF file with the version installed above.
# <<

svg <- tempfile(fileext = "gif")
asciicast::write_gif(cast, svg, theme = "monokai")

Really fun for adding to tweets!

An animation created with asciicast that shows that nzchar() returns TRUE when a string is nonempty and FALSE when a string is empty, using Sys.getenv() to query existing 'R_LIBS_USER' and nonexisting 'test' environment variables.