Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/SCasanova/f1dataR

An R package to access Formula 1 Data from the Ergast API and the unofficial F1 data stream via the fastf1 python library.
https://github.com/SCasanova/f1dataR

f1 formula1 r r-package rstats sports-data

Last synced: 5 days ago
JSON representation

An R package to access Formula 1 Data from the Ergast API and the unofficial F1 data stream via the fastf1 python library.

Awesome Lists containing this project

README

        

---
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

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.

[![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)

## 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.