Skip to content

Commit

Permalink
Merge pull request #158 from fhdsl/update-data-output-lab
Browse files Browse the repository at this point in the history
[Data Output] Changing the dataset for the lab and a few other Data Output Updates
  • Loading branch information
avahoffman authored Oct 9, 2024
2 parents dcbb90a + d0bed3a commit c198843
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 608 deletions.
4 changes: 2 additions & 2 deletions modules/Data_Input/Data_Input.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,9 @@ Don't forget to use `<-` to assign your data to an object!

💻 [Data Input Lab](https://daseh.org/modules/Data_Input/lab/Data_Input_Lab.Rmd)

[Posit's Data Import Cheatsheet](https://rstudio.github.io/cheatsheets/data-import.pdf)
📃 [Posit's Data Import Cheatsheet](https://rstudio.github.io/cheatsheets/data-import.pdf)

[Day 2 Cheatsheet](https://daseh.org/modules/cheatsheets/Day-2.pdf)
📃 [Day 2 Cheatsheet](https://daseh.org/modules/cheatsheets/Day-2.pdf)

```{r, fig.alt="The End", out.width = "50%", echo = FALSE, fig.align='center'}
knitr::include_graphics(here::here("images/the-end-g23b994289_1280.jpg"))
Expand Down
32 changes: 31 additions & 1 deletion modules/Data_Output/Data_Output.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ write_delim(dat, file = "CO_ER_heat_newNames.csv", delim = ",")
```


## GUT CHECK!

What does `write_csv()` do? Saves data to..

A. R's memory

B. A file on your hard drive

C. A ggplot


## R binary file

`.rds` is an extension for R native file format.
Expand Down Expand Up @@ -110,6 +121,21 @@ knitr::include_graphics(here::here("images/subset_objects_in_environment.png"))
knitr::include_graphics(here::here("images/save_environment.png"))
```


## REMINDER: Saving a ggplot to file

A few options:

- RStudio \> Plots \> Export \> Save as image / Save as PDF
- RStudio \> Plots \> Zoom \> [right mouse click on the plot] \> Save image as
- In the code

```{r, eval = FALSE}
ggsave(filename = "saved_plot.png", # will save in working directory
plot = rp_fac_plot,
width = 6, height = 3.5) # by default in inches
```

## Summary {.small}

- Use `write_csv()` and `write_delim()` from the `readr` package to write your (modified) data
Expand All @@ -120,7 +146,11 @@ knitr::include_graphics(here::here("images/save_environment.png"))

💻 [Data Output Lab](https://daseh.org/modules/Data_Output/lab/Data_Output_Lab.Rmd)

```{r, fig.alt="The End", out.width = "50%", echo = FALSE, fig.align='center'}
📃 [Posit's Data Import Cheatsheet](https://rstudio.github.io/cheatsheets/data-import.pdf)

📃 [Day 8 Cheatsheet](https://daseh.org/modules/cheatsheets/Day-2.pdf)

```{r, fig.alt="The End", out.width = "30%", echo = FALSE, fig.align='center'}
knitr::include_graphics(here::here("images/the-end-g23b994289_1280.jpg"))
```

Expand Down
12 changes: 5 additions & 7 deletions modules/Data_Output/lab/Data_Output_Lab.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ Remember anything you type here can be "sent" to the console with Cmd-Enter (OS-
# Part 1

### 1.1

Read in the SARS-CoV-2 wastewater data from URL
https://daseh.org/data/SARS-CoV-2_Wastewater_Data.csv
and assign it to an object named `covid`.
Load the tidyverse package. Then read in the CalEnviroScreen dataset from
https://daseh.org/data/CalEnviroScreen_data.csv
and assign it to an object named `ces.

```
# General format
Expand All @@ -29,7 +28,7 @@ library(readr)

### 1.2

Filter the dataset so that the "reporting_jurisdiction" column is equal to "Maryland". Store the modified dataset as `covid_filtered`.
Filter the dataset so that the `CaliforniaCounty` column is equal to "Yuba". Store the modified dataset as `ces_Yuba`.

```
# General format
Expand All @@ -42,7 +41,7 @@ NEW_OBJECT <- OBJECT %>% filter(COLUMNNAME == CRITERIA)

### 1.3

Write out the `covid_filtered` object as a CSV file calling it "covid_filtered.csv", using `write_csv()`:
Write out the `ces_Yuba` object as a CSV file calling it "ces_Yuba.csv", using `write_csv()`:

```{r 1.3response}
Expand All @@ -60,7 +59,6 @@ Copy your code from problem 1.3 and modify it to write to the data directory ins
```

### P.2

Write one of the objects in your Environment to your working directory in `rds` format. Call the file `my_variable.rds`.

```{r P.2response}
Expand Down
28 changes: 13 additions & 15 deletions modules/Data_Output/lab/Data_Output_Lab_Key.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,40 @@ Remember anything you type here can be "sent" to the console with Cmd-Enter (OS-
# Part 1

### 1.1

Read in the SARS-CoV-2 wastewater data from URL
https://daseh.org/data/SARS-CoV-2_Wastewater_Data.csv
and assign it to an object named `covid`.
Load the tidyverse package. Then read in the CalEnviroScreen dataset from
https://daseh.org/data/CalEnviroScreen_data.csv
and assign it to an object named `ces`.

```
# General format
library(readr)
# OBJECT <- read_csv(FILE)
library(tidyverse)
OBJECT <- read_csv(FILE)
```

```{r 1.1response}
library(tidyverse)
covid <- read_csv(file = "https://daseh.org/data/SARS-CoV-2_Wastewater_Data.csv")
ces <- read_csv(file = "https://daseh.org/data/CalEnviroScreen_data.csv")
```

### 1.2

Filter the dataset so that the "reporting_jurisdiction" column is equal to "Maryland". Store the modified dataset as `covid_filtered`.
Filter the dataset so that the `CaliforniaCounty` column is equal to "Yuba". Store the modified dataset as `ces_Yuba`.

```
# General format
NEW_OBJECT <- OBJECT %>% filter(COLUMNNAME == CRITERIA)
```

```{r 1.2response}
covid_filtered <- covid %>% filter(reporting_jurisdiction == "Maryland")
ces_Yuba <- ces %>% filter(CaliforniaCounty == "Yuba")
```

### 1.3

Write out the `covid_filtered` object as a CSV file calling it "covid_filtered.csv", using `write_csv()`:
Write out the `ces_Yuba` object as a CSV file calling it "ces_Yuba.csv", using `write_csv()`:

```{r 1.3response}
write_csv(covid_filtered, file = "covid_filtered.csv")
write_csv(ces_Yuba, file = "ces_Yuba.csv")
```


Expand All @@ -57,9 +56,8 @@ write_csv(covid_filtered, file = "covid_filtered.csv")
Copy your code from problem 1.3 and modify it to write to the data directory inside your R Project. *Note: you may need to make a new folder named "data" if it doesn't already exist.*

```{r eval = FALSE, label = 'P.1response'}
getwd()
dir.create("data")
write_csv(covid_filtered, file = "data/covid_filtered.csv")
getwd() # Check -- are you in the project directory?
write_csv(ces_Yuba, file = "data/ces_Yuba.csv")
```

### P.2
Expand All @@ -73,7 +71,7 @@ write_rds(y, file = "my_variable.rds")

### P.3

Read the RDS file from your working directory back into your Environment. Call the file `z`.
Read the RDS file you just created from your working directory back into your Environment. Call the file `z`.

```{r P.3response}
z <- read_rds(file = "my_variable.rds")
Expand Down
1 change: 0 additions & 1 deletion modules/Data_Output/lab/covid_filtered.csv

This file was deleted.

Loading

0 comments on commit c198843

Please sign in to comment.