Make prettier documents by reusing chunks in RMarkdown

No revelations here, just a little R tip for generating more readable documents.

Screenshot-RStudio.png

Original with lots of code at the top

There are times when I want to show code in a document, but I don’t want it to be the first thing that people see. What I want to see first is the output from that code. In this silly example, I want the reader to focus their attention on the result of myFunction(), which is 49.

---
title: "Testing chunk reuse"
author: "Neil Saunders"
date: "24/02/2015"
output: html_document
---

## Introduction
Here is my very interesting document. But first, let me show you my long and ugly R function.

```{r chunk 1}
# it's not really long and ugly
# it just squares the input
# but imagine that it is long and ugly

myFunction <- function(x) {
  print(x ^ 2)
}

myFunction(7)
```

Screenshot-RStudio-1.png

Function use before definition = error

I could define myFunction() later in the document but of course that leads to an error when the function is called before it has been defined.

---
title: "Testing chunk reuse"
author: "Neil Saunders"
date: "24/02/2015"
output: html_document
---

## Introduction
Here is my very interesting document.

```{r chunk1}
myFunction(7)
```

## This is chunk 2
My long and ugly R function is now down here.

```{r chunk2}
# it's not really long and ugly
# it just squares the input
# but imagine that it is long and ugly

myFunction <- function(x) {
  print(x ^ 2)
}
```

Solution: use the chunk option ref.label to call chunk 2 from chunk 1. You can also use echo=FALSE to hide chunk1 in the final document, but still see the code (in chunk 2) and its output.

---
title: "Testing chunk reuse"
author: "Neil Saunders"
date: "24/02/2015"
output: html_document
---

## Introduction
Here is my very interesting document.

Chunk 1 is calling chunk 2 here, but you can't see it.
```{r chunk1, ref.label="chunk2", echo=FALSE}
```

## This chunk is unnamed but can now use code from chunk 2
```{r}
myFunction(7)
```

## This is chunk 2
My long and ugly R function is now down here.

```{r chunk2}
# it's not really long and ugly
# it just squares the input
# but imagine that it is long and ugly

myFunction <- function(x) {
  print(x ^ 2)
}
```

Screenshot-RStudio-2.png

The result of calling chunk2 from chunk1

And here’s the result.

8 thoughts on “Make prettier documents by reusing chunks in RMarkdown

  1. Pingback: What you’re doing is rather desperate… |

  2. Pingback: Distilled News | Data Analytics & R

    1. nsaunders Post author

      Good to know! Thanks. But the problem here is how to “show always”, but not where you would normally need to declare the code.

  3. cotsapas

    Out of curiosity, how do you feel about RMarkdown vs Knitr/Sweave? Comes up in the group over and over – my own preference is Sweave, but that’s a function of exposure rather than assessment.

    1. nsaunders Post author

      I like them both! Sweave when you need the precise formatting and layout of LaTeX: tables, figure placement, equations, a nice PDF report. Rmarkdown + knitr when you want speed, when HTML will do. The nice thing is that pandoc can generate multiple formats from the markdown.

      That said, there’s an increasing number of layout tools (packages, CSS styles, mathjax) which make knitr/markdown documents almost as customisable, but I think Sweave/LaTeX still has the edge for complete control.

Comments are closed.