/
extra-exercises.qmd
137 lines (106 loc) · 4.38 KB
/
extra-exercises.qmd
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Extra exercises
## Creating automatic analysis pipelines
### Exercise: Update function to also calculate median and IQR
> Time: \~8 minutes.
Let's make a change to our function and test out how the
`tar_outdated()` and `tar_visnetwork()` work.
1. Open up the `R/functions.R` file.
2. Add median and interquartile range (IQR) to the `summarise()`
function, by adding it to the end of `list(mean = mean, sd = sd)`,
after the second `sd`. Note, IQR should look like `iqr = IQR` since
we want the output columns to have a lowercase for the column names.
3. Run `tar_outdated()` and `tar_visnetwork()` in the Console (or by
using the Command Palette {{< var keybind.palette >}}, then "targets
outdated" or "targets visual"). What does it show?
4. Style using {{< var keybind.styler >}}. You might need to force a
reformat if the code is too long by highlighting the line and using
{{< var keybind.palette >}}, then "reformat".
5. Run `tar_make()` in the Console or with
{{< var keybind.targets_make >}}. Re-check for outdated targets and
visualize the network again.
6. Open up the Git interface and commit the changes to the Git history
with {{< var keybind.git >}}.
```{r solution-new-function-descriptive-stats}
#| code-fold: true
#| code-summary: "**Click for a potential solution**. Only click if you are struggling or are out of time."
#' Calculate descriptive statistics of each metabolite.
#'
#' @param data Lipidomics dataset.
#'
#' @return A data.frame/tibble.
#'
descriptive_stats <- function(data) {
data %>%
dplyr::group_by(metabolite) %>%
dplyr::summarise(dplyr::across(value, list(
mean = mean,
sd = sd,
median = median,
iqr = IQR
))) %>%
dplyr::mutate(dplyr::across(tidyselect::where(is.numeric), ~round(.x, digits = 1)))
}
```
## A general approach to doing statistical analyses
### Exercise: How would you define a linear regression with parsnip?
> Time: \~10 minutes.
Using [parsnip's "Examples"
vignette](https://parsnip.tidymodels.org/articles/Examples.html) as well
as the code we wrote for the logistic regression above as a template,
write `{parsnip}` code that would define a simple (an engine of`"lm"`)
linear regression model. Begin by making a new Markdown header and code
chunk at the bottom of the `doc/learning.qmd` file, like listed below:
````
## Exercises
### Linear regression model definition
```{{r}}
```
````
After writing the code, style the file using {{< var keybind.styler >}}.
We will eventually delete these exercise text in the Quarto file, but
for now, commit the changes to the Git history with
{{< var keybind.git >}}.
```{r solution-define-linear-reg-model}
#| eval: false
#| code-fold: true
#| code-summary: "**Click for the solution**. Only click if you are struggling or are out of time."
linear_reg_specs <- linear_reg() %>%
set_engine("lm")
```
## Building a website with Quarto
### Exercise: Look through the new website folder
> Time: \~5 minutes.
In your main project folder, there should now be a folder called
`_site/`. Look through the folder. What files are now created? Open up
the `index.html` file in your browser while in RStudio by clicking the
`index.html` file and then selecting the "Open in web browser".
<!-- TODO: Confirm the name of the website folder. -->
Recall how we described what a website is. Can you see how these files
and folders are simply HTML files that the browser shows as website? In
the browser URL, change the `index.html` ending link to
`doc/report.html`. See how it changes to the file based on how it is
structured in the folder?
### Exercise: Make figures prettier
> Time: \~25 minutes
Looking through the [Cell
Reference](https://quarto.org/docs/reference/cells/cells-knitr.html)
documentation, complete these tasks.
- Add a `fig-cap` option to each of the figures.
- For the distribution plot, set the code chunk option `column` to
`page` to make it bigger.
Look through the `{ggplot2}` documentation on `theme_set()` and
`theme()` to update the look of all your figures. In the `setup` code
chunk of the `doc/learning.qmd` file, make a new theme object and use
`theme_set()` to apply that theme to all plots. Use this as a scaffold:
```{{r setup}}
# ... previous code
updated_theme <- theme(
# ... make changes
)
# Or start with a template like theme_classic()
updated_theme <- theme_TEMPLATE() +
theme(
# ...
)
theme_set(updated_theme)
```