Day 9 — Text, code and results in one R Markdown file
You have a script, a figure and a few numbers. The usual next step is to paste the figure into Word and type the numbers into sentences. Then one value in the data is corrected, and the document keeps the old numbers. Months later, nobody, including you, can say which version of the data produced Figure 2. An R Markdown report keeps the explanation, the code and its results in one file, and rebuilds all of them from the data each time you render it.
Today you will:
.Rmd file: header, text and code chunks;Day 10 uses this to write your first complete short report. Every report in this course is made this way.
report.Rmd is the source: your text plus R code. You edit this.report.html: text, results and figures..Rmd, the data and the HTML together..Rmd file---
title: "Seedling growth"
output: html_document
---
## Question
How much did the seedlings grow?
```{r import}
library(readr)
seedlings <- read_csv("data/seedlings.csv",
show_col_types = FALSE)
```
--- lines: title and output format.## Question is a heading.```{r} and ```. import is the chunk’s label; each label must be unique.# outside a chunk makes a heading; inside a chunk it starts an R comment.> rmarkdown::render("report.Rmd",
+ output_dir = "reports")
processing file: report.Rmd
...
Output created: reports/report.html
> list.files("reports")
[1] "report.html"
reports/report.html. Open it in a browser:The rendered report.html in a browser: title, question text, the code, and a table and plot of growth by group.
growth_mm must come after the chunk that calculates it.library() and the import go first.In the text of the .Rmd:
There are `r nrow(seedlings)` plants.
The HTML reads: There are 24 plants.
```{r growth-figure, fig.cap="Each point is
one independently potted seedling."}
ggplot(seedlings,
aes(x = treatment, y = growth_mm)) +
geom_point(position = position_jitter(
width = 0.08, height = 0, seed = 8)) +
labs(x = "Treatment",
y = "Change in seedling length (mm)")
```
library(ggplot2) to the first chunk.fig.cap becomes the caption under the figure.
The caption says what the figure shows. A sentence below it says what you see: growth varies within both groups, and the groups overlap.
> rmarkdown::render("report.Rmd")
processing file: report.Rmd
1/2
2/2 [import]
Error:
! 'data/seedling.csv' does not exist in current
working directory:
'.../introcomputing-course'.
...
Quitting from report.Rmd:10-14 [import]
Execution halted
seedling, not seedlings).Quitting from ... [import] says where: the chunk labelled import.library(), an object made only in a later chunk, two chunks with the same label..Rmd alone is not enoughdata/seedlings.csv. Someone who gets only report.Rmd cannot render it..Rproj, .Rmd, data/, output/, reports/, with the same layout.| Idea | Key fact |
|---|---|
| Why | Results come from the data at every render; no copied numbers that go stale |
| Source and output | Edit report.Rmd; report.html is a reading copy, rebuilt at each render |
| Parts | Header between ---; text; code chunks between fences, each with a unique label |
| Render | Knit, or rmarkdown::render() in the Console, never inside the report |
| Order | Chunks run top to bottom: packages, import, calculation, then plot |
| Fresh session | The report must contain every step; restart R, then render to test |
| Inline R | `r nrow(seedlings)` puts a calculated number in a sentence; typed words still need checking |
| Errors | Read the first error and its chunk label; change one thing at a time |
| Sharing | .Rmd plus data in the same project layout |
Next (Day 10, first report): write a short report that answers a biological question and that someone else can rerun.
Now: open the Day 9 exercises. Leave with report.Rmd (question, methods, labelled figure, interpretation) and its rendered HTML, rendered again after restarting R.