Skip to content

Commit

Permalink
update cs_08
Browse files Browse the repository at this point in the history
  • Loading branch information
adammwilson committed Oct 17, 2024
1 parent f2c7d4f commit d0eabc2
Show file tree
Hide file tree
Showing 90 changed files with 14,591 additions and 1,106 deletions.
1 change: 0 additions & 1 deletion CS_06.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ reading:
- Raster Vector Interactions [GCR](https://r.geocompx.org/raster-vector){target='blank'}
tasks:
- Calculate annual mean temperatures from a monthly spatio-temporal dataset
- Remove Antarctica from the `world` dataset
- Summarize raster values within polygons
- Generate a summary figure and table.
- Save your script as a .R or .Rmd in your course repository
Expand Down
22 changes: 22 additions & 0 deletions CS_07_help.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generate a dataset
# make a scatterplot
# summarize a linear model

# Short script to illustrate copilot in coding
library(tidyverse)

data=tibble(
x=rnorm(10),
y=3*x+15+rnorm(10))


ggplot(data,aes(x=x,y=y))+
geom_point()+
geom_smooth(method="lm")







23 changes: 23 additions & 0 deletions CS_07_help2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Create a robust function to calculate the standard error of a vector
# The function name will be calc_se with an argument x for the vector, with
# an argument to remove missing values called na.rm, and a default value of TRUE
# The function should have type checking to only allow numeric vectors

# ```{r}
# calc_se=function(x,na.rm=T){
# if(!is.numeric(x)){
# stop("Input must be a numeric vector")
# }

# if(na.rm){
# x=na.omit(x)
# }
# sqrt(var(x)/length(x))
# }


# # Test the function
x=c(3,6,12,89)
calc_se(x)


14 changes: 14 additions & 0 deletions CS_07_help3.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


library(dplyr)

mtcars

mtcars |>
group_by(cyl) |>
summarize(
mean = mean(mpg),
# add the standard error for mpg
se = sd(mpg) / sqrt(n())

)
5 changes: 5 additions & 0 deletions CS_07_help4.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Downloads total population data for buffalo, ny using the tidycensus package
# save the downloaded data as a csv file
# use ggplot to plot the population data


34 changes: 34 additions & 0 deletions CS_07_help5.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@


library(terra)
library(spData)
library(tidyverse)
library(sf)


# download.file("https://crudata.uea.ac.uk/cru/data/temperature/absolute.nc","crudata.nc")

# read in the data using the rast() function from the terra package
tmean=rast("crudata.nc")
data(world)

max_temp <- tmean %>%
max() %>%
terra::extract(y=world, fun=max, na.rm=T, small=T)

world_clim=bind_cols(world,max_temp)

ggplot(world_clim,aes(fill=max))+
geom_sf()+
coord_sf(label_graticule="SW",label_axes="SW")+
scale_fill_viridis_c(name="Maximum\nTemperature (C)")+
theme(legend.position = 'bottom')


world_clim%>%
group_by(continent)%>%
slice_max(n=1,order_by=max) %>%
dplyr::select(name_long,continent,mux=max)%>%
arrange(desc(min))%>%
st_set_geometry(NULL)

33 changes: 16 additions & 17 deletions CS_08.Rmd
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
---
title: One Script, Many Products
subtitle: RMarkdown to create dynamic research outputs. Publishing to github/word/html/etc
subtitle: Quarto to create dynamic research outputs. Publishing to github/word/html/etc
week: 8
type: Case Study
reading:
- Browse website about [RMarkdown](https://rmarkdown.rstudio.com/index.html)
- Browse [R Markdown, the Definitive Guide](https://bookdown.org/yihui/rmarkdown/)
- Browse website about [Quarto](https://quarto.org/)
tasks:
- Build a RMarkdown document that downloads a dataset, produces one graph and one table, and exports to four different formats (HTML, GitHub Markdown, Word, Powerpoint).
- Build a Quarto document that downloads a dataset, produces one graph and one table, and exports to four different formats (HTML, GitHub Markdown, Word, Powerpoint).
---

```{r, echo=FALSE, message=FALSE, results='hide', purl=FALSE}
Expand Down Expand Up @@ -40,25 +39,25 @@ You are working on a team that needs to provide regular updates about a dataset
* Word document that is included in company reports
* A PDF document for downloading/printing

This takes the employee about 3 hours every week. You are a new member of the team and you confidently declare you could automate the procedure using R and RMarkdown (and that you could complete the automation in less than three hours!). The team looks at you with wide eyes. You realize you better get working.
This takes the employee about 3 hours every week. You are a new member of the team and you confidently declare you could automate the procedure using R and Quarto (and that you could complete the automation in less than three hours!). The team looks at you with wide eyes. You realize you better get working.

## Specifying multiple outputs with RMarkdown
## Specifying multiple outputs with Quarto
You can specify that RMarkdown should produce multiple outputs using the following syntax in the YAML header:

```
output:
html_document: default
github_document: default
powerpoint_presentation: default
word_document: default
format:
html: default
github: default
pptx: default
docx: default
```

You can read more about the YAML header and all the options [here](https://bookdown.org/yihui/rmarkdown/html-document.html). Note that you can specify many options for each output format to change the theme, structure, etc.
You can read more about the YAML header and all the options [here](https://quarto.org/docs/authoring/front-matter.html). Note that you can specify many options for each output format to change the theme, structure, etc.

However, if you click the "Knit" button in RStudio, it will only make one output. To 'render' all of them, you have to use a command like this in the R Console.
However, if you click the "Render" button in RStudio, it will only make one output. To 'render' all of them, you have to use a command like this in the R Console.

```
rmarkdown::render("path/to/file.Rmd",output_format = "all")
quarto::quarto_render("path/to/file.qmd",output_format = "all")
```

## Data
Expand All @@ -79,14 +78,14 @@ The document should:
<div id="demo1" class="collapse">

## Hints
1. Create a new RMarkdown Document (possibly starting with the template in `File -> New File -> R Markdown`
2. Edit the YAML header at the top of the .Rmd file to specify the desired file types as noted above.
1. Create a new Quarto Document (possibly starting with the template in `File -> New File -> Quarto Document`)
2. Edit the YAML header at the top of the .qmd file to specify the desired file types as noted above.
3. Write code to read the "Mauna Loa CO2 annual mean data" from [this website](https://www.esrl.noaa.gov/gmd/ccgg/trends/data.html).
* check out `readr`::read_table()` or similar to import the dataset into R directly from the URL (ftp://aftp.cmdl.noaa.gov/products/trends/co2/co2_annmean_mlo.txt)
* after you look at the format of the text file, you may want to check out the `skip` parameter of `read_table()` or potentially use the comment character `#`. You can also use `col_names` to name the columns correctly.
4. Use ggplot to plot a time series of CO2 levels through time
5. Add an additional table below the graph and format it nicely with `knitr::kable()` or similar.
6. Use `rmarkdown::render(file, output_format = "all")` to render all the outputs specified in the YAML.
6. Use `quarto::render("path/to/file.qmd",output_format = "all")` to render all the outputs specified in the YAML.
7. Consider changing the 'chunk' settings so that the underlying code (and any messages) are hidden in the output documents. For example, consider `results='hide',message=FALSE, echo=F`.
8. Tables can be a little tricky to embed in multiple formats. One approach is to use `as_image()` in kableExtra package which generates a png file that is easy to embed. You can use it like this:
```
Expand Down
Binary file modified CS_08_Template/CS_08_template.docx
Binary file not shown.
594 changes: 204 additions & 390 deletions CS_08_Template/CS_08_template.html

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 0 additions & 11 deletions CS_08_Template/CS_08_template.md

This file was deleted.

Binary file modified CS_08_Template/CS_08_template.pptx
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
---
title: "Carbon Dioxide Concentrations at Mona Loa Observatory"
author: "Adam M. Wilson"
output:
html_document: default
github_document: default
powerpoint_presentation: default
word_document: default
toc: true
format:
html: default # Website
gfm: default # Github Flavored Markdown
pptx: default # Powerpoint
docx: default # MS Word
---

```{r setup, include=FALSE}
Expand Down Expand Up @@ -34,7 +35,7 @@ data=read_table("ftp://aftp.cmdl.noaa.gov/products/trends/co2/co2_annmean_mlo.tx
# Annual Mean Carbon Dioxide Concentrations 1959-Present
```{r pressure, echo=FALSE}
ggplot(data,aes(x=year,y=mean))+
geom_line(col="red",size=2)+
geom_line(col="red",linewidth=2)+
ylab("Mauna Loa Annual Mean CO_2 (ppm)")+
xlab("Year")
```
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CS_08_Template/CS_08_template_files/figure-docx/pressure-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CS_08_Template/CS_08_template_files/figure-html/pressure-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CS_08_Template/CS_08_template_files/figure-pptx/pressure-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d0eabc2

Please sign in to comment.