Skip to content

Commit 0282790

Browse files
authored
Update column unnesting (#285)
* Avoid pick_driver deprecation fixes #282 * Update load_sprint to handle Jolpica changes fixes #281 * Update load_sprint testing to check both variations * Use `names_sep` in `tidyr::unnest()` * Style & Document code
1 parent bcda8b7 commit 0282790

4 files changed

Lines changed: 85 additions & 61 deletions

File tree

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
^CRAN-SUBMISSION$
2020
^\w*.pem$
2121
^revdep$
22+
cobertura.xml

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ docs
1515
.RData
1616
/revdep/
1717
Thumbs.db
18+
cobertura.xml

R/load_results.R

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
#' in seconds.
1414
load_results <- function(season = get_current_season(), round = "last") {
1515
if (season != "current" && (season < 1950 || season > get_current_season())) {
16-
cli::cli_abort('{.var season} must be between 1950 and {get_current_season()} (or use "current")')
16+
cli::cli_abort(
17+
'{.var season} must be between 1950 and {get_current_season()} (or use "current")'
18+
)
1719
}
1820

19-
url <- glue::glue("{season}/{round}/results.json?limit=40",
20-
season = season, round = round
21+
url <- glue::glue(
22+
"{season}/{round}/results.json?limit=40",
23+
season = season,
24+
round = round
2125
)
2226
data <- get_jolpica_content(url)
2327

@@ -31,10 +35,23 @@ load_results <- function(season = get_current_season(), round = "last") {
3135
# all races from before 2004 will have no 'Fastest Lap' column,
3236
# but also 2021 round 12 (Belgian GP) where no racing laps were run
3337
data %>%
34-
tidyr::unnest(cols = c("Driver", "Time", "Constructor"), names_repair = "universal") %>%
38+
tidyr::unnest(
39+
cols = c("Driver", "Time", "Constructor"),
40+
names_sep = ".",
41+
names_repair = "universal"
42+
) %>%
3543
suppressWarnings() %>%
3644
suppressMessages() %>%
37-
dplyr::select("driverId", "constructorId", "position", "points", "grid":"status", gap = "time") %>%
45+
dplyr::select(
46+
"driverId" = "Driver.driverId",
47+
"constructorId" = "Constructor.constructorId",
48+
"points",
49+
"position",
50+
"grid",
51+
"laps",
52+
"status",
53+
gap = "Time.time"
54+
) %>%
3855
dplyr::mutate(
3956
fastest_rank = NA_integer_,
4057
fastest = NA_character_,
@@ -45,35 +62,61 @@ load_results <- function(season = get_current_season(), round = "last") {
4562
janitor::clean_names()
4663
} else if (!("AverageSpeed" %in% colnames(data$FastestLap))) {
4764
data %>%
48-
tidyr::unnest(cols = c("Driver", "Constructor", "Time", "FastestLap"), names_repair = "universal") %>%
49-
dplyr::select("driverId", "constructorId", "points", "position", "grid", "laps", "status", "time", "Time", "rank") %>%
50-
tidyr::unnest(cols = "Time", names_repair = "universal") %>%
65+
tidyr::unnest(
66+
cols = c("Driver", "Constructor", "Time", "FastestLap"),
67+
names_sep = ".",
68+
names_repair = "universal"
69+
) %>%
70+
tidyr::unnest(
71+
cols = "FastestLap.Time",
72+
names_sep = ".",
73+
names_repair = "universal"
74+
) %>%
5175
suppressWarnings() %>%
5276
suppressMessages() %>%
5377
dplyr::mutate(top_speed_kph = NA_real_) %>%
54-
dplyr::select("driverId", "constructorId", "points", "position", "grid", "laps", "status", gap = "time...8", fastest_rank = "rank", fastest = "time...9", top_speed_kph) %>%
78+
dplyr::select(
79+
"driverId" = "Driver.driverId",
80+
"constructorId" = "Constructor.constructorId",
81+
"points",
82+
"position",
83+
"grid",
84+
"laps",
85+
"status",
86+
gap = "Time.time",
87+
fastest_rank = "FastestLap.rank",
88+
fastest = "FastestLap.Time.time",
89+
top_speed_kph
90+
) %>%
5591
dplyr::mutate(time_sec = time_to_sec(.data$fastest)) %>%
5692
tibble::as_tibble() %>%
5793
janitor::clean_names()
5894
} else {
5995
data %>%
60-
tidyr::unnest(cols = c("Driver", "Constructor", "Time", "FastestLap"), names_repair = "universal") %>%
61-
dplyr::select("driverId", "points", "position", "grid":"AverageSpeed", "constructorId", "name") %>%
6296
tidyr::unnest(
63-
cols = c("Time", "AverageSpeed"),
97+
cols = c("Driver", "Constructor", "Time", "FastestLap"),
98+
names_sep = ".",
99+
names_repair = "universal"
100+
) %>%
101+
tidyr::unnest(
102+
cols = c("FastestLap.Time", "FastestLap.AverageSpeed"),
103+
names_sep = ".",
64104
names_repair = "universal"
65105
) %>%
66106
suppressWarnings() %>%
67107
suppressMessages() %>%
68108
dplyr::select(
69-
"driverId",
70-
"constructorId",
71-
"points":"status",
72-
gap = "time...8",
73-
fastest_rank = "rank",
109+
"driverId" = "Driver.driverId",
110+
"constructorId" = "Constructor.constructorId",
111+
"points",
112+
"position",
113+
"grid",
74114
"laps",
75-
fastest = "time...11",
76-
top_speed_kph = "speed",
115+
"status",
116+
gap = "Time.time",
117+
fastest_rank = "FastestLap.rank",
118+
fastest = "FastestLap.Time.time",
119+
top_speed_kph = "FastestLap.AverageSpeed.speed",
77120
) %>%
78121
dplyr::mutate(time_sec = time_to_sec(.data$fastest)) %>%
79122
tibble::as_tibble() %>%

R/load_sprint.R

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -46,50 +46,29 @@ load_sprint <- function(season = get_current_season(), round = "last") {
4646
data <- data %>%
4747
tidyr::unnest(
4848
cols = c("Driver", "Constructor", "Time", "FastestLap"),
49-
names_repair = "universal"
49+
names_repair = "universal",
50+
names_sep = "."
5051
) %>%
5152
tidyr::unnest(
52-
cols = c("Time"),
53-
names_repair = "universal"
54-
)
55-
56-
if ("time...24" %in% names(data)) {
57-
data <- data %>%
58-
suppressWarnings() %>%
59-
suppressMessages() %>%
60-
dplyr::select(
61-
"driverId",
62-
"constructorId",
63-
"points",
64-
"position",
65-
"grid",
66-
"laps",
67-
"status",
68-
"position",
69-
gap = "time...21",
70-
"lap",
71-
fastest = "time...24"
72-
)
73-
} else {
74-
data <- data %>%
75-
suppressWarnings() %>%
76-
suppressMessages() %>%
77-
dplyr::select(
78-
"driverId",
79-
"constructorId",
80-
"points",
81-
"position",
82-
"grid",
83-
"laps",
84-
"status",
85-
"position",
86-
gap = "time...21",
87-
"lap",
88-
fastest = "time...23"
89-
)
90-
}
91-
92-
data %>%
53+
cols = c("FastestLap.Time"),
54+
names_repair = "universal",
55+
names_sep = "."
56+
) %>%
57+
suppressWarnings() %>%
58+
suppressMessages() %>%
59+
dplyr::select(
60+
"driverId" = "Driver.driverId",
61+
"constructorId" = "Constructor.constructorId",
62+
"points",
63+
"position",
64+
"grid",
65+
"laps",
66+
"status",
67+
"position",
68+
"gap" = "Time.time",
69+
"lap" = "FastestLap.lap",
70+
"fastest" = "FastestLap.Time.time"
71+
) %>%
9372
dplyr::mutate(time_sec = time_to_sec(.data$fastest)) %>%
9473
tibble::as_tibble() %>%
9574
janitor::clean_names()

0 commit comments

Comments
 (0)