Skip to content
R for the Rest of Us Logo

R in 3 Months (Spring 2025)

Week 12 Live Session (Spring 2025)

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

{fs} package to move files between folders

View code shown in video

Misc Questions

# Functions that take variables as arguments ------------------------------

library(tidyverse)

penguins <- read_csv("data-raw/penguins.csv")

filter_by_year <- function(year_to_filter) {
  penguins |>
    filter(year == year_to_filter)
}

filter_by_year(year_to_filter = 2009)

calculate_mean <- function(variable) {
  penguins |>
    summarize(avg = mean({{ variable }}, na.rm = TRUE))
}

calculate_mean(bill_depth_mm)


# Colors ------------------------------------------------------------------

penguins_bill_length_by_island <-
  penguins |>
  group_by(island) |>
  summarize(mean_bill_length = mean(bill_length_mm, na.rm = TRUE))

ggplot(
  data = penguins_bill_length_by_island,
  aes(
    x = island,
    y = mean_bill_length,
    fill = island,
    label = island
  )
) +
  geom_col() +
  geom_text(vjust = -1) +
  theme_minimal(base_family = "Georgia")


scale_fill_custom <- function() {
  scale_fill_manual(values = c(
    "#1f77b4",
    "#ff7f0e",
    "#2ca02c",
    "#d62728",
    "#9467bd",
    "#8c564b",
    "#e377c2"
  ))
}

ggplot(
  data = penguins_bill_length_by_island,
  aes(
    x = island,
    y = mean_bill_length,
    fill = island,
    label = island
  )
) +
  geom_col() +
  geom_text(vjust = -1) +
  # theme_minimal(base_family = "Georgia") +
  omni::theme_omni() +
  omni::scale_fill_omni_discrete()

Quarto document I created to show how to make fonts work:

---
title: "Fonts"
format:
  html:
    mainfont: "Georgia"
execute: 
  echo: false
  warning: false
  message: false
  fig-height: 6
editor_options: 
  chunk_output_type: console
---

```{r}
library(tidyverse)
library(here)

penguins <- read_csv(here("data-raw/penguins.csv"))
```


## Set fonts in theme

```{r}
penguins_bill_length_by_island <-
  penguins |>
  group_by(island) |>
  summarize(mean_bill_length = mean(bill_length_mm, na.rm = TRUE))

ggplot(
  data = penguins_bill_length_by_island,
  aes(
    x = island,
    y = mean_bill_length,
    label = island
  )
) +
  geom_col() +
  geom_text(vjust = -1) +
  theme_minimal(base_family = "Georgia")
```


## Set fonts in geoms

```{r}
ggplot(
  data = penguins_bill_length_by_island,
  aes(
    x = island,
    y = mean_bill_length,
    label = island
  )
) +
  geom_col() +
  geom_text(
    vjust = -1,
    family = "Georgia"
  ) +
  theme_minimal(base_family = "Georgia")
```

## update_geom_defaults()

Proident aliquip sint aliqua enim culpa elit irure non amet cillum ipsum est cillum magna. Qui reprehenderit minim dolor reprehenderit officia sint consectetur irure. Occaecat ad sunt nisi deserunt ut nulla proident laborum commodo laborum sint excepteur veniam nisi ullamco. In officia eu cupidatat officia. Reprehenderit eiusmod consectetur nulla occaecat consequat mollit labore mollit. Aliqua labore elit ipsum laboris consequat.

```{r}
update_geom_defaults(
  geom = "text",
  aes(
    family = "Georgia",
    color = "red"
  )
)
```

```{r}
ggplot(
  data = penguins_bill_length_by_island,
  aes(
    x = island,
    y = mean_bill_length,
    label = island
  )
) +
  geom_col() +
  geom_text(vjust = -1) +
  theme_minimal(base_family = "Georgia")
```

```{r}
penguins |>
  count(year) |>
  mutate(year = as.character(year)) |>
  ggplot(
    aes(
      x = n,
      y = year,
      label = n
    )
  ) +
  geom_col() +
  geom_text(
    hjust = 2,
    color = "white"
  ) +
  theme_minimal(base_family = "Georgia")
```

Parameterized Reporting

Quarto document

---
title: "Penguins Report"
format: html
execute: 
  echo: false
  warning: false
  message: false
params:
  island: "Torgersen"
---

```{r}
library(tidyverse)
library(palmerpenguins)

data(penguins)
```

This is a report about penguins on `r params$island` island.

```{r}
penguins |> 
  filter(island == params$island) |> 
  group_by(sex) |> 
  summarize(avg_bill_length = mean(bill_length_mm, na.rm = TRUE)) |> 
  ungroup() |> 
  drop_na(sex) |> 
  ggplot(aes(x = sex, y = avg_bill_length)) +
  geom_col() +
  labs(title = str_glue("Average bill length by sex"))
```

render.R

library(tidyverse)
library(palmerpenguins)
library(quarto)

quarto_render(
  input = "report.qmd",
  output_file = "Biscoe.html",
  execute_params = list(
    island = "Biscoe"
  )
)

# islands <- c("Dream", "Biscoe", "Torgersen")

islands <-
  penguins |>
  distinct(island) |>
  pull(island) |>
  as.character()

reports <-
  tibble(
    input = "report.qmd",
    output_file = str_glue("{islands}.html"),
    execute_params = map(islands, ~ list(island = .))
  )



pwalk(reports, quarto_render)


# Move reports ------------------------------------------------------------

library(fs)

report_files <-
  dir_ls(
    regexp = ".html"
  )

dir_create("reports")

file_move(
  path = report_files,
  new_path = "reports"
)

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Course Content

127 Lessons