1515# ' or NULL if no sprint exists for this season/round combo
1616load_sprint <- function (season = get_current_season(), round = " last" ) {
1717 if (season != " current" && (season < 2021 || season > get_current_season())) {
18- cli :: cli_abort(' {.var season} must be between 2021 and {get_current_season()} (or use "current")' )
18+ cli :: cli_abort(
19+ ' {.var season} must be between 2021 and {get_current_season()} (or use "current")'
20+ )
1921 }
2022
21- url <- glue :: glue(" {season}/{round}/sprint.json?limit=40" ,
22- season = season , round = round
23+ url <- glue :: glue(
24+ " {season}/{round}/sprint.json?limit=40" ,
25+ season = season ,
26+ round = round
2327 )
2428
2529 data <- get_jolpica_content(url )
@@ -29,38 +33,63 @@ load_sprint <- function(season = get_current_season(), round = "last") {
2933 }
3034
3135 if (length(data $ MRData $ RaceTable $ Races ) == 0 ) {
32- cli :: cli_alert_warning(glue :: glue(" No Sprint data for season = {season}, round = {round}" ,
33- season = season , round = round
36+ cli :: cli_alert_warning(glue :: glue(
37+ " No Sprint data for season = {season}, round = {round}" ,
38+ season = season ,
39+ round = round
3440 ))
3541 return (NULL )
3642 }
3743
3844 data <- data $ MRData $ RaceTable $ Races $ SprintResults [[1 ]]
3945
40- data %> %
46+ data <- data %> %
4147 tidyr :: unnest(
4248 cols = c(" Driver" , " Constructor" , " Time" , " FastestLap" ),
4349 names_repair = " universal"
4450 ) %> %
4551 tidyr :: unnest(
4652 cols = c(" Time" ),
4753 names_repair = " universal"
48- ) %> %
49- suppressWarnings() %> %
50- suppressMessages() %> %
51- dplyr :: select(
52- " driverId" ,
53- " constructorId" ,
54- " points" ,
55- " position" ,
56- " grid" ,
57- " laps" ,
58- " status" ,
59- " position" ,
60- gap = " time...21" ,
61- " lap" ,
62- fastest = " time...23"
63- ) %> %
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 %> %
6493 dplyr :: mutate(time_sec = time_to_sec(.data $ fastest )) %> %
6594 tibble :: as_tibble() %> %
6695 janitor :: clean_names()
0 commit comments