AE 13: Debugging R code

Suggested answers

Application exercise
Answers
Modified

October 22, 2024

Packages

We will use the following packages in this application exercise.

  • tidyverse: For data import, wrangling, and visualization.
  • babynames: For working with the Social Security Administration’s baby names data.
library(tidyverse)
library(babynames)

Capture and solve a reprex

Your turn: Copy the reprex from the course discussion board. Turn this into clean code using reprex::reprex_clean() and add it to the code chunk below. Then debug the code, fix any issues, and post the corrected code on the discussion board using a reprex.

library(tidyverse)
library(babynames)

name_trend <- function(person_name) {
  babynames |>
    filter(name == person_name) |>
    ggplot(mapping = aes(x = year, y = n, color = sex)) +
    geom_line() +
    scale_color_brewer(type = "qual") +
    labs(
      title = str_glue("Name: {person_name}"),
      x = "Year",
      y = "Number of births",
      color = NULL
    ) +
    theme_minimal()
}

name_trend("Benjamin")
1
Put Name: {person_name} in parentheses