AE 07: Debugging

Application exercise

In this activity, we will go over how to identify and fix common coding errors. This demonstration will use the mtcars data set. In each of these code chunks, you will either need to add or alter code in order to get it running. Please take notes and treat this AE as a “common errors debugging sheet” to use in the future. There will only be one error per code chunk.

  1. First, let’s make a quick exploratory plot to assess these data.
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0     ✔ purrr   0.3.5
✔ tibble  3.1.8     ✔ dplyr   1.0.9
✔ tidyr   1.2.1     ✔ stringr 1.4.1
✔ readr   2.1.3     ✔ forcats 0.5.2
Warning: package 'ggplot2' was built under R version 4.2.2
Warning: package 'tidyr' was built under R version 4.2.2
Warning: package 'readr' was built under R version 4.2.2
Warning: package 'purrr' was built under R version 4.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
mtcars |> 
  ggplot(
    aes(x = mpg, y = wt)
  ) + 
  geom_point()

Takeaway - need to library packages

mtcars |>
  ggplot(
    aes(x = mpg, y = gear)
  ) + 
  geom_point()

Takeaway - object must be defined. Watch for mispellings

#|label: plot-3
#|eval: false 

mtcars |>
  ggplot(
    aes(x = mpg, y = wt)
  ) + 
  geom_point()

Takeaway - match up ()s

#|label: plot-4
#|eval: false

mtcars |>
  ggplot(
    aes(x = mpg)
  ) + 
  geom_bar()

Takeaway - can’t have x and y

#|label: plot-5
#|eval: false

mtcars.new <- mtcars |>
  filter(mpg > 17)

mtcars.new |>
  ggplot(
    aes(x = mpg, y = wt)
  ) + 
  geom_point()

Takeaway - must define new data set

#|label: plot-6
#|eval: false


mtcars |>
  ggplot(
    aes(x = mpg, y = wt)
  ) + 
  geom_point()

Takeaway - don’t have pipe on own line

p <- mtcars |>
  mutate(cyl = factor(cyl)) |>
  ggplot(
    aes(x = mpg, y = wt, color = cyl)
  ) + 
  geom_point() 
  
p + scale_color_viridis_d()

Takeaway - _c vs _d

mtcars |>
  mutate(cyl = factor(cyl)) |>
  ggplot(
    aes(x = mpg, y = wt, color = cyl)
  ) + 
  geom_point() 

Takeaway - missing ,

HW-1 Demo

The following code has multiple errors. Let’s fix the code to the point where we make the following graph:

Strategies:

  • Don’t diagnose all errors at once

  • Go line by line

library(openintro)
 
duke_forest |>
  mutate(garage = if_else(str_detect(parking, "Garage"), "Garage", "No garage")) |>
  ggplot(aes(x = price, fill = garage)) +
  geom_histogram() +
  facet_wrap(~garage, ncol = 1) +
  labs(
    x = "Price in $",
    y = "",
    title = "Histogram of Price of Homes by Garage or not",
    fill = "Garage or not"
  )

Takeaway - str_detect needs variable to search for

x = needs quotes

facet_wrap needs ~ before variable