Skip to main content

AFL API R Package

Overview​

The API R Package allows users to connect to the AFL API and retrieve transformed data from its endpoints. The package contains a suite of functions that flatten the API response into an R dataframe for ease of use, visibility & analysis.

note

Authorised access to the API is required.

Install & Access​

The following code snippet will install the cdAFLAPI R Package on your machine and load it into your current R workspace.

Installation
install.packages('http://www.championdata.com/scratch/api/cdAFLAPI.tar.gz',source=TRUE,repos=NULL) 
library(cdAFLAPI)

Credentials​

Your authorised API credentials must be set in your R global environment as text strings under the variable names api_un (username) and api_pw (password).

API Credentials
api_un = "your username"
api_pw = "your password"
note

Your username and password must be assigned to the variables ‘api_un’ and ‘api_pw’.

Help/Details​

Running the below command will provide a brief explainer of the supported function names and what they provide.

Help
library(help=cdAFLAPI)

Running the command cdAFLAPI:: will pop out a list as shown below: R package example
Hovering over a function and pressing F1 (as stated in the bottom of yellow dialogue box) will open helper documentation for that specific function, as well as examples and the required inputs for the function.

Pulling Match IDs​

Many of the helper functions in the Champion Data R package require the input of a matchID. A simple way to get a hold of match ID’s is to call the cdAFLAPI::getFixture() helper function which returns, among other things, all matches with match ID’s for a given season.

Get Match IDs
cdAFLAPI::getFixture() %>% 
select(season.id, round.code, match.id, home.name , away.name)
note

Passing nothing to the cdAFLAPI::getFixture() function will default to returning matches for the current season, however you can supply a year to return matches for that given season ie. cdAFLAPI::getFixture(2022).

Updating Package Version​

To update the package to the most recent version, run the following:

Update Package
detach('package:cdAFLAPI',unload=TRUE)
install.packages('http://www.championdata.com/scratch/api/cdAFLAPI.tar.gz',source=TRUE,repos=NULL)
.rs.restartR()
library(cdAFLAPI)

You can ensure the package version update has been successful by inspecting the packages DESCRIPTION file, which contains details about Champion Data’s AFL API package, including the current version number loaded. To open the DESCIPTION file in R studio, navigate to the ‘Packages’ tab in the viewer pane, locate and click on the cdAFLAPI hyperlink which will take you to the package landing page, then click on the DESCRIPTION file hyperlink.

R studio packages tab R studio help tab