-
Notifications
You must be signed in to change notification settings - Fork 2
Querying a GraphQL client for linked data using R
Henry Partridge edited this page Oct 19, 2017
·
1 revision
The following script relies on the ghql
R package (Chamberlain 2017) to query multidimensional QB datasets using GraphQL. The example uses the graphql-qb service at graphql-qb.publishmydata.com which stores data from statistics.gov.scot.
devtools::install_github("hadley/devtools")
devtools::install_github("ropensci/ghql")
library(ghql) # for querying
library(jsonlite) # for parsing the json response
library(httr) # for working with URLs
library(tidyverse) # for tidying data
Initialize the GraphQL client by pointing it to the appropriate endpoint e.g. http://graphql-qb.publishmydata.com/graphql
client <- GraphqlClient$new(url = "http://graphql-qb.publishmydata.com/graphql")
No OAuth token is required for this endpoint but the headers
argument can be used for this purpose.
qry <- Query$new()
qry$query('query', '
{
datasets(dimensions: {and: ["http://statistics.gov.scot/def/dimension/gender"]}) {
title
description
}
}
')
responses <- client$exec(qry$queries$query)
df <- as.data.frame(responses)
glimpse(df)
## Observations: 55
## Variables: 2
## $ data.datasets.title <chr> "Mid-Year Population Estimates (hist...
## $ data.datasets.description <chr> "Mid-year estimates by age and gende...
df <- rename(df, Dataset = data.datasets.title,
Description = data.datasets.description)
The table below shows the first 6 responses.
Dataset | Description |
---|---|
Mid-Year Population Estimates (historical geographical boundaries) | Mid-year estimates by age and gender. Higher level geographies are aggregated using 2001 data zones. |
Pupil Attainment | Number of pupils who attained a given number of qualifications by level and stage. |
Earnings | Median gross weekly earnings (£s) by gender and workplace/residence measure. |
Income Support Claimants | Number of income support claimants by age and gender (age split not available for gender). |
Healthy Life Expectancy | Years of Healthy Life Expectancy (including confidence intervals) by gender |
Disability Living Allowance | Number of Disability Living Allowance claimants by age group and gender. |
- Chamberlain, Scott (2017). ghql: General Purpose GraphQL Client. R package version 0.0.3.9110. https://github.com/ropensci/ghql
- Ooms, Jeroen (2014). The jsonlite Package: A Practical and Consistent Mapping Between JSON Data and R Objects. arXiv:1403.2805 [stat.CO] URL https://arxiv.org/abs/1403.2805.
- Wickham, Hadley (2017). httr: Tools for Working with URLs and HTTP. R package version 1.3.1. https://CRAN.R-project.org/package=httr
- Wickham, Hadley (2017). tidyverse: Easily Install and Load 'Tidyverse' Packages. R package version 1.1.1. https://CRAN.R-project.org/package=tidyverse