Parsing RMarkdown
for fun  and profit 

A brief look at the parsermd package

rundel/parsermd

 

 

 

Colin Rundel
Univ of Edinburgh / Duke Univ

bit.ly/rsg_parsermd_slides
hw01.Rmd

---
title: "Homework 01 - Hello R"
output: html_document
---

### Load packages

```{r setup, message=FALSE}
library(tidyverse)
library(datasauRus)
```


### Exercise 1

Based on the help file, how many rows and how many columns does the
`datasaurus_dozen` file have? What are the variables included in the
data frame? Add your responses to your lab report. When you’re done,
commit your changes with the commit message “Added answer for Ex 1”,
and push.

#### Solution

(Type your answer to Exercise 1 here. This exercise does not require
any R code.)

### Exercise 2

Plot `y` vs. `x` for the `dino` dataset. Then, calculate the correlation
coefficient between x and y for this dataset.

#### Solution

(The answers for this Exercise are given for you below. But you should
clean up some of the narrative so that it only includes what you want to
turn in.)

First lets plot the data in the dino dataset:

```{r plot-dino, fig.height=3, fig.width=6}
dino_data <- datasaurus_dozen %>%
  filter(dataset == "dino")

ggplot(data = dino_data, mapping = aes(x = x, y = y)) +
  geom_point()
```

And next calculate the correlation between `x` and `y` in this dataset:

```{r cor-dino}
dino_data %>%
  summarize(r = cor(x, y))
```

### Exercise 3

Plot `y` vs. `x` for the `star` dataset. You can (and should) reuse code we
introduced above, just replace the dataset name with the desired dataset. Then,
calculate the correlation coefficient between x and y for this dataset. How
does this value compare to the r of dino?

#### Solution

(Add code and narrative as needed. Note that the R chunks are labeled with
`plot-star` and `cor-star` to provide spaces to place the code for plotting
and calculating the correlation coefficient. To finish, clean up the narrative
by removing these instructions.)

```{r plot-star}

```

This is some text, you should replace me with more meaningful text...

```{r cor-star}

```

library(parsermd)
parse_rmd("hw01.Rmd")

## ├── YAML [2 lines]
## ├── Heading [h3] - Load packages
## │   └── Chunk [r, 1 opt, 2 lines] - setup
## ├── Heading [h3] - Exercise 1
## │   ├── Markdown [2 lines]
## │   └── Heading [h4] - Solution
## │       └── Markdown [2 lines]
## ├── Heading [h3] - Exercise 2
## │   ├── Markdown [2 lines]
## │   └── Heading [h4] - Solution
## │       ├── Markdown [4 lines]
## │       ├── Chunk [r, 2 opts, 5 lines] - plot-dino
## │       ├── Markdown [2 lines]
## │       └── Chunk [r, 2 lines] - cor-dino
## └── Heading [h3] - Exercise 3
##     ├── Markdown [2 lines]
##     └── Heading [h4] - Solution
##         ├── Markdown [4 lines]
##         ├── Chunk [r, 1 lines] - plot-star
##         ├── Markdown [2 lines]
##         └── Chunk [r, 1 lines] - cor-star


parse_rmd("hw01.Rmd") %>%
  as_tibble()

## # A tibble: 21 x 5
##    sec_h3        sec_h4   type          label         ast
##    <chr>         <chr>    <chr>         <chr>         <rmd_ast>
##  1 NA            NA       rmd_yaml_list NA            <yaml>
##  2 Load packages NA       rmd_heading   NA            <heading [h3]>
##  3 Load packages NA       rmd_chunk     setup         <chunk [r]>
##  4 Exercise 1    NA       rmd_heading   NA            <heading [h3]>
##  5 Exercise 1    NA       rmd_markdown  NA            <markdown [2]>
##  6 Exercise 1    Solution rmd_heading   NA            <heading [h4]>
##  7 Exercise 1    Solution rmd_markdown  NA            <markdown [2]>
##  8 Exercise 2    NA       rmd_heading   NA            <heading [h3]>
##  9 Exercise 2    NA       rmd_markdown  NA            <markdown [2]>
## 10 Exercise 2    Solution rmd_heading   NA            <heading [h4]>
## # … with 11 more rows

AST WTF?!?!


parse_rmd("hw01.Rmd") %>%
  rmd_subset(sec_refs = c("Exercise *", "Solution"), keep_setup = TRUE)

## ├── YAML [2 lines]
## ├── Chunk [r, 1 opt, 2 lines] - setup
## ├── Heading [h3] - Exercise 1
## │   └── Heading [h4] - Solution
## │       └── Markdown [3 lines]
## ├── Heading [h3] - Exercise 2
## │   └── Heading [h4] - Solution
## │       ├── Markdown [6 lines]
## │       ├── Chunk [r, 2 opts, 5 lines] - plot-dino
## │       ├── Markdown [2 lines]
## │       └── Chunk [r, 2 lines] - cor-dino
## └── Heading [h3] - Exercise 3
##     └── Heading [h4] - Solution
##         ├── Markdown [5 lines]
##         ├── Chunk [r, 1 lines] - plot-star
##         ├── Markdown [2 lines]
##         └── Chunk [r, 1 lines] - cor-star

