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.
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.
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_un = "your username"
api_pw = "your password"
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.
library(help=cdAFLAPI)
Running the command cdAFLAPI:: will pop out a list as shown below:
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.
cdAFLAPI::getFixture() %>%
select(season.id, round.code, match.id, home.name , away.name)
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​
From package version 1.4.9
onwards, you can run the cdAFLAPI.update()
function to automatically install & update your current version of the R package. It is recommended that you restart your R session after doing so, and if you are using Rstudio, the function will prompt you to do so automatically.
If for some reason cdAFLAPI.update()
does not work or you wish to install the package manually, you can run the following lines of code:
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 running the function cdAFLAPI.version()
(from package version 1.4.9
onwards) to get a printout of the version string, you can also run cdAFLAPI.version(info = "details")
to get printout of package-related metadata.
You can also achieve this by manually inspecting the packages DESCRIPTION file. 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.