The Fingertips API was published recently at this link. At first sight it looks rather complex so we are developing scripts in R to facilitate extracting data via the API. The data and meta data are all available in JSON format which is easily handled by R using packages like jsonlite
.
Lets start with a simple example - extracting the age groups contained within the Fingertips tool. All the calls to the API are of the form https://fingertips.phe.org.uk/api/ so to read the data we simply need to pass the URL to the fromJSON
function. So for example the following code reads the IDs and names for all the age bands used in Fingertips.
if(!require(jsonlite))install.packages("jsonlite")
Loading required package: jsonlite
library(jsonlite)
ages <- fromJSON("https://fingertips.phe.org.uk/api/ages")
head(ages, 20)
We can extract the gender codes in a similar fashion
sexes <- fromJSON("https://fingertips.phe.org.uk/api/sexes")
head(sexes)
And see what profiles are contained in the tool
profiles <- fromJSON("https://fingertips.phe.org.uk/api/profiles")
names(profiles)
[1] "Id" "Name" "Key" "GroupIds"
head(profiles, 20)
We can see that within each profiles there are GroupIDs which reflect profile subdomains. For examples, within the PHOF tool which is profile 19, there are 6 domains, or 8 in the Common Mental Disorders Profile.
Domain names can be extracted as follows:
## for mental health profiles
mh <- fromJSON("https://fingertips.phe.org.uk/api/profile?profile_id=40")
mhdomains <- as.vector(mh$GroupIds)
url <- paste0("https://fingertips.phe.org.uk/api/group_metadata?group_ids=", paste0(mhdomains, collapse = "%2C"))
url
[1] "https://fingertips.phe.org.uk/api/group_metadata?group_ids=8000026%2C8000041%2C8000042%2C8000043%2C8000044%2C1938132720%2C1938132954%2C1938132955"
mhdon <- fromJSON(url)
mhdon
Follows a similar pattern.
## First we need to extract area types
areatype <- fromJSON("https://fingertips.phe.org.uk/api/area_types")
areatype
## for LADs Id = 1
areanames <- fromJSON("https://fingertips.phe.org.uk/api/areas/by_area_type?area_type_id=1")
areanames
## First we need to extract area types
subject <- "alcohol"
url = paste0("https://fingertips.phe.org.uk/api/indicator_search?search_text=", subject)
## for LADs Id = 1
indicators <- fromJSON(url)
indicators
named list()