not_cran = identical(Sys.getenv("NOT_CRAN"), "true")
online <- !is.null(curl::nslookup("r-project.org", error = FALSE))
eval_param <- not_cran & online

knitr::opts_chunk$set(
    eval = eval_param,
    message = FALSE,
    warning = FALSE,
    collapse = TRUE,
    comment = "#>"
)

library(fitzRoy)
library(dplyr)

The package provides easy access to AFLW data from the AFL.com.au website. This can be accessed via the normal fetch_ functions in the same way you would access Men's data.

This vignette talks through the main data sources. To fully understand how the fetch_ functions work - please read the Main Fetch Functions vignette.

Fixture

Firstly, we can return the fixture for a season or particular round.

not_cran
online
eval_param
fetch_fixture(season = 2021, comp = "AFLW") %>%
  select(compSeason.name, round.name, 
         home.team.name, away.team.name, 
         venue.name)
fitzRoy:::fixture_afl_aflw_2021 %>%
  select(compSeason.name, round.name, 
         home.team.name, away.team.name, 
         venue.name)

Lineup

We can get the lineup for a given set of matches in a particular round.

fetch_lineup(2021, round_number = 1, comp = "AFLW") %>%
  select(round.name, status, teamName, 
         player.playerName.givenName,
         player.playerName.surname, teamStatus)
fitzRoy:::lineup_aflw_2021_1 %>%
  select(round.name, status, teamName, 
         player.playerName.givenName,
         player.playerName.surname, teamStatus)

Results

The match results, including the teams playing, venue information and final scores are returned via fetch_results.

fetch_results(2020, round_number = 1, comp = "AFLW") %>%
  select( match.date, match.name,
         homeTeamScore.matchScore.totalScore, awayTeamScore.matchScore.totalScore)
fitzRoy:::results_afl_aflw_2020 %>%
  dplyr::filter(round.roundNumber == 1) %>%
  select(match.date, match.name,
         homeTeamScore.matchScore.totalScore,
         awayTeamScore.matchScore.totalScore)

Ladder

We can also get the ladder at any point with fetch_ladder.

fetch_ladder(2020, round_number = 6, comp = "AFLW") %>%
  select(season, round_name, 
         position, team.name, played, 
         pointsFor, pointsAgainst)
fitzRoy:::ladder_afl_aflw_2020 %>%
  dplyr::filter(round_number == 7) %>%
  select(season, round_name, 
         position, team.name, played, 
         pointsFor, pointsAgainst)

Stats

Lastly - we have basic player stats. This will return player level statistics such as possessions, time on ground, fantasy points and a myriad of other statistics.

fetch_player_stats(2020, round_number = 1, comp = "AFLW") %>%
  select(player.player.player.givenName:clearances.totalClearances)
fitzRoy:::stats_afl_aflw_2020 %>%
  dplyr::filter(round.roundNumber == 1) %>%
  dplyr::select(player.player.player.givenName:clearances.totalClearances)

Player Details

We can return player details such as data of birth and listed height and weight.

details_aflw <- fetch_player_details(team = "Western Bulldogs", current = TRUE, comp = "AFLW", source = "AFL")

head(details_aflw)
details_aflw <- fitzRoy:::details_aflw
head(details_aflw)

Coaches Votes

We can also return the coaches votes for a particular season, team or round.

fetch_coaches_votes(season = 2021, round_number = 9, comp = "AFLW", team = "Western Bulldogs")
fitzRoy:::aflw_coaches_votes %>%
  dplyr::filter(Home.Team=="Western Bulldogs" & Round == 9) %>% 
  head()

Legacy/Advanced Stats

We have a legacy function to provide advanced AFLW stats. This is going to be deprecated in favour of a more robust solution but still works for now. The following code should show you how to use those functions.

Match data

A good thing to check is that the cookie is working. Often this gets changed or moved and without it, the code won't work.

cookie <- get_afl_cookie()
print(cookie)
cookie <- fitzRoy:::cookie
print(cookie)

Note - if this is NULL the rest of this Vignette won't show any outputs but the code will remain!

if (is.null(cookie)) {
    eval_param = FALSE
  }

We can use the fetch_results() function to retrieve match data matches.

match_data <- fetch_results(2020, round_number = 1, comp = "AFLW")
match_data <- fitzRoy:::results_afl_aflw_2020 %>%
  dplyr::filter(round.roundNumber == 1)

Note that there will be warnings if a fixture is available but no match data has been added yet. If this is the case, make sure you don't try to request detailed match stats for these match IDs.

glimpse(match_data)

Detailed stats

The get_aflw_detailed_data() can be used to return more detailed data than the match data shown above. It takes a vector of match IDs as an argument. For example, let's say we want detailed stats for the first 10 games in match_data above. Then we would do:

first10 <- head(match_data, 10)
first10_ids <- first10$Match.Id
first10_ids
detailed <- get_aflw_detailed_data(first10_ids)
glimpse(detailed)
fitzRoy:::detailed_stats_aflw_2020 %>% glimpse()


jimmyday12/fitzRoy documentation built on March 26, 2024, 8:56 p.m.