Skip to content

Instantly share code, notes, and snippets.

View tonyelhabr's full-sized avatar
🏠
Working from home

Tony ElHabr tonyelhabr

🏠
Working from home
View GitHub Profile
@tonyelhabr
tonyelhabr / print-for-chunks.md
Last active April 6, 2024 14:34
Replicate reprex printing for arbitrary variables

This is useful if you simply want to copy-paste some code output without having to run code (or render/knit a whole document).

Method 1

## Based on internals of reprex:::reprex_impl
print_df_for_chunk <- function(code_string) {
  reprex_document_options <- list(
    venue = 'gh', 
@tonyelhabr
tonyelhabr / scrape-places.md
Last active March 11, 2024 13:14
Scraping Google Places API

Setup.

library(googleway)
library(dplyr)
library(tibble)

DATA_DIR <- "path/to/dir"
dir.create(DATA_DIR, showWarnings = FALSE, recursive = TRUE)
@tonyelhabr
tonyelhabr / scrape-big5-team-logos.md
Created January 26, 2024 13:59
Get Big 5 team logos from FBref
library(rvest)
library(tibble)

url <- 'https://fbref.com/en/comps/Big5/Big-5-European-Leagues-Stats'
page <- read_html(url)

team_elements <- page |> 
  html_elements('table') |> 
@tonyelhabr
tonyelhabr / delete-gha-runs.md
Created January 8, 2024 01:06
Delete GitHub action run logs
library(gh)
library(purrr)

token <- Sys.getenv("GITHUB_PAT")
REPO <- "my-repo"
OWNER <- "me"

runs <- gh::gh(
 "GET /repos/:owner/:repo/actions/runs",
@tonyelhabr
tonyelhabr / shots.md
Created September 14, 2023 11:30
shot x-y data

Full set of fotmob shot releases here: https://github.com/JaseZiv/worldfootballR_data/releases/tag/fotmob_match_details

raw_shots <- readr::read_csv('https://github.com/JaseZiv/worldfootballR_data/releases/download/fotmob_match_details/47_match_details.csv')
#> Rows: 28667 Columns: 43
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (18): league_name, league_round_name, parent_league_season, match_time_u...
#> dbl (22): match_id, match_round, league_id, parent_league_id, home_team_id, ...
@tonyelhabr
tonyelhabr / pull-2023-fifa-womens-world-cup-xg.md
Created August 27, 2023 22:20
Pull xG data from StatsBomb and Opta (via FBRef) for the 2023 FIFA Women's World Cup

Raw data pull

library(StatsBombR)
library(worldfootballR)
library(dplyr)
library(janitor)
library(tibble)
@tonyelhabr
tonyelhabr / scrape-2023-fifa-womens-world-cup.md
Last active July 21, 2023 19:02
Scrape player stats for the 2023 FIFA Women's World Cup
library(httr)
library(tibble)
library(tidyr)
library(dplyr)
library(purrr)
library(janitor)
library(cli)
library(stringr)
@tonyelhabr
tonyelhabr / ff_scores.csv
Last active July 2, 2023 22:34
Scrape ESPN fantasy football league scores (2018/19-2022/23)
season week user_name opponent_user_name user_score opponent_score result
2018 1 Juan Avalos Manuel Espinosa 137.84 86.26 W
2018 1 Manuel Espinosa Juan Avalos 86.26 137.84 L
2018 1 Enrique Rodriguez Tracy Krohn 123.14 121.78 W
2018 1 Steven Valencia Juan Pineda 134.54 86.2 W
2018 1 Andrew ElHabr Andrew Lara 106.26 102.06 W
2018 1 Drake Hernandez Tony ElHabr 131.04 123.42 W
2018 1 Tony ElHabr Drake Hernandez 123.42 131.04 L
2018 1 Andrew Lara Andrew ElHabr 102.06 106.26 L
2018 1 Tracy Krohn Enrique Rodriguez 121.78 123.14 L
@tonyelhabr
tonyelhabr / overperformance.md
Last active May 30, 2023 12:21
Emperical bayes estimation of xG overperformance

This is my attempt to replicate the analysis by Laurie Shaw here.

Creating some fake data.

library(tibble)
library(purrr)
library(tidyr)
library(MASS)
library(ggplot2)
@tonyelhabr
tonyelhabr / latest-opta-club-rankings.md
Created April 30, 2023 17:28
Get latest opta club rankings from release.
library(readr)
library(dplyr)
opta_club_rankings <- read_csv('https://github.com/tonyelhabr/club-rankings/releases/download/club-rankings/opta-club-rankings.csv')
opta_club_rankings |> 
  filter(updated_at == max(updated_at))
#> # A tibble: 3,000 × 7
#>     rank team              rating `ranking change 7 days` date       updated_at          id                       
#>    <dbl> <chr>              <dbl>                   <dbl> <date>     <dttm>              <chr>