Skip to content

Commit

Permalink
Class 12 slides
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemaz committed Feb 26, 2024
1 parent 2babf8c commit 325426b
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 2 deletions.
89 changes: 89 additions & 0 deletions docs/class12.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
title: "Geospatial Analysis with R"
subtitle: Class 12
output:
xaringan::moon_reader:
lib_dir: libs
css: ["default", "lucy", "middlebury-fonts", "themes/class14plus.css"]
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---

```{r, message=FALSE, eval=FALSE, warning=FALSE}
library(leaflet)
library(sf)
library(dplyr)
districts <- read_sf(
system.file("extdata/districts.geojson", package = "geospaar")
)
bb <- unname(st_bbox(districts))
xy <- st_centroid(districts) %>% st_coordinates() %>%
bind_cols(name = districts$distName, .)
slist <- list("color" = "white")
label_opts <- labelOptions(noHide = TRUE, style = slist, direction = 'center',
textOnly = TRUE, textsize = "5px")
m <- leaflet() %>%
addProviderTiles("Esri.WorldImagery") %>%
fitBounds(bb[1], bb[2], bb[3], bb[4]) %>%
addPolygons(data = districts, fill = FALSE, color = "white",
group = "Districts", weight = 1) %>%
addLabelOnlyMarkers(xy$X, xy$Y, label = xy$name, group = "Names",
labelOptions = label_opts) %>%
addLayersControl(overlayGroups = c("Districts", "Names"),
options = layersControlOptions(collapsed = FALSE,
autoZIndex = FALSE))
```

```{r, eval=FALSE, echo=FALSE}
library(htmlwidgets)
saveWidget(m, "docs/figures/zambialeaflet.html")
```


---

<iframe seamless src="figures/zambialeaflet.html" width="100%"
height="500"></iframe>

---
## Homework review

- Use `lapply` to make three `data.frame`s captured in a list `l`, each composed of one randomly sampled column `v1` (selecting from integers 1:10, with length = 20), and the second being `v2` composed of lowercase letters, randomly selected using `sample`, also of length 20.
- The iterator in the `lapply` should be 10, 20, 30, which become the random seeds for the sampling (in the body of the `lapply`)
- After making `l`, use a `for` loop to iterate through each element of `l`, writing each out to a folder `external/data/` in your project.
- Change the name of each as part of the iteration, so that `l[[1]]` is written out as `external/data/dataset1.csv`, etc. Hint: you can use `paste0` to make each file path and name.
- After writing these out, use another `lapply` to read back in the three datasets into a new list `l2`. Bonus: Use `dir` to programmatically read in the file paths from your `external/data` folder.

---

## Working with data

### `do.call`
```{r, eval=FALSE}
csv_files <- list.files(here::here("external/data/"), pattern = ".csv",
full.names = TRUE)
l <- lapply(csv_files, readr::read_csv)
do.call(rbind, l)
```


---
### Manipulating and analyzing data

- reshape
- mutate
- select
- filter
- split-apply-combine








236 changes: 236 additions & 0 deletions docs/class12.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>Geospatial Analysis with R</title>
<meta charset="utf-8" />
<script src="libs/header-attrs-2.19/header-attrs.js"></script>
<link href="libs/remark-css-0.0.1/default.css" rel="stylesheet" />
<link href="libs/remark-css-0.0.1/lucy.css" rel="stylesheet" />
<link href="libs/remark-css-0.0.1/middlebury-fonts.css" rel="stylesheet" />
<link rel="stylesheet" href="themes/class14plus.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: center, middle, inverse, title-slide

.title[
# Geospatial Analysis with R
]
.subtitle[
## Class 12
]

---



```r
library(leaflet)
library(sf)
library(dplyr)
districts &lt;- read_sf(
system.file("extdata/districts.geojson", package = "geospaar")
)
bb &lt;- unname(st_bbox(districts))
xy &lt;- st_centroid(districts) %&gt;% st_coordinates() %&gt;%
bind_cols(name = districts$distName, .)
slist &lt;- list("color" = "white")
label_opts &lt;- labelOptions(noHide = TRUE, style = slist, direction = 'center',
textOnly = TRUE, textsize = "5px")

m &lt;- leaflet() %&gt;%
addProviderTiles("Esri.WorldImagery") %&gt;%
fitBounds(bb[1], bb[2], bb[3], bb[4]) %&gt;%
addPolygons(data = districts, fill = FALSE, color = "white",
group = "Districts", weight = 1) %&gt;%
addLabelOnlyMarkers(xy$X, xy$Y, label = xy$name, group = "Names",
labelOptions = label_opts) %&gt;%
addLayersControl(overlayGroups = c("Districts", "Names"),
options = layersControlOptions(collapsed = FALSE,
autoZIndex = FALSE))
```




---

&lt;iframe seamless src="figures/zambialeaflet.html" width="100%"
height="500"&gt;&lt;/iframe&gt;

---
## Homework review

- Use `lapply` to make three `data.frame`s captured in a list `l`, each composed of one randomly sampled column `v1` (selecting from integers 1:10, with length = 20), and the second being `v2` composed of lowercase letters, randomly selected using `sample`, also of length 20.
- The iterator in the `lapply` should be 10, 20, 30, which become the random seeds for the sampling (in the body of the `lapply`)
- After making `l`, use a `for` loop to iterate through each element of `l`, writing each out to a folder `external/data/` in your project.
- Change the name of each as part of the iteration, so that `l[[1]]` is written out as `external/data/dataset1.csv`, etc. Hint: you can use `paste0` to make each file path and name.
- After writing these out, use another `lapply` to read back in the three datasets into a new list `l2`. Bonus: Use `dir` to programmatically read in the file paths from your `external/data` folder.

---

## Working with data

### `do.call`

```r
csv_files &lt;- list.files(here::here("external/data/"), pattern = ".csv",
full.names = TRUE)
l &lt;- lapply(csv_files, readr::read_csv)

do.call(rbind, l)
```


---
### Manipulating and analyzing data

- reshape
- mutate
- select
- filter
- split-apply-combine








</textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"highlightStyle": "github",
"highlightLines": true,
"countIncrementalSlides": false
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
window.dispatchEvent(new Event('resize'));
});
(function(d) {
var s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
if (!r) return;
s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
d.head.appendChild(s);
})(document);

(function(d) {
var el = d.getElementsByClassName("remark-slides-area");
if (!el) return;
var slide, slides = slideshow.getSlides(), els = el[0].children;
for (var i = 1; i < slides.length; i++) {
slide = slides[i];
if (slide.properties.continued === "true" || slide.properties.count === "false") {
els[i - 1].className += ' has-continuation';
}
}
var s = d.createElement("style");
s.type = "text/css"; s.innerHTML = "@media print { .has-continuation { display: none; } }";
d.head.appendChild(s);
})(document);
// delete the temporary CSS (for displaying all slides initially) when the user
// starts to view slides
(function() {
var deleted = false;
slideshow.on('beforeShowSlide', function(slide) {
if (deleted) return;
var sheets = document.styleSheets, node;
for (var i = 0; i < sheets.length; i++) {
node = sheets[i].ownerNode;
if (node.dataset["target"] !== "print-only") continue;
node.parentNode.removeChild(node);
}
deleted = true;
});
})();
// add `data-at-shortcutkeys` attribute to <body> to resolve conflicts with JAWS
// screen reader (see PR #262)
(function(d) {
let res = {};
d.querySelectorAll('.remark-help-content table tr').forEach(tr => {
const t = tr.querySelector('td:nth-child(2)').innerText;
tr.querySelectorAll('td:first-child .key').forEach(key => {
const k = key.innerText;
if (/^[a-z]$/.test(k)) res[k] = t; // must be a single letter (key)
});
});
d.body.setAttribute('data-at-shortcutkeys', JSON.stringify(res));
})(document);
(function() {
"use strict"
// Replace <script> tags in slides area to make them executable
var scripts = document.querySelectorAll(
'.remark-slides-area .remark-slide-container script'
);
if (!scripts.length) return;
for (var i = 0; i < scripts.length; i++) {
var s = document.createElement('script');
var code = document.createTextNode(scripts[i].textContent);
s.appendChild(code);
var scriptAttrs = scripts[i].attributes;
for (var j = 0; j < scriptAttrs.length; j++) {
s.setAttribute(scriptAttrs[j].name, scriptAttrs[j].value);
}
scripts[i].parentElement.replaceChild(s, scripts[i]);
}
})();
(function() {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
if (/^(https?:)?\/\//.test(links[i].getAttribute('href'))) {
links[i].target = '_blank';
}
}
})();
// adds .remark-code-has-line-highlighted class to <pre> parent elements
// of code chunks containing highlighted lines with class .remark-code-line-highlighted
(function(d) {
const hlines = d.querySelectorAll('.remark-code-line-highlighted');
const preParents = [];
const findPreParent = function(line, p = 0) {
if (p > 1) return null; // traverse up no further than grandparent
const el = line.parentElement;
return el.tagName === "PRE" ? el : findPreParent(el, ++p);
};

for (let line of hlines) {
let pre = findPreParent(line);
if (pre && !preParents.includes(pre)) preParents.push(pre);
}
preParents.forEach(p => p.classList.add("remark-code-has-line-highlighted"));
})(document);</script>

<script>
slideshow._releaseMath = function(el) {
var i, text, code, codes = el.getElementsByTagName('code');
for (i = 0; i < codes.length;) {
code = codes[i];
if (code.parentNode.tagName !== 'PRE' && code.childElementCount === 0) {
text = code.textContent;
if (/^\\\((.|\s)+\\\)$/.test(text) || /^\\\[(.|\s)+\\\]$/.test(text) ||
/^\$\$(.|\s)+\$\$$/.test(text) ||
/^\\begin\{([^}]+)\}(.|\s)+\\end\{[^}]+\}$/.test(text)) {
code.outerHTML = code.innerHTML; // remove <code></code>
continue;
}
}
i++;
}
};
slideshow._releaseMath(document);
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML';
if (location.protocol !== 'file:' && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
</body>
</html>
1 change: 1 addition & 0 deletions docs/index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ The class materials were designed by Lyndon Estes and Lei Song.
- tidyr universe, working with data
- regression, plotting
- Week 7 (Feb 19, 21)
- [Class 12 slides](class12.html)
- more ggplot, intro to vector
- Assignment 3 due (Unit 1 Module 4)
- Spring Break
Expand Down
3 changes: 2 additions & 1 deletion docs/index.html

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

## Spring 2024

Updated on: 2024-02-21
Updated on: 2024-02-26

<center>

Expand Down Expand Up @@ -72,6 +72,7 @@ The class materials were designed by Lyndon Estes and Lei Song.
- tidyr universe, working with data
- regression, plotting
- Week 7 (Feb 19, 21)
- [Class 12 slides](class12.html)
- more ggplot, intro to vector
- Assignment 3 due (Unit 1 Module 4)
- Spring Break
Expand Down

0 comments on commit 325426b

Please sign in to comment.