-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntro2_RMarkdown.Rmd
122 lines (84 loc) · 2.71 KB
/
Intro2_RMarkdown.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
title: "CodeChunks and more"
author: "Thomas Koentges"
date: "22/11/2021"
output: html_document
params:
x_axis: "price"
y_axis: "carat"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse = T)
library(tidyverse)
comma <- function(x) format(x, digits = 2, big.mark = ",")
smaller <- diamonds %>%
filter(carat <= 2.5)
```
## Code Chunks
You can insert new code chunks in 3 ways:
1. *CTRL/CMD + Alt/Option + I*
2. Click +C
3. Type opening and closing ```
## How a code chunk looks
````r
```{r chunk_name, echo=F}`r ''`
# if you read this code in the source file, this is not a code chunk!
library(dplyr)
mtcars %>%
group_by(cyl) %>%
summarize(n = n(), mean = mean(mpg))
```
````
```{r chunk_name, echo=F, eval=F}
# this would be a code chunk! But the code is neither printed (echo = F) nor executed (eval = F)
library(dplyr)
mtcars %>%
group_by(cyl) %>%
summarize(n = n(), mean = mean(mpg))
```
### How it works
```{r}
mtcars %>%
group_by(cyl) %>%
summarize(n = n(), mean = mean(mpg))
```
### Important chunk options
- `eval = FALSE`
- `include = FALSE`
- `echo = FALSE`
- `message = FALSE` or `warning = FALSE`
- `results = 'hide'` and `fig.show = 'hide'`
- `error = TRUE`
- `cache = TRUE`
## Cache example
```{r iris_data, cache=TRUE}
my_data <- iris
# filter(Species != "virginica")
```
```{r iris_plot, cache = T}
my_plot <- my_data %>%
ggplot(aes(Sepal.Width, col = Species)) +
geom_boxplot()
```
```{r iris_vis}
my_plot
```
## Inline R
You can write R code inline to calculate variables. For instance: We have data about `r nrow(diamonds)` diamonds. Only `r nrow(diamonds) - nrow(smaller)` are larger than 2.5 carats.
## Exercises
1. Make the numbers above nicer using the `comma()` function.
2. Add a section that explores how diamond sizes vary by cut, colour, and clarity. Assume you’re writing a report for someone who doesn’t know R, and instead of setting echo = FALSE on each chunk, set a global option.
3. Download diamond-sizes.Rmd from https://github.com/hadley/r4ds/tree/master/rmarkdown. Add a section that describes the largest 20 diamonds, including a table that displays their most important attributes.
4. Set up a network of chunks where d depends on c and b, and both b and c depend on a. Have each chunk print lubridate::now(), set cache = TRUE, then verify your understanding of caching.
## Other interesting things
:::: {.columns}
::: {.column width="40%"}
#### Info in 2-columns
On the right side is a very informative figure!
:::
::: {.column width="60%"}
```{r diamond-plot, fig.cap="Something useful about diamonds", fig.width=4, fig.height=3, echo=F}
ggplot(diamonds, aes_string(params$x_axis, params$y_axis)) + geom_point()
```
:::
::::