Make prettier documents by reusing chunks in RMarkdown

February 23, 2015
By

[This article was first published on What You're Doing Is Rather Desperate » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

Filed under: programming, R, statistics Tagged: how to, knitr, rmarkdown, rstats

To leave a comment for the author, please follow the link and comment on their blog: What You're Doing Is Rather Desperate » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)