|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +element: notes |
| 4 | +title: Quarto |
| 5 | +language: R |
| 6 | +--- |
| 7 | + |
| 8 | +### Literate programming |
| 9 | + |
| 10 | +* Combine text, code, figures, tables, etc. |
| 11 | +* Quickly generate websites |
| 12 | +* Automatically generate reports |
| 13 | +* Easy to share results and code with collaborators |
| 14 | + |
| 15 | +### Getting started |
| 16 | + |
| 17 | +```r |
| 18 | +install.packages(c("quarto", "dplyr", "ggplot2", "gt", "readr")) |
| 19 | +``` |
| 20 | + |
| 21 | +* `File` -> `New File` -> `Quarto Document` |
| 22 | +* Title: `Portal Population Dynamics` |
| 23 | +* Default Output Format: `HTML` |
| 24 | +* Uncheck `Use visual markdown editor` |
| 25 | +* `Create` |
| 26 | +* Generates a basic stub of a `.qmd` document |
| 27 | +* The material between the `---`'s is called "front matter" |
| 28 | +* Metadata about the document |
| 29 | +* Title, author, and what we want to create from this file by default |
| 30 | +* Delete everything below the second `---` |
| 31 | + |
| 32 | +### Markdown |
| 33 | + |
| 34 | +* Basic approach to formatting text |
| 35 | +* Let's you do |
| 36 | + * `# Headers` |
| 37 | + * `*italics*` |
| 38 | + * `**bold**` |
| 39 | + * `[links](http://google.com)` |
| 40 | + * Lists |
| 41 | + * `*` |
| 42 | + * `1.` |
| 43 | + |
| 44 | +<pre><code> |
| 45 | +## Concept |
| 46 | + |
| 47 | +Exploration of population dynamic patterns at **The Portal Project**. |
| 48 | +How do counts of rodents like *Dipodomys* species change through time? |
| 49 | + |
| 50 | +In this document I will: |
| 51 | + |
| 52 | +1. Load data from the [Portal Project](https://portal.weecology.org) |
| 53 | +2. Process it into population time series |
| 54 | +3. And make initial visualizations |
| 55 | +</code></pre> |
| 56 | + |
| 57 | +* Easy to read for humans |
| 58 | +* Easy to convert into other things for computers |
| 59 | + |
| 60 | +* Press `Render` to create `HTML` from the document |
| 61 | +* Can also create `pdf` & Word versions of our files |
| 62 | +* Change output to `pdf` or `docx` |
| 63 | + |
| 64 | +* Switch back to html |
| 65 | +* Click the gear icon and select `Preview in Viewer Pane` |
| 66 | +* `Render` |
| 67 | +* This shows the rendered page right in RStudio |
| 68 | + |
| 69 | +* Markdown is common on lots of websites |
| 70 | +* Used to create all of the exercises and lectures in this course |
| 71 | + |
| 72 | +### Visual editor |
| 73 | + |
| 74 | +* We can also view and interact with the markdown in a style similar to a word processor |
| 75 | +* Click `Visual` |
| 76 | +* Markdown is now rendered in the editor |
| 77 | +* Can use the editor bar to make markdown changes |
| 78 | +* UNBOLD: The Portal Project |
| 79 | +* Click `Source` |
| 80 | +* See that it is no longer bolded using ** |
| 81 | + |
| 82 | +### R chunks |
| 83 | + |
| 84 | +* Quarto allows you to include code to run in the document |
| 85 | +* Click on `+C` button, which stands for "add code" |
| 86 | + |
| 87 | +<pre><code> |
| 88 | +## Required Packages |
| 89 | + |
| 90 | +```{r} |
| 91 | +library(dplyr) |
| 92 | +library(ggplot2) |
| 93 | +library(readr) |
| 94 | +``` |
| 95 | +</code></pre> |
| 96 | + |
| 97 | +* Can run this code by clicking the green arrow |
| 98 | +* Load the data and look at it |
| 99 | + |
| 100 | +<pre><code> |
| 101 | +## Data |
| 102 | + |
| 103 | +```{r} |
| 104 | +data <- read_csv("https://ndownloader.figshare.com/files/2292172") |
| 105 | +data |
| 106 | +``` |
| 107 | +</code></pre> |
| 108 | + |
| 109 | +* If we run the code with the green arrow we'll get a nicely formatted table |
| 110 | +* If we click the Render button the output will be included in the html web page |
| 111 | + |
| 112 | +* You can run code inside your text, too: |
| 113 | + |
| 114 | +``` |
| 115 | +The data includes `r length(unique(data$species_id))` species. |
| 116 | +``` |
| 117 | + |
| 118 | +### Analysis Example |
| 119 | + |
| 120 | +<pre><code> |
| 121 | +## Analysis |
| 122 | + |
| 123 | +Get the time-series of counts for all species. |
| 124 | + |
| 125 | +```{r} |
| 126 | +time_series <- |
| 127 | + data |> |
| 128 | + group_by(species_id, year) |> |
| 129 | + summarize(count = n()) |> |
| 130 | + filter(sum(count) > 200) |
| 131 | +time_series |
| 132 | +``` |
| 133 | +</code></pre> |
| 134 | + |
| 135 | +### Including Graphs |
| 136 | + |
| 137 | +* Graphs will be included as figures in the document |
| 138 | + |
| 139 | +<pre><code> |
| 140 | +## Plot the time-series. |
| 141 | + |
| 142 | +```{r} |
| 143 | +ggplot(time_series, aes(x = year, y = count)) + |
| 144 | + geom_line() + |
| 145 | + facet_wrap(vars(species_id), scales = "free_y") |
| 146 | +``` |
| 147 | +</code></pre> |
| 148 | + |
| 149 | +### Chunk options |
| 150 | + |
| 151 | +* Chunks have lots of useful options |
| 152 | +* Can add them at the start of the chunk using `#| `, the option name, and then the option value |
| 153 | +* `#| echo: false` let's you show the results of the code chunk without showing the code. |
| 154 | +* ADD: `#| echo: false` to plot chunk |
| 155 | +* `#| message: false` will remove messages returned by R |
| 156 | +* ADD: `#| message: false` to package, data, and analysis chunks |
| 157 | +* RENDER |
| 158 | + |
| 159 | +* There are also chunk options to include figure legends and alt text |
| 160 | +* ADD TO FIGURE CHUNK: |
| 161 | + |
| 162 | +``` |
| 163 | +#| fig-cap: "Figure 1. Time-series of rodent population dynamics at Portal." |
| 164 | +#| fig-alt: "Time-seris of 12 rodent species showing a range of dynamics from increases to decreases to variable, but not directionally changing, response" |
| 165 | +``` |
| 166 | + |
| 167 | +### Tables |
| 168 | + |
| 169 | +* We can also make nicely formated tables using the `gt()` function |
| 170 | +* Let's say we need to know who many total individuals of each species were included in the analysis |
| 171 | + |
| 172 | +<pre><code> |
| 173 | +```{r} |
| 174 | +time_series |> |
| 175 | + group_by(species_id) |> |
| 176 | + summarize(total_count = sum(count)) |> |
| 177 | + gt() |> |
| 178 | + cols_label( |
| 179 | + species_id = "Species", |
| 180 | + total_count = "Total Population" |
| 181 | + ) |
| 182 | +``` |
| 183 | +</code></pre> |
| 184 | + |
| 185 | +* You can add a label and then cite the table/figure in the text |
| 186 | + |
| 187 | +```r |
| 188 | +#| label: tbl-total-count |
| 189 | +#| tbl-cap: "Total population counts for each species in the study" |
| 190 | +``` |
| 191 | + |
| 192 | +* You can then reference the table in the text using `@tbl-total-count` |
| 193 | + |
| 194 | +### Citations |
| 195 | + |
| 196 | +* `Insert` -> `Citation` -> `From DOI` -> `10.1101/332783` -> `+` -> `Insert` |
| 197 | +* Creates a `bibliography.bib` file with the citations |
| 198 | +* This is called a bibtex file |
| 199 | +* Can now cite papers using [@ernest2018] |
| 200 | +* It will appear as an in-text citation and in a References section at the bottom of the document |
| 201 | + |
| 202 | +### Other Output Formats |
| 203 | + |
| 204 | +* Slides |
| 205 | +* Websites |
0 commit comments