Skip to content

2.3 Tidy Eval

Ian Lyttle edited this page Jun 12, 2022 · 5 revisions

2.3.1 Pass the dots

No files to be templated in.

Tasks

  1. exercise:
    • R/get-matches.R:
      • add ... to formals of uss_get_matches()
      • use ... in dplyr::filter() for results
      • add to documentation (#' @inheritParams dplyr::filter)
      • add to description in documentation
      • add example
    • tests/testthat/test-get-matches.R:
      • test ... works using dplyr::filter()

Result

  • we can pass dplyr::filter() expressions to uss_get_matches(), it will filter the results.
  • R CMD CHECK will be happy.

2.3.2 Use the pronouns

Template in files:

  • R/teams-matches.R
  • tests/testthat/test-teams-matches.R

Tasks

  1. walk through code, tests
  2. usethis::use_import_from("rlang", ".env")
  3. in make_teams_matches():
at_home <- FALSE
teams_matches_visitor <-
  data_matches |>
  dplyr::rename(
    team = "visitor",
    opponent = "home",
    goals_against = "goals_home",
    goals_for = "goals_visitor"
  ) |>
  dplyr::mutate(at_home = .env$at_home)

Result

  • we have some contrived code
  • R CMD CHECK will be happy.

2.3.3 Splice !!!

No files to be templated in.

Tasks

  1. in make_teams_matches():
# do this also with teams_matches_home
teams_matches_visitor <-
  data_matches |>
  dplyr::rename(!!!rename_visitor()) |>
  dplyr::mutate(at_home = .env$at_home)

Result

  • we have cleaner code
  • R CMD CHECK will be happy.

2.3.4 Naming new columns

No files to be templated in.

Tasks

  1. usethis::use_import_from("rlang", ":=")
  2. in make_teams_matches():
name_of_at_home <- "at_home"
at_home <- FALSE
teams_matches_visitor <-
  data_matches |>
  dplyr::rename(!!!rename_visitor()) |>
  dplyr::mutate("{name_of_at_home}" := .env$at_home)

Result

  • we have some very contrived code
  • R CMD CHECK will be happy.

2.3.5 across()

No files to be templated in.

Tasks

In make_teams_matches():

  1. discuss use of dplyr::select() and dplyr::all_of()
  2. add dplyr::arrange() step
  3. rerun tests
  4. accept change in snapshot
result <-
  teams_matches_home |>
  dplyr::bind_rows(teams_matches_visitor) |>
  dplyr::select(dplyr::all_of(cols_teams_matches())) |>
  dplyr::arrange(
    dplyr::across(
      dplyr::all_of(
        c("country", "tier", "season", "team", "date")
      )
    )
  )

Result

  • We get
  • R CMD CHECK will be happy.

2.3.6 Exercise

Template in files:

  • R/points.R
  • R/seasons.R
  • tests/testthat/test-points.R
  • tests/testthat/test-seasons.R

Tasks

  1. walk through code, emphasizing use of function as an argument
  2. add new function, uss_make_seasons_final():
  • add minimal roxygen at top
  • change mutate() to summarise()
  • add treatment for date
  • change cumsum() to sum()

Result

  • we have three new exported functions: uss_points(), uss_make_seasons_cumulative(), uss_make_seasons_final()
  • R CMD CHECK will be happy.

2.3.7 Curly-Curly

Template in files:

  • R/plot.R
  • R/snapshot.R
  • tests/testthat/test-plot.R

Tasks

  1. usethis::use_package("ggplot2")
  2. Walk through code, esp. snapshot code.
  3. Add argument aes_y = .data$wins to uss_plot_seasons_tiers(), use {{}} to handle.
  4. Add documentation #' @param aes_y ..., example.
  5. Uncomment tests.
  6. time permitting
    1. change one of the colors ever-so-slightly
    2. run the snapshot tests

Result

  • we can use an expression to specify the y-aesthetic
  • R CMD CHECK will be happy.
Clone this wiki locally