parse_rmd("hw01.Rmd") %>%
  rmd_subset(sec_refs = c("Exercise *", "Solution"), keep_setup = TRUE) %>%
  rmd_set_options(error = TRUE)

## ├── YAML [2 lines]
## ├── Chunk [r, 2 opts, 2 lines] - setup
## ├── Heading [h3] - Exercise 1
## │   └── Heading [h4] - Solution
## │       └── Markdown [3 lines]
## ├── Heading [h3] - Exercise 2
## │   └── Heading [h4] - Solution
## │       ├── Markdown [6 lines]
## │       ├── Chunk [r, 3 opts, 5 lines] - plot-dino
## │       ├── Markdown [2 lines]
## │       └── Chunk [r, 1 opt, 2 lines] - cor-dino
## └── Heading [h3] - Exercise 3
##     └── Heading [h4] - Solution
##         ├── Markdown [5 lines]
##         ├── Chunk [r, 1 opt, 1 lines] - plot-star
##         ├── Markdown [2 lines]
##         └── Chunk [r, 1 opt, 1 lines] - cor-star


parse_rmd("hw01.Rmd") %>%
  rmd_subset(sec_refs = c("Exercise *", "Solution"), keep_setup = TRUE) %>%
  rmd_set_options(error = TRUE) %>%
  as_document(collapse = "\n") %>%
  cat()

## ---
## title: Homework 01 - Hello R
## output: html_document
## ---
##
## ```{r setup, message = FALSE, error = TRUE}
## library(tidyverse)
## library(datasauRus)
## ```
##
## ### Exercise 1
##
## #### Solution
##
## (Type your answer to Exercise 1 here. This exercise does not require
## any R code.)
##
##
## ### Exercise 2
##
## #### Solution
##
## (The answers for this Exercise are given for you below. But you should
## clean up some of the narrative so that it only includes what you want to
## turn in.)
##
## First lets plot the data in the dino dataset:
##
##
## ```{r plot-dino, fig.height = 3, fig.width = 6, error = TRUE}
## dino_data <- datasaurus_dozen %>%
##   filter(dataset == "dino")
##
## ggplot(data = dino_data, mapping = aes(x = x, y = y)) +
##   geom_point()
## ```
##
## And next calculate the correlation between `x` and `y` in this dataset:
##
##
## ```{r cor-dino, error = TRUE}
## dino_data %>%
##   summarize(r = cor(x, y))
## ```
##
## ### Exercise 3
##
## #### Solution
##
## (Add code and narrative as needed. Note that the R chunks are labeled with
## `plot-star` and `cor-star` to provide spaces to place the code for plotting
## and calculating the correlation coefficient. To finish, clean up the narrative
## by removing these instructions.)
##
##
## ```{r plot-star, error = TRUE}
##
## ```
##
## This is some text, you should replace me with more meaningful text...
##
##
## ```{r cor-star, error = TRUE}
##
## ```

parse_rmd("hw01-student.Rmd") %>%
  rmd_subset(sec_refs = c("Exercise *", "Solution"), keep_setup = TRUE) %>%
  rmd_set_options(error = TRUE) %>%
  render(name="hw01_sol")

## processing file: hw01_sol.Rmd
...
## Output created: hw01_sol.html

In practice


parse_rmd("hw01.Rmd") %>%
  rmd_subset(sec_refs = c("Exercise *", "Solution")) %>%
  rmd_subset(sec_refs = c("Exercise 3", "Solution"), type_refs = "rmd_markdown", exclude = TRUE) %>%
  rmd_template()

## # A tibble: 8 x 4
##   sec_h3     sec_h4   type          label
##   <chr>      <chr>    <chr>         <chr>
## 1 Exercise 1 Solution rmd_markdown  NA
## 2 Exercise 2 Solution rmd_markdown  NA
## 3 Exercise 2 Solution rmd_chunk     plot-dino
## 4 Exercise 2 Solution rmd_markdown  NA
## 5 Exercise 2 Solution rmd_chunk     cor-dino
## 6 Exercise 3 Solution rmd_chunk     plot-star
## 7 Exercise 3 Solution rmd_chunk     cor-star

parse_rmd("hw01.Rmd") %>%
  rmd_subset(sec_refs = c("Exercise *", "Solution")) %>%
  rmd_subset(sec_refs = c("Exercise 3", "Solution"), type_refs = "rmd_markdown", exclude = TRUE) %>%
  rmd_template() %>%
  rmd_check_template("hw01-student.Rmd", .)

## x The following required elements were missing in the document:
##   ● Section "Exercise 1" > "Solution" is missing required markdown text.
##   ● Section "Exercise 2" > "Solution" is missing a required code chunk named 'cor-dino'.

Feedback given to students via GitHub Actions

Example repo and workflows rundel/hw01

Thank you!



rundel/parsermd
rundel
rundel@gmail.com
bit.ly/rsg_parsermd (repo)
bit.ly/rsg_parsermd_slides (slides)