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
6 changes: 3 additions & 3 deletions R/load_driver_telemetry.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@
}

if (laps == "fastest") {
reticulate::py_run_string(glue::glue("tel = session.laps.pick_driver('{driver}').pick_fastest().get_telemetry().add_distance().add_driver_ahead()",
reticulate::py_run_string(glue::glue("tel = session.laps.pick_drivers('{driver}').pick_fastest().get_telemetry().add_distance().add_driver_ahead()",

Check warning on line 71 in R/load_driver_telemetry.R

View check run for this annotation

Codecov / codecov/patch

R/load_driver_telemetry.R#L71

Added line #L71 was not covered by tests
driver = driver
))
} else if (laps != "all") {
reticulate::py_run_string(glue::glue("tel = session.laps.pick_driver('{driver}').pick_lap({laps}).get_telemetry().add_distance().add_driver_ahead()",
reticulate::py_run_string(glue::glue("tel = session.laps.pick_drivers('{driver}').pick_lap({laps}).get_telemetry().add_distance().add_driver_ahead()",

Check warning on line 75 in R/load_driver_telemetry.R

View check run for this annotation

Codecov / codecov/patch

R/load_driver_telemetry.R#L75

Added line #L75 was not covered by tests
driver = driver, laps = laps
))
} else {
reticulate::py_run_string(glue::glue("tel = session.laps.pick_driver('{driver}').get_telemetry().add_distance().add_driver_ahead()",
reticulate::py_run_string(glue::glue("tel = session.laps.pick_drivers('{driver}').get_telemetry().add_distance().add_driver_ahead()",

Check warning on line 79 in R/load_driver_telemetry.R

View check run for this annotation

Codecov / codecov/patch

R/load_driver_telemetry.R#L79

Added line #L79 was not covered by tests
driver = driver
))
}
Expand Down
73 changes: 51 additions & 22 deletions R/load_sprint.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
#' or NULL if no sprint exists for this season/round combo
load_sprint <- function(season = get_current_season(), round = "last") {
if (season != "current" && (season < 2021 || season > get_current_season())) {
cli::cli_abort('{.var season} must be between 2021 and {get_current_season()} (or use "current")')
cli::cli_abort(
'{.var season} must be between 2021 and {get_current_season()} (or use "current")'
)
}

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

data <- get_jolpica_content(url)
Expand All @@ -29,38 +33,63 @@ load_sprint <- function(season = get_current_season(), round = "last") {
}

if (length(data$MRData$RaceTable$Races) == 0) {
cli::cli_alert_warning(glue::glue("No Sprint data for season = {season}, round = {round}",
season = season, round = round
cli::cli_alert_warning(glue::glue(
"No Sprint data for season = {season}, round = {round}",
season = season,
round = round
))
return(NULL)
}

data <- data$MRData$RaceTable$Races$SprintResults[[1]]

data %>%
data <- data %>%
tidyr::unnest(
cols = c("Driver", "Constructor", "Time", "FastestLap"),
names_repair = "universal"
) %>%
tidyr::unnest(
cols = c("Time"),
names_repair = "universal"
) %>%
suppressWarnings() %>%
suppressMessages() %>%
dplyr::select(
"driverId",
"constructorId",
"points",
"position",
"grid",
"laps",
"status",
"position",
gap = "time...21",
"lap",
fastest = "time...23"
) %>%
)

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 %>%
dplyr::mutate(time_sec = time_to_sec(.data$fastest)) %>%
tibble::as_tibble() %>%
janitor::clean_names()
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-load_sprint.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ test_that("load_sprint works", {
expect_equal(sprint_2021_10$driver_id[3], "bottas")
expect_equal(sprint_2021_10$position[1], "1")

sprint_2025_2 <- load_sprint(2025, 2)

expect_equal(nrow(sprint_2025_2), 20)
expect_equal(sprint_2025_2$driver_id[1], "hamilton")
expect_equal(sprint_2025_2$position[1], "1")

expect_error(load_sprint(3050, 2), "`season` must be between 2021 and *")

# A sprint doesn't exist for season = 2021, round = 11
Expand Down