Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
^CRAN-SUBMISSION$
^\w*.pem$
^revdep$
cobertura.xml
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ docs
.RData
/revdep/
Thumbs.db
cobertura.xml
81 changes: 62 additions & 19 deletions R/load_results.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
#' in seconds.
load_results <- function(season = get_current_season(), round = "last") {
if (season != "current" && (season < 1950 || season > get_current_season())) {
cli::cli_abort('{.var season} must be between 1950 and {get_current_season()} (or use "current")')
cli::cli_abort(
'{.var season} must be between 1950 and {get_current_season()} (or use "current")'
)
}

url <- glue::glue("{season}/{round}/results.json?limit=40",
season = season, round = round
url <- glue::glue(
"{season}/{round}/results.json?limit=40",
season = season,
round = round
)
data <- get_jolpica_content(url)

Expand All @@ -31,10 +35,23 @@ load_results <- function(season = get_current_season(), round = "last") {
# all races from before 2004 will have no 'Fastest Lap' column,
# but also 2021 round 12 (Belgian GP) where no racing laps were run
data %>%
tidyr::unnest(cols = c("Driver", "Time", "Constructor"), names_repair = "universal") %>%
tidyr::unnest(
cols = c("Driver", "Time", "Constructor"),
names_sep = ".",
names_repair = "universal"
) %>%
suppressWarnings() %>%
suppressMessages() %>%
dplyr::select("driverId", "constructorId", "position", "points", "grid":"status", gap = "time") %>%
dplyr::select(
"driverId" = "Driver.driverId",
"constructorId" = "Constructor.constructorId",
"points",
"position",
"grid",
"laps",
"status",
gap = "Time.time"
) %>%
dplyr::mutate(
fastest_rank = NA_integer_,
fastest = NA_character_,
Expand All @@ -45,35 +62,61 @@ load_results <- function(season = get_current_season(), round = "last") {
janitor::clean_names()
} else if (!("AverageSpeed" %in% colnames(data$FastestLap))) {
data %>%
tidyr::unnest(cols = c("Driver", "Constructor", "Time", "FastestLap"), names_repair = "universal") %>%
dplyr::select("driverId", "constructorId", "points", "position", "grid", "laps", "status", "time", "Time", "rank") %>%
tidyr::unnest(cols = "Time", names_repair = "universal") %>%
tidyr::unnest(
cols = c("Driver", "Constructor", "Time", "FastestLap"),
names_sep = ".",
names_repair = "universal"
) %>%
tidyr::unnest(
cols = "FastestLap.Time",
names_sep = ".",
names_repair = "universal"
) %>%
suppressWarnings() %>%
suppressMessages() %>%
dplyr::mutate(top_speed_kph = NA_real_) %>%
dplyr::select("driverId", "constructorId", "points", "position", "grid", "laps", "status", gap = "time...8", fastest_rank = "rank", fastest = "time...9", top_speed_kph) %>%
dplyr::select(
"driverId" = "Driver.driverId",
"constructorId" = "Constructor.constructorId",
"points",
"position",
"grid",
"laps",
"status",
gap = "Time.time",
fastest_rank = "FastestLap.rank",
fastest = "FastestLap.Time.time",
top_speed_kph
) %>%
dplyr::mutate(time_sec = time_to_sec(.data$fastest)) %>%
tibble::as_tibble() %>%
janitor::clean_names()
} else {
data %>%
tidyr::unnest(cols = c("Driver", "Constructor", "Time", "FastestLap"), names_repair = "universal") %>%
dplyr::select("driverId", "points", "position", "grid":"AverageSpeed", "constructorId", "name") %>%
tidyr::unnest(
cols = c("Time", "AverageSpeed"),
cols = c("Driver", "Constructor", "Time", "FastestLap"),
names_sep = ".",
names_repair = "universal"
) %>%
tidyr::unnest(
cols = c("FastestLap.Time", "FastestLap.AverageSpeed"),
names_sep = ".",
names_repair = "universal"
) %>%
suppressWarnings() %>%
suppressMessages() %>%
dplyr::select(
"driverId",
"constructorId",
"points":"status",
gap = "time...8",
fastest_rank = "rank",
"driverId" = "Driver.driverId",
"constructorId" = "Constructor.constructorId",
"points",
"position",
"grid",
"laps",
fastest = "time...11",
top_speed_kph = "speed",
"status",
gap = "Time.time",
fastest_rank = "FastestLap.rank",
fastest = "FastestLap.Time.time",
top_speed_kph = "FastestLap.AverageSpeed.speed",
) %>%
dplyr::mutate(time_sec = time_to_sec(.data$fastest)) %>%
tibble::as_tibble() %>%
Expand Down
63 changes: 21 additions & 42 deletions R/load_sprint.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,50 +46,29 @@ load_sprint <- function(season = get_current_season(), round = "last") {
data <- data %>%
tidyr::unnest(
cols = c("Driver", "Constructor", "Time", "FastestLap"),
names_repair = "universal"
names_repair = "universal",
names_sep = "."
) %>%
tidyr::unnest(
cols = c("Time"),
names_repair = "universal"
)

if ("time...24" %in% names(data)) {
data <- data %>%
suppressWarnings() %>%
suppressMessages() %>%
dplyr::select(
"driverId",
"constructorId",
"points",
"position",
"grid",
"laps",
"status",
"position",
gap = "time...21",
"lap",
fastest = "time...24"
)
} else {
data <- data %>%
suppressWarnings() %>%
suppressMessages() %>%
dplyr::select(
"driverId",
"constructorId",
"points",
"position",
"grid",
"laps",
"status",
"position",
gap = "time...21",
"lap",
fastest = "time...23"
)
}

data %>%
cols = c("FastestLap.Time"),
names_repair = "universal",
names_sep = "."
) %>%
suppressWarnings() %>%
suppressMessages() %>%
dplyr::select(
"driverId" = "Driver.driverId",
"constructorId" = "Constructor.constructorId",
"points",
"position",
"grid",
"laps",
"status",
"position",
"gap" = "Time.time",
"lap" = "FastestLap.lap",
"fastest" = "FastestLap.Time.time"
) %>%
dplyr::mutate(time_sec = time_to_sec(.data$fastest)) %>%
tibble::as_tibble() %>%
janitor::clean_names()
Expand Down