-
Notifications
You must be signed in to change notification settings - Fork 5
/
README.Rmd
170 lines (126 loc) · 6.6 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
---
output: github_document
html_document:
includes:
in_header: GAfile.html
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
dpi = 250,
dev = "png",
fig.width = 6,
fig.height = 4
)
library(tibble)
options(
tibble.max_extra_cols = 10,
tibble.bold = TRUE,
tibble.print_max = 5,
pillar.min_title_width = 5
)
library(f1dataR)
withr::local_timezone("UTC")
```
# f1dataR <img src='man/figures/logo.png' align="right" width="25%" min-width="120px"/>
An R package to access Formula 1 Data from the Jolpica API (formerly Ergast) and the official F1 data stream via the FastF1 Python library.
<!-- badges: start -->
[![R-CMD-check](https://github.com/SCasanova/f1dataR/actions/workflows/check-standard.yaml/badge.svg)](https://github.com/SCasanova/f1dataR/actions/workflows/check-standard.yaml)
[![test-coverage](https://github.com/SCasanova/f1dataR/actions/workflows/test-coverage.yaml/badge.svg)](https://github.com/SCasanova/f1dataR/actions/workflows/test-coverage.yaml)
[![Codecov test coverage](https://img.shields.io/codecov/c/github/SCasanova/f1dataR?label=codecov&logo=codecov)](https://app.codecov.io/gh/SCasanova/f1dataR?branch=main)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
[![CRAN status](https://www.r-pkg.org/badges/version/f1dataR)](https://CRAN.R-project.org/package=f1dataR)
[![CRAN downloads](https://cranlogs.r-pkg.org/badges/grand-total/f1dataR)](https://CRAN.R-project.org/package=f1dataR)
<!--badges: end -->
## Installation
Install the stable version from CRAN:
```{r eval = FALSE}
install.packages("f1dataR")
```
or install the development version from GitHub:
```{r eval = FALSE}
if (!require("remotes")) install.packages("remotes")
remotes::install_github("SCasanova/f1dataR")
library(f1dataR)
```
## Data Sources
Data is pulled from:
* [Jolpica F1 API](https://api.jolpi.ca/ergast/)
* [F1 Data Stream](https://www.formula1.com/en/timing/f1-live) via the [Fast F1 python library](https://docs.fastf1.dev/index.html)
Note the Ergast Motor Racing Database API will be shutting down at the end of 2024. A new data source (Jolpica-F1 project) was identified and implemented.
## Functions
### Load Lap Times
> `load_laps(season = "current", race = "last")`
This function loads lap-by-lap time data for all drivers in a given season
and round. Round refers to race number. The defaults are current season and last race. Lap data is limited to 1996-present.
**Example:**
```{r load_laps}
load_laps()
```
or
```{r load_specific_laps}
load_laps(season = 2021, round = 15)
```
### Driver Telemetry
> `load_driver_telemetry(season = "current", race = "last", session = "R", driver, laps = "all")`
When the parameters for season (four digit year), round (number or GP name), session (FP1. FP2, FP3, Q, S, SS, or R), and driver code (three letter code) are entered, the function will load all data for a session and the pull the info for the selected driver. The first time a session is called, loading times will be relatively long but in subsequent calls this will improve to only a couple of seconds
```{r load_telemetries}
load_driver_telemetry(season = 2022, round = 4, driver = "PER")
load_driver_telemetry(season = 2018, round = 7, "Q", "HAM", laps = "fastest")
```
### Lap-by-Lap information
> `load_session_laps(season = "current", race = "last", session = "R", add_weather = FALSE)`
This function will give us detailed information of lap and sector times, tyres, weather (optional), and more for every lap of the GP and driver.
```{r load_session_laps}
load_session_laps(season = 2023, round = 4, add_weather = TRUE)
```
### Circuit Data
> `load_circuit_details(2023, 4)`
This function loads circuit details for a specific race session. Note that different track layouts are used at some circuits depending on the year of the race. Useful for visualizing or annotating data. Contains information on corners, marshal_lights and marshal_sectors.
### Plotting
> `plot_fastest(season = "current", round = "last", session = "R", driver, color = "gear")`
A built in plotting function that plots the circuit and a driver's fastest laps' `speed` or `gear` exists.
```{r plot, eval=require('ggplot2', quietly = TRUE), message=FALSE, warning=FALSE}
plot_fastest(season = 2023, round = 1, session = "R", driver = "VER", color = "gear")
```
Two helper functions exist as well. The first, `theme_dark_f1()` assists with colour schemes similar to that used in other F1 graphics. The second, `correct_track_ratio()` is a function that fixes track ratio issues that appear when you create images similar to that above from `plot_fastest()`. Please refer to their documentation for usage.
### Metadata Lookups
The package echos the metadata information look-up from the FastF1 package. this is a convenient way to programmatically look up drivers, teams, driver-team relationships, team colors, driver colors, tire types & colors and more. See the following functions for this look-up:
* `get_driver_abbreviation()`
* `get_driver_color()`
* `get_driver_color_map()`
* `get_driver_colour()`
* `get_driver_colour_map()`
* `get_driver_name()`
* `get_driver_style()`
* `get_driver_telemetry()`
* `get_drivers_by_team()`
* `get_session_drivers_and_teams()`
* `get_team_by_driver()`
* `get_team_color()`
* `get_team_colour()`
* `get_team_name()`
* `get_tire_compounds()`
Note that (in support of plotting functions) driver colors and marker type / line style can be retrieved from `get_driver_style()`. The function `get_driver_color()` will return the same color value for both drivers in a team.
### Cache information
The cache directory for sessions can be set manually with the options function
```{r cache_example, eval = FALSE}
options(f1dataR.cache = "path/to/directory")
```
### Other functions
Many other functions exist, and are flexible enough to call the current `season` with the string `"current"` or use the year as a numeric value. Similarly, `round` can be `"last"` or a round number (from 1 to the total number of races in a season).
* `load_constructors()`
* `load_drivers(season = "current")`
* `load_circuits(season = "current")`
* `load_pitstops(season = "current", round = "last")`
* `load_quali(season = "current", round = "last")`
* `load_results(season = "current", round = "last")`
* `load_schedule(season = ``r get_current_season()``)`
* `load_sprint(season = "current", round = "last")`
* `load_standings(season = "current", round = "last", type = c("driver", "constructor"))`
### Clear F1 Cache
> `clear_f1_cache()`
Clears the cache for all functions in the package.