Top Banner
Literate Programming in R Markdown David A. Selby Department of Statistics, University of Warwick 27 September 2016 David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 1 / 30
34

Literate Programming in R Markdown

Jan 30, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Literate Programming in R Markdown

Literate Programming in R Markdown

David A. Selby

Department of Statistics, University of Warwick

27 September 2016

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 1 / 30

Page 2: Literate Programming in R Markdown

1 Literate Programming

2 Markdown

3 R Markdown

4 Lazy, productive research

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 2 / 30

Page 3: Literate Programming in R Markdown

Literate Programming

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 3 / 30

Page 4: Literate Programming in R Markdown

Motivation

1 Literate programming helps peers understand and replicate your results,find errors and suggest enhancements

2 “Literate programming produces better-quality programs” — DonaldKnuth

3 Literate programming saves time and effort, so you can spend moretime:

• doing real research• in the pub

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 4 / 30

Page 5: Literate Programming in R Markdown

Motivation

1 Literate programming helps peers understand and replicate your results,find errors and suggest enhancements

2 “Literate programming produces better-quality programs” — DonaldKnuth

3 Literate programming saves time and effort, so you can spend moretime:

• doing real research• in the pub

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 4 / 30

Page 6: Literate Programming in R Markdown

Motivation

1 Literate programming helps peers understand and replicate your results,find errors and suggest enhancements

2 “Literate programming produces better-quality programs” — DonaldKnuth

3 Literate programming saves time and effort, so you can spend moretime:

• doing real research• in the pub

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 4 / 30

Page 7: Literate Programming in R Markdown

Motivation

1 Literate programming helps peers understand and replicate your results,find errors and suggest enhancements

2 “Literate programming produces better-quality programs” — DonaldKnuth

3 Literate programming saves time and effort, so you can spend moretime:

• doing real research

• in the pub

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 4 / 30

Page 8: Literate Programming in R Markdown

Motivation

1 Literate programming helps peers understand and replicate your results,find errors and suggest enhancements

2 “Literate programming produces better-quality programs” — DonaldKnuth

3 Literate programming saves time and effort, so you can spend moretime:

• doing real research• in the pub

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 4 / 30

Page 9: Literate Programming in R Markdown

Effective communication

“If you can’t write clearly, you probably don’t think nearly as wellas you think you do.” — Kurt Vonnegut

“If it was hard to write, it should be hard to read.”— Computer programmers’ proverb

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 5 / 30

Page 10: Literate Programming in R Markdown

Commenting code

What does this code do?

data(women)plot(women)fit <- lm(weight ~ height, data = women)abline(fit)

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 6 / 30

Page 11: Literate Programming in R Markdown

Commenting code

With comments:

# Analysis of the 'women' dataset in Rdata(women) # Load the dataplot(weight~height, data = women) # Make a scatter plotfit <- lm(weight ~ height, data = women) # Fit linear modelabline(fit) # Add a line of best fit to the plot

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 7 / 30

Page 12: Literate Programming in R Markdown

Literate Programming

“Let us change our traditional attitude to the construction ofprograms: Instead of imagining that our main task is to instruct acomputer what to do, let us concentrate rather on explaining tohumans what we want the computer to do.”— Donald Knuth

Who will read your code?

1 Your supervisor2 Collaborators3 Reviewers4 Future you

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 8 / 30

Page 13: Literate Programming in R Markdown

The World Almanac and Book of Facts (1975) includes a dataset of heights (in)and weights (lbs) of 15 American women aged 30–39. It is built into R:

data(women)

As height increases, weight appears to increases (almost) linearly: every inch inheight adds approximately 3.45 lbs. This was determined by fitting a simple linearregression model of weight against height:

fit <- lm(weight ~ height, data = women)

The resulting least-squares regression line can be drawn on a scatter plot of heightagainst weight. The fit looks quite good. . .

plot(weight~height, data = women)abline(fit)

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 9 / 30

Page 14: Literate Programming in R Markdown

58 60 62 64 66 68 70 72

120

130

140

150

160

height

wei

ght

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 10 / 30

Page 15: Literate Programming in R Markdown

Markdown

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 11 / 30

Page 16: Literate Programming in R Markdown

Markdown syntax

Here is some text in *italics*, in **bold** and `teletype`.

Here is a new paragraph, a [link](www.google.com) and animage:![Wally](wally.jpg)

* These are* bullet points

> "To be, or not to be, that is the question."^[*Hamlet*, Act III, Scene I]

1. And this is1. a numbered7. list

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 12 / 30

Page 17: Literate Programming in R Markdown

Markdown output

Here is some text in italics, in bold and teletype.Here is a new paragraph, a link and an image:

• These are• bullet points

“To be, or not to be, that is the question.” 1

1 And this is2 a numbered3 list

1Hamlet, Act III, Scene IDavid A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 13 / 30

Page 18: Literate Programming in R Markdown

Markdown tables

| Left | Centre | Right || ----------------- |:--------------:| ------:|| You can | This text is | 42 || use **Markdown** | centre-aligned | 314 || *within* tables | | 37 |

Output

Left Centre Right

You can This text is 42use Markdown centre-aligned 314within tables 37

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 14 / 30

Page 19: Literate Programming in R Markdown

Markdown code chunks

To investigate the relationship between `height` and `weight`,we fitted a *simple linear regression model*, as follows.

```rmodel <- lm(weight ~ height, data = women)summary(model)plot(model) # Residual diagnostics```

OutputTo investigate the relationship between height and weight, we fitted asimple linear regression model, as follows.

model <- lm(weight ~ height, data = women)summary(model)plot(model) # Residual diagnostics

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 15 / 30

Page 20: Literate Programming in R Markdown

YAML headers

---title: "The name of my Markdown document"author: "David A. Selby"date: "27 September 2016"output: pdf_document---

(content)

YAML (yet another markup language) headers let you specify additionaloptions before rendering your document

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 16 / 30

Page 21: Literate Programming in R Markdown

Markdown: so what?

So far, Markdown is just a lightweight typesetting program.How will this help you become more productive?Introducing R Markdown. . .

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 17 / 30

Page 22: Literate Programming in R Markdown

R Markdown

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 18 / 30

Page 23: Literate Programming in R Markdown

R Markdown

An ordinary Markdown code chunk:

```ryour R code goes here```

An R Markdown R code chunk:

```{r}your R code goes here```

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 19 / 30

Page 24: Literate Programming in R Markdown

R Markdown

You can run R in-line with text as well. To add in-line R code, we use thesyntax ‘r your_code_here‘. This will evaluate and return the resultwithin the paragraph. For example:

If we multiply 13 and 56 we get `r 13 * 56`.The date today is `r format(Sys.Date(), "%d %B %Y")`.There are `r nrow(iris)` observations in the iris data set.

OutputIf we multiply 13 and 56 we get 728.The date today is 27 September 2016.There are 150 observations in the iris data set.

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 20 / 30

Page 25: Literate Programming in R Markdown

I heard you like code chunks. . .

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 21 / 30

Page 26: Literate Programming in R Markdown

Re-using code chunks

Yo dawg, check out this *cool* plot:

```{r chunk1, echo = FALSE}image(volcano, col = terrain.colors(20), labels = NULL)```

Here is the code we used to make it!

```{r chunk2}```{r chunk1, eval = FALSE}```

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 22 / 30

Page 27: Literate Programming in R Markdown

Re-using code chunks (output)

Yo dawg, check out this cool plot:

0.0 0.2 0.4 0.6 0.8 1.0

0.0

0.2

0.4

0.6

0.8

1.0

Here is the code we used to make it!

image(volcano, col = terrain.colors(20), labels = NULL)

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 23 / 30

Page 28: Literate Programming in R Markdown

Other programming languages2A Python code chunk

```{python}x = ['To', 'be', 'or', 'not', 'to', 'be']y = [i.upper() for i in x]print(" ".join(y) + 5 * '?!')```

Output

x = ['To', 'be', 'or', 'not', 'to', 'be']y = [i.upper() for i in x]print(" ".join(y) + 5 * '?!')

## TO BE OR NOT TO BE?!?!?!?!?!

2Assuming they are installed and on your PATHDavid A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 24 / 30

Page 29: Literate Programming in R Markdown

Lazy, productive research

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 25 / 30

Page 30: Literate Programming in R Markdown

Nobody need ever know!

• knitr::kable or xtable::xtable to auto-generate tables• echo = FALSE to hide code in output• cache = TRUE to save results that take a long time to run• output: word_document to generate .docx files• Set a bibliography in YAML, then cite:

e.g. “As found by [@fisher1931]. . . ”

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 26 / 30

Page 31: Literate Programming in R Markdown

Another thing R Markdown is great for

Presentations!

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 27 / 30

Page 32: Literate Programming in R Markdown

Outreach in R Markdown

Jekyll transforms Markdown into static websites and blogsGitHub Pages serves and hosts Jekyll web sites for freeknitr-jekyll Automatically knits R Markdown documents, builds them

with Jekyll and serves them locally

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 28 / 30

Page 33: Literate Programming in R Markdown

Write your entire thesis in R Markdown

http://www.bookdown.org

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 29 / 30

Page 34: Literate Programming in R Markdown

Links & further reading

Literate Programming Donald Knuth (1992)R Markdown http://rmarkdown.rstudio.com

knitr http://yihui.name/knitrR Markdown reference guide and cheat sheet

https://www.rstudio.com/resources/cheatsheets/

David A. Selby (Statistics) Literate Programming in R Markdown 27 September 2016 30 / 30