Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Winter25 data output #677

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions modules/Data_Output/Data_Output.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ output:
---

```{r, echo = FALSE}
library(knitr)
library(readr)
opts_chunk$set(comment = "")
library(tidyverse)
knitr::opts_chunk$set(comment = "")
```

<style type="text/css">
Expand All @@ -23,7 +22,7 @@ pre { /* Code block - slightly smaller in this lecture */

While its nice to be able to read in a variety of data formats, it's equally important to be able to output data somewhere.

The `readr` package provides data exporting functions which have the pattern `write_*`:
The `readr` package in the tidyverse provides data exporting functions which have the pattern `write_*`:

- `write_csv()`,
- `write_delim()`, others.
Expand Down Expand Up @@ -61,6 +60,15 @@ write_csv(dat, file = "YouthTobacco_newNames.csv")
write_delim(dat, file = "YouthTobacco_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

Expand Down Expand Up @@ -109,6 +117,20 @@ 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 @@ -119,6 +141,10 @@ knitr::include_graphics(here::here("images/save_environment.png"))

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

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

📃 [Day 2 Cheatsheet](https://jhudatascience.org/intro_to_r/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
2 changes: 1 addition & 1 deletion modules/Data_Output/lab/Data_Output_Lab.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ and assign it to an object named `vacc`.

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

Expand Down
2 changes: 1 addition & 1 deletion modules/Data_Output/lab/Data_Output_Lab_Key.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ and assign it to an object named `vacc`.

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

Expand Down
Loading