-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.4-Processing_lists.qmd
executable file
·162 lines (121 loc) · 3.14 KB
/
4.4-Processing_lists.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
---
title: "Working with Lists of Plots"
author:
- Elizabeth King
- Kevin Middleton
format:
revealjs:
theme: [default, custom.scss]
standalone: true
self-contained: true
logo: QMLS_Logo.png
slide-number: true
show-slide-number: all
link-external-newwindow: true
---
## What else can we do?
```{r}
#| label: setup
#| echo: false
#| warning: false
#| message: false
library(tidyverse)
library(cowplot)
ggplot2::theme_set(theme_cowplot())
library(patchwork)
```
- Anything else you want in the function
- Summary stats
- Regression
- etc.
## Mammal life history data
```{r}
M <- read_delim("Data/mammals.txt", show_col_types = FALSE) |>
mutate(across(.cols = where(is.double),
.fns = ~ ifelse(.x == -999, NA, .x))) |>
rename(Order = MSW05_Order,
Species = MSW05_Binomial,
Body_mass = `5-1_AdultBodyMass_g`,
Forearm = `8-1_AdultForearmLen_mm`,
Body_length = `13-1_AdultHeadBodyLen_mm`,
Eye_opening = `2-1_AgeatEyeOpening_d`,
Repro_age = `3-1_AgeatFirstBirth_d`,
BMR_1 = `18-1_BasalMetRate_mLO2hr`,
BMR_2 = `5-2_BasalMetRateMass_g`,
Neonate_mass = `5-3_NeonateBodyMass_g`) |>
select(Order, Species, Body_mass, Forearm, Body_length, Eye_opening,
Repro_age, BMR_1, BMR_2, Neonate_mass)
head(M)
```
## Update processing function
```{r}
#| echo: true
trait_analysis <- function(trait, M) {
M_no_NA <- M |>
drop_na(Order, Body_mass, all_of(trait)) |>
select(Order, Body_mass, all_of(trait)) |>
mutate(across(.cols = where(is.double), .fns = ~ log10(.x + 0.001)))
summary_table <- M_no_NA |>
group_by(Order) |>
summarize(across(.cols = where(is.double),
.fns = mean))
p <- ggplot(M_no_NA, aes(x = Body_mass, y = .data[[trait]], color = Order)) +
geom_point() +
labs(y = str_replace(trait, "_", " ")) +
theme(legend.position = "none")
fm <- lm(M_no_NA[[trait]] ~ M_no_NA[["Body_mass"]])
return(list(trait = trait, p = p, summary_table = summary_table, fm = fm))
}
```
## Process traits
- Pass the new function as `.f`
```{r}
#| echo: true
LL <- map(.x = names(M)[4:10],
.f = trait_analysis,
M = M)
```
## A list of lists
Each element in the list has
1. Trait (string)
2. Plot
3. Table of means
4. An `lm()` fitted model
```{r}
#| echo: true
length(LL)
names(LL[[1]])
```
## Access parts of the list
```{r}
#| echo: true
LL[[1]]$summary_table
```
## What can we do now?
- Extract all the plots or tables or regressions
- Generate a report with each trait in turn
## Extract all the plots
```{r}
plots <- map(.x = 1:length(LL),
.f = function(ii, LL) {
return(LL[[ii]]$p)
},
LL = LL)
wrap_plots(plots)
```
`pluck(LL, ii, "p")` would also work here.
## Generate a report with each trait
In a Quarto file:
```{{r}}
#| output: "asis"
for (ii in 1:length(LL)) {
# Make a header
cat("\n## ", LL[ii], "\n")
# Print formatted table
knitr::kable(LL[[ii]]$summary_table)
# Print plot
print(LL[[ii]]$p)
# Print linear model summary
print(summary(LL[[ii]])$fm)
}
```