Reused code-chunks in Rmarkdown not working with magrittr pipe

Viewed 88

The following is based on content within The R Markdown Cookbook which suggests that named code chunks can be re-used.

The following minimal (albeit contrived) example works as expected:

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library (magrittr)
```

```{r filterCyl, include=FALSE, eval=FALSE}
dplyr::filter(cyl == 6L)
```

```{r filterGear, include=FALSE, eval=FALSE}
dplyr::filter(gear == 4L)
```

```{r, eval=FALSE}
mtcars %>%
  <<filterCyl>>
```

However, replacing the lower-most block with:

```{r, eval=FALSE}
mtcars %>%
  <<filterCyl>> %>%
  <<filterGear>>
```

...results in the following being shown in the resulting HTML:

mtcars %>%  
  <<filterCyl>> %>%  
  dplyr::filter(gear == 4L)  

The <<filterCyl>> block is not being substituted in as I'd expect.

If I remove the %>% operators from that final block, it does get subsituted in; however, the resulting R code isn't valid.

Does this appear to be a bug? Is there some workaround that could be used here?

0 Answers
Related