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

[RStudio and Reproducibility] updating rstudio lecture and lab and reproducibility #176

Merged
merged 13 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
56 changes: 30 additions & 26 deletions modules/RStudio/RStudio.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -316,19 +316,6 @@ You can run all chunks above a specific chunk using this button:
knitr::include_graphics("images/previous_chunks.png")
```

## Chunk settings

```{r echo=FALSE, fig.align='center', out.width="80%"}
knitr::include_graphics("images/chunk_settings.png")
```

## Chunk settings

You can specify if a chunk will be seen in the report or not.

```{r echo=FALSE, fig.align='center', out.width="80%"}
knitr::include_graphics("images/chunk_settings2.png")
```

## Errors

Expand All @@ -339,19 +326,6 @@ Note that sometimes the error occurs earlier than RStudio thinks.
knitr::include_graphics("images/errors.png")
```

## Rainbow Parentheses

Tools --> Global Options --> Code --> Display --> Use rainbow parentheses

Press enter to save this setting and get out of this menu.

<br>
<br>

```{r echo=FALSE, fig.align='center', out.width="80%"}
knitr::include_graphics("https://posit.co/wp-content/themes/Posit/public/markdown-blogs/rstudio-1-4-preview-rainbow-parentheses/colorfulCode.png")
```


## Useful R Studio Shortcuts

Expand Down Expand Up @@ -442,7 +416,37 @@ Image by <a href="https://pixabay.com/users/geralt-9301/?utm_source=link-attribu

# Extra Slides

## Chunk settings

```{r echo=FALSE, fig.align='center', out.width="80%"}
knitr::include_graphics("images/chunk_settings.png")
```

## Chunk settings

You can specify if a chunk will be seen in the report or not.

```{r echo=FALSE, fig.align='center', out.width="80%"}
knitr::include_graphics("images/chunk_settings2.png")
```

## Sometimes you want to hide your code

If you want to keep your code so people can see it if they want to there is a nice option called code folding - check it out here: https://stackoverflow.com/questions/69326576/show-output-but-hide-code-when-sending-rmd-to-other-people

## Rainbow Parentheses

Tools --> Global Options --> Code --> Display --> Use rainbow parentheses

This can help you see your code more easily.

Press enter to save this setting and get out of this menu.

<br>
<br>

```{r echo=FALSE, fig.align='center', out.width="80%"}
knitr::include_graphics("https://posit.co/wp-content/themes/Posit/public/markdown-blogs/rstudio-1-4-preview-rainbow-parentheses/colorfulCode.png")
```


45 changes: 24 additions & 21 deletions modules/RStudio/lab/RStudio_Lab.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ editor_options:

# R Markdown

Please read through everything and then try the exercises. This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. The way you can create a file like this in RStudio is: File → New File → R Markdown and then using the default or using a template.
The file you are reading is called an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. The way you can create a file like this in RStudio is: File → New File → R Markdown and then using the default or using a template.

1. Try compiling this document using the “Knit” button. What files are produced?

### The Code

The gray area below is a code chunk that will set up our packages and data. You can also run the code within the editor area by pressing the green play button. Don't worry right now about what the code is doing, we will cover this later. We just want you to get used to RStudio and RMarkdowns. This dataset is one we'll be working with quite a lot in the lectures. It contains county-level data about ER visits for heat-related illnesses in Colorado for the years 2011-2022.

2. Press the green play button. What happens (check the environment too!)?

```{r setup, message=FALSE}
library(tidyverse)

Expand All @@ -22,13 +26,17 @@ er_2 <- er %>%
filter(!is.na(rate))
```

When you click the **Knit** button (at the top of RStudio), a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. Next we will show you some code - no worries about understanding it yet, we just want to get you familiar with where code goes. When code is in an RMarkdown file chunk, it is saved to a file. When it is in the console, it is not saved. The console is useful for installing packages like we just did, this is because we only need to do it once, so we don't usually need to save the code.
When you click the **Knit** button (at the top of RStudio), a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. Note that when it does this it starts from scratch - it can't use any objects you created outside of the Rmd file.
carriewright11 marked this conversation as resolved.
Show resolved Hide resolved

### Plotting some data

Here is code that will make a plot of some data.You can embed an R code chunk like this: Try pressing the green play button to see what happens. Make sure you have run the previous code chunk first by pressing the green play button in that chunk.
Here is code that will make a plot of some data.

3. Try the "run all chunks above" button that is to the left of the play button on the following chunk. What happens?

```{r plot, out.width = "100%"}
4. Then press the play button on this same chunk. What happens?

```{r out.width = "100%"}
# keep only some counties
er_3 <- er_2 %>%
filter(county %in% c("Arapahoe", "Denver", "Jefferson", "Larimer"))
Expand All @@ -37,43 +45,38 @@ palette <- c(
Arapahoe = "blue",
Denver = "darkgreen",
Jefferson = "orange",
Larimer = "purple"
Larimer = "salmon"
)

ggplot(aes(x = year, y = rate, colour = county, group = county), data = er_3) +
geom_line() +
scale_colour_manual(values = palette)
```

```{r out.width = "100%", label = '0response'}

```
### Adding new chunks


# Exercise

Here are a few changes that will show you how to change small things in `R` code and the output it makes. After each change, hit the **Knit** button again.

### 1.1

Go through the code for the plot above and change the colors in `palette` to something other than what they originally were. See http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf for a large list of colors. For example, you could replace blue with red.

### 1.2

Add a new R chunk after `<!-- Your code chunk goes here -->`. You can use the insert chunk button or copy paste an existing code chunk. Include a lowercase x inside the chunk as the code. Make sure you press the knit button after this to see what the new chunk looks like.
5. Add a new R chunk after `<!-- Your code chunk goes here -->`. You can use the insert chunk button or copy paste an existing code chunk. Include a lowercase x inside the chunk as the code. Make sure you press the knit button after this to see what the new chunk looks like.

```{r}
x <- c(1, 2, 3)
```

<!-- Your code chunk goes here -->



# Practice on Your Own!

Here are a few changes that will show you how to change small things in `R` code and the output it makes. After each change, hit the **Knit** button again.

### P.1

Update the options of the R chunk you just made (with the lowercase x in it) so that the output option is `show output only`. How does the chunk look now?
Go through the code for the plot above and change the colors in `palette` to something other than what they originally were. See http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf for a large list of colors. For example, you could replace blue with red.

### P.2

Create another R Markdown Document from RStudio dropdowns: File → New File → R Markdown.

### P.3

Add a new header with two hash symbols `##` at the start of a line with some text. Knit the report to see how it looks.
57 changes: 26 additions & 31 deletions modules/RStudio/lab/RStudio_Lab_Key.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ editor_options:

# R Markdown

Please read through everything and then try the exercises. This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. The way you can create a file like this in RStudio is: File → New File → R Markdown and then using the default or using a template.
The file you are reading is called an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. The way you can create a file like this in RStudio is: File → New File → R Markdown and then using the default or using a template.

1. Try compiling this document using the “Knit HTML” button. What files are produced?
carriewright11 marked this conversation as resolved.
Show resolved Hide resolved

### The Code

The gray area below is a code chunk that will set up our packages and data. You can also run the code within the editor area by pressing the green play button. Don't worry right now about what the code is doing, we will cover this later. We just want you to get used to RStudio and RMarkdowns. This dataset is one we'll be working with quite a lot in the lectures. It contains county-level data about ER visits for heat-related illnesses in Colorado for the years 2011-2022.

2. Press the green play button. What happens (check the environment too!)?

```{r setup, message=FALSE}
library(tidyverse)

Expand All @@ -22,30 +26,17 @@ er_2 <- er %>%
filter(!is.na(rate))
```

When you click the **Knit** button (at the top of RStudio), a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. Next we will show you some code - no worries about understanding it yet, we just want to get you familiar with where code goes. When code is in an RMarkdown file chunk, it is saved to a file. When it is in the console, it is not saved. The console is useful for installing packages like we just did, this is because we only need to do it once, so we don't usually need to save the code.
When you click the **Knit** button (at the top of RStudio), a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. Note that when it does this it starts from scratch - it can't use any objects you created outside of the Rmd file.
carriewright11 marked this conversation as resolved.
Show resolved Hide resolved

### Plotting some data

Here is code that will make a plot of some data.You can embed an R code chunk like this: Try pressing the green play button to see what happens. Make sure you have run the previous code chunk first by pressing the green play button in that chunk.

```{r plot, out.width = "100%"}
# keep only some counties
er_3 <- er_2 %>%
filter(county %in% c("Arapahoe", "Denver", "Jefferson", "Larimer"))
Here is code that will make a plot of some data.

palette <- c(
Arapahoe = "blue",
Denver = "darkgreen",
Jefferson = "orange",
Larimer = "purple"
)
3. Try the "run all chunks above" button that is to the left of the play button on the following chunk. What happens?

ggplot(aes(x = year, y = rate, colour = county, group = county), data = er_3) +
geom_line() +
scale_colour_manual(values = palette)
```
4. Then press the play button on this same chunk. What happens?

```{r out.width = "100%", label = '0response'}
```{r out.width = "100%"}
# keep only some counties
er_3 <- er_2 %>%
filter(county %in% c("Arapahoe", "Denver", "Jefferson", "Larimer"))
Expand All @@ -62,31 +53,35 @@ ggplot(aes(x = year, y = rate, colour = county, group = county), data = er_3) +
scale_colour_manual(values = palette)
```

### Adding new chunks

# Exercise

Here are a few changes that will show you how to change small things in `R` code and the output it makes. After each change, hit the **Knit** button again.

### 1.1

Go through the code for the plot above and change the colors in `palette` to something other than what they originally were. See http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf for a large list of colors. For example, you could replace blue with red.

### 1.2

Add a new R chunk after `<!-- Your code chunk goes here -->`. You can use the insert chunk button or copy paste an existing code chunk. Include a lowercase x inside the chunk as the code. Make sure you press the knit button after this to see what the new chunk looks like.
5. Add a new R chunk after `<!-- Your code chunk goes here -->`. You can use the insert chunk button or copy paste an existing code chunk. Include a lowercase x inside the chunk as the code. Make sure you press the knit button after this to see what the new chunk looks like.

```{r}
x <- c(1, 2, 3)
```

<!-- Your code chunk goes here -->

```{r}
x
```


# Practice on Your Own!

Here are a few changes that will show you how to change small things in `R` code and the output it makes. After each change, hit the **Knit** button again.

### P.1

Update the options of the R chunk you just made (with the lowercase x in it) so that the output option is `show output only`. How does the chunk look now?
Go through the code for the plot above and change the colors in `palette` to something other than what they originally were. See http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf for a large list of colors. For example, you could replace blue with red.

### P.2

Create another R Markdown Document from RStudio dropdowns: File → New File → R Markdown.

### P.3

Add a new header with two hash symbols `##` at the start of a line with some text. Knit the report to see how it looks.

## New Header
14 changes: 10 additions & 4 deletions modules/Reproducibility/Reproducibility.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,19 @@ Provide sufficient detail so that you can understand what you did.

## Need random numbers to stay consistent?

Use `set.seed()` : sets the starting state for the random number generator (RNG) in R.
Use `set.seed()` : sets the starting state for the [random number generator (RNG)](https://en.wikipedia.org/wiki/Random_number_generation) in R.

```{r}
set.seed(100)
```{r, echo = TRUE}
set.seed(123)
sample(10)
set.seed(123)
sample(10)
set.seed(456)
sample(10)
```

Note that these are only psuedo random and the values are created doing calculations based on the given seed. Thus the same "random" values will be reproduced by everyone using the same seed with `set.seed`.

## R Markdown syntax

Before:
Expand Down Expand Up @@ -176,7 +182,7 @@ ottrpal::include_slide("https://docs.google.com/presentation/d/1-cauITbscmnDEDt5

## Session info can help

```{r, eval = FALSE}
```{r, eval = FALSE, echo = TRUE}
sessionInfo()
```

Expand Down
2 changes: 1 addition & 1 deletion scripts/blank_labs_from_keys.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

find modules/ -type f -name "*_Key.Rmd" -print0 | while IFS= read -r -d '' file; do
find modules/ -type f ! -name "*RStudio*" -name "*_Key.Rmd" -print0 | while IFS= read -r -d '' file; do
# Remove Rmarkdown chunk content
# !! Note that the chunks must be named, with the name ending with "response"
# to be cleared. This prevents unwanted erasure of library loading and knitr
Expand Down
Loading