1313# ' in seconds.
1414load_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() %> %
0 commit comments