-
Notifications
You must be signed in to change notification settings - Fork 11
/
grandma_recipe.qmd
226 lines (146 loc) · 11 KB
/
grandma_recipe.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
---
title: "Grandma's Glop Recipe"
author: "Eric Anderson"
editor: source
markdown:
wrap: 72
bibliography: "./glop.bib"
csl: molecular-ecology.csl
format:
html:
theme: solar
toc: true
toc-float: true
df-print: paged
docx:
toc: true
pdf:
toc: true
---
# Setup
```{r Setup}
#| code-fold: true
#| message: false
library(tidyverse)
library(knitr)
```
# Prelude
## Subeheader
### Sub-sub-header
Many thanks to my r-mentor [Eric Anderson](https://swfsc.noaa.gov/staff.aspx?id=740) for sharing this lesson.
![Eric Anderson](./figures/Eric-Anderson-60.jpg)
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF,MS Word documents and even slideshows. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the **Render** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.
We are going to go through using many of the features of R Markdown whilst writing up a recipe that can calculate appropriate quantities if you want to increase the recipe by any factor.
First, we are going to make a subsection dedicated to grandma from whom this recipe came.
## Dedication to grandma {#tag-it}
This recipe came from my grandmother whom I always remember used to say:
> You only get to be young once,\
> But you can stay immature forever\
> --grandma
Notice in the above that we used `>` to make a block quote and we *also* used two spaces (which, of course, you cannot see!) at the end of each line to force a line break.
I leave this dedication with a picture of grandma:
![A pic of grandma](figures/Warthog4.jpg)
**Holy Oversized Image Batman**! You have got to be kidding me! There must be some way to make that image smaller.
**With Quarto, there is a way**
![A pic of grandma](figures/Warthog4.jpg){width=50%}
![A pic of grandma](figures/Warthog4.jpg){width=2in}
![A pic of grandma](figures/Warthog4.jpg){width="2in"}
We might come back to this [Dedication](#tag-it) at some point, so we have linked it to a tag. Note that it does not seem you can link to arbitrary text, but you can to section headers.
# The Ingredients (Static Version)
Let's list those ingredients for a single recipe's worth of Grandma's Glop. We call it "static" because it applies to just a single recipe. We will look at "automagically" doubling, or tripling, or "pi"-ing the recipe in a [later section](#calc-quants).
### Static List version {#static-recipe-list}
Let's see, we have already seen how to make text *italic* and **bold**. Did you notice how Rstudio highlights those bits in the text editor window? That is called "Syntax Highlighting" and it is super helpful.
Let's write our recipe in a list. Be wary! There has to be an empty line above the list *and* the space after the asterisk or number is important (i.e. mandatory)!
- 2 cups flour
- 4 eggs
- 1 Pound of Grandma Gorp:
- 0.5 pounds raisins
- 0.5 pounds cashews
Notice, the indent level is 4 spaces. You can use a `-` or a `*` or a `+` to start those list items. In fact, you can mix and match them too, as I did, but it is probably better to be consistent. (Use the same symbol within a nested level and different ones between).
Hey! did you notice the backticks (upper left of your keyboard--- same key as the tilde) around things. Those include inline code-fragments---basically they get printed out in monospaced type (any maybe in a different color). Super useful for variable names or functions like `x_data` or `y_data`.
While we are at it, let's look at what I have done with the text in the .Rmd file (the text file). Note that I have put plenty of line endings in so that there are not big long lines. This is a good habit to get into because git likes to look at differences between files on a line by line basis. If the lines are long, then it is harder for git to reasonably merge changes. Rstudio's editor will soft-wrap the lines so that it looks like there are line breaks but there really aren't. Like this ridiculously long line.
And do keep in mind that a blank line equates to a paragraph break.
### Static table version {#static-table-list}
We could also have written that stuff out in a table. There are actually lots of ways to write tables. See [this link](http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#tables).
| What | How Much | Units |
|:--------|:--------:|------:|
| Flour | 2 | cups |
| Eggs | 4 | eggs! |
| Raisins | 1/2 | lbs |
| Cashews | 1/2 | lbs |
: *Whoa! that is neat, but kind of a lot of work to input. I sure wouldn't want to do that for a big table! We will investigate ways to automate that. Note that the colons dictate the alignment.*
# How to make it
Once you have the ingredients, you have to prepare the meal. This is a great task for an ordered list:
1. Break the eggs and scramble them
2. Grind the cashews in a food mill
3. Eat the raisins to keep your energy up because you are going to have a lot of stirring to do. Notice that you can break lines in the text within an item, so long as you don't leave a blank line. The indenting makes the text source more readable but is not necessary.
4. Add cashews to eggs, stir in flour
5. Mix for 30 minutes.
Note that you can put whatever number you want in the front and the markdown interpreter will figure out which numbers they should be. The numbers must be followed by a period!
## Cooking time: It's complicated
Now, the big trick to getting grandma's glop just right is cooking it for the proper length of time. Describing this is going to require that we cite some literature and also that we write down some equations...What fun! Let's see how we do that with R Markdown. Cooking time $M$ (for minutes) decreases as the square root of the cooking temperature $T$, but increases as the $\frac{3}{2}$-power of the elevation $a$ (for altitude), in feet. For $300^\circ \leq T \leq 500^\circ$ and $0\leq a \leq 5000$, cooking time is $$
M = (20 - \sqrt{T/4}) \times \biggl(\frac{a+5000}{2500}\biggr)^\frac{3}{2}
$$ Anyone that is really interested in writing technical mathematical documents should learn to use LaTeX directly. Thankfully, you can imbed R code within LaTeX much as can be done with R Markdown.
We should perhaps qualify this model with a few statements. This model was not empirically derived by rigorous tests, rather we guessed at it given similar work done on cooking times of *Cassiopea* jellies at altitude [@sharp2022] and for *Orbicella* with different microbiomes [@boville2023]. But please bear in mind that we did not account for the issues noted by @gonzalez-pech2021 which arise while using next generation oven technology. You can easily add citations by clicking "Addins" and "Insert Citations"
Clearly, it would be nice to have a table of cooking times, $M$, for a few values of $T$ and $a$. Let's take $T\in\{300, 350, 400, 450, 500\}$ and $a\in\{0, 1250, 2500, 3750, 5000\}$. Holy smokes! There will be 25 rows in that table! I don't want to write that by hand!
Thankfully, we can use R to do it for us. We make a a data frame of values, then use a nifty function `kable` from the `knitr` package to make the table. Get ready, we are going to use some R:
```{r make-the-tam-table}
# first make the data frame
temps <- rep(seq(300,500,50), 5)
alts <- rep(seq(0, 5000, 1250), each=5)
tab <- tibble(T = temps, a = alts, M = (20-sqrt(temps/4)) * ((alts+5000)/2500) ^ (3/2))
```
And then we print it and it looks like this. (Check out how I hid that code block)
```{r}
#| echo: false
kable(tab)
```
Cooking times, $M$, at various temperatures, $T$, and elevations, $a$.
## Calculating Recipe Quantities {#calc-quants}
This could be done several ways, but let us simply look at how we would list the ingredients.
First, make an R code block where we declare the quantities:
```{r quants}
amts <- c(Flour = 2, Eggs = 4, Raisins = 0.5, Cashews = 0.5)
mult <- 40 # make a mult-fold version of the recipe
aa <- amts * mult
```
Then access those variables inline in the list. For example: In order to make `r mult` times the standard recipe, you will need
- `r aa["Flour"]` cups flour
- `r aa["Eggs"]` eggs
- `r aa["Raisins"] + aa["Cashews"]` lbs of Grandma Gorp:
- `r aa["Raisins"]` lbs raisins
- `r aa["Cashews"]` lbs cashews
And, if you want to make $\pi$ times the standard recipe you need:
```{r, include=FALSE}
aa <- amts * pi
```
- `r aa["Flour"]` cups flour
- `r aa["Eggs"]` eggs
- `r aa["Raisins"] + aa["Cashews"]` lbs of Grandma Gorp:
- `r aa["Raisins"]` lbs raisins
- `r aa["Cashews"]` lbs cashews
# A note about citations and references
You can define different styles in a "CSL" file which you can put where Rstudio can find it (in your project) and then call it out in the YAML header. You apparently can store your citation database in many different ways. I chose BibTeX because that is what I am used to. The citation data base for this document is `blop.bib`
Note that by default only the citations in your data base that actually got cited will be included in the References.
It should be pointed out that you can easily grab BibTex-formatted citations from [Google Scholar](http://scholar.google.com).
If you want to use a different bibliography style, browse the styles available at [Zotero Style Repository](https://zotero.org/styles). You can just download them and put them in your project. If we have time, we can try using the *Science* citation style.
If you use [Zotero](https://zotero.org) as your citation manager, you can set it up to maintain a bibliography in your repository based on a collection (folder) in Zotero. Every time you add a citation to the collection, Zotero will automagically write it to the .bib file!!
Much more about citations with R markdown can be found [here](http://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html) and you can find plenty of other good links at the bottom of this [page](http://rmarkdown.rstudio.com/).
# Making PDF and Word documents
By including `docx:` and `pdf:` in the YAML header, a little arrow appears next to the *Render* button.
To make a PDF document you need the TeX typesetting engine. It is free, but many of you probably don't have it yet.
To make a Word doc, click the down triangle to the right of *Render* and choose "Render Word". Can you figure out what changes in the document? (Again, it is textual and transparent!) Contrast that to the Word document itself---do this at your Unix command prompt (in the right directory of course):
cat grandma-recipe.docx
## Publishing on the web
Quarto also makes it easy to publish your work on `Github Pages`!
Following along with [these instructions](https://quarto.org/docs/publishing/github-pages.html) (using the `Render to Docs` option)
1. To get started, change your project configuration to use docs as the output-dir. For example:
<!-- -->
project:
type: website
output-dir: docs
touch .nojekyll
2. Render and push to Github.
3. Finally, configure your GitHub repository to publish from the docs directory of your main branch. Not quite working though...
## References