A Knitr Based Approach to R Puzzles

R
Author

Filip Reierson

Published

February 28, 2024

The R programming language is full of interesting quirks and functions that do not come up regularly in the typical analyst’s workflow. However, knowing about the less common functions and inner workings of R can nonetheless help when debugging or developing robust code. Recently I have, along with fellow researchers, been looking for ways in which R is not always intuitive. I have learned a lot from this and many laughs have ensued. Most of the code snippets we have shared with each other have been in the form of puzzles, where the puzzle taker must predict the output from a line of code or lines of code piped together.

In this post I share how to use a puzzle template I have developed for presenting these types of puzzles in the format of a revealjs presentation. The code is available from freierson/r-puzzle-template. An example of the output from my approach is shown in Figure 1, for some of the puzzles that I have come up with. I expect that others will come up with much better and creative puzzles, but these show off the functionality of the puzzle template. The step by step answers, dynamically generated based on location of base R pipes, are meant to encourage the puzzle taker to fully understand the solution, without having to start up R.

Figure 1: An example of the puzzle template applied to four puzzles.



To create a similar presentation follow the steps below.

Step 1: Download the puzzle template

Download the repository freierson/r-puzzle-template.

Step 2: Write some R code which requires a deep understanding of the language to determine its output

For example, in the following code it is not clear whether the c inside of is.function() is the vector function c or the column with the same name in the data.frame.

data.frame(
  x = 1:2,
  c = 1:2
) |>
  dplyr::filter(is.function(c)) |>
  nrow()

Separate the R into code chunks in all-puzzles.qmd.

Step 3: On the first line of each code chunk write a puzzle prompt as a comment

Note that the exact phrasing of the puzzle is up to the puzzle composer. For example:

  • What number does the following line of code return?
  • How many rows does the following line of code return?

See the below code chunks for a demonstration.

```{r}
# What number does the following line of code return?
data.frame(
  x = 1:2,
  c = 1:2
) |>
  dplyr::filter(is.function(c)) |>
  nrow()
```
```{r}
# How many rows does the following line of code return?
data.frame(
  x = 1:2,
  c = 1:2
) |>
  dplyr::filter(is.function(!!c))
```

Step 4: Render index.qmd

If the puzzles run without error the puzzle presentation should have generated. The resulting file, index.html, can be included on websites using an iframe, shown as a presentation, or shared as a pdf. To create a pdf file you may press ‘e’ when the revealjs presentation is open followed by the print command, usually Ctrl+P or Cmd+P, and selecting print to pdf.

Conclusion

Puzzles that involve predicting the output of R code can be a fun exercise for those especially interested in R programming. With this template it is not necessary to install or open R to engage with these puzzles and they can be shared easily with others.