Install package
# devtools::install_github("StatisticsNZ/open-data-api/R.NZSAPI")
library(R.NZSAPI)
Set API key as system variable in ~/.Renviron file variable must be named "nz_stat_api_key"
nz_stat_api_key=xxxxxxxxxxxxxxxxxxxxxxxx
Find available datasets
(stat_cat <- get_odata_catalogue(slim_df=TRUE))
title description endpoint
<chr> <chr> <chr>
1 Employment Indicators This employment indicator... EmploymentIndicators
2 Overseas Cargo Overseas cargo records all... OverseasCargo
3 Covid19 Indicators Stats NZ's COVID-19 data... Covid-19Indicators
4 International Migra... International migration mea... InternationalMigration
5 Household Labour Fo... Statistics New Zealand’s q... HouseholdLabourForceSurvey
6 2018 Census of Pop... Example aggregates from the... 2018Census-PopulationDwellings
7 Overseas Merchandis... Overseas Merchandise Trade... OverseasMerchandiseTrade
8 International Travel International travel covers... InternationalTravel
9 National Accounts The conceptual framework use... NationalAccounts
Using an endpoint from stat_cat to obtain data observations
stat_df <- get_odata(endpoint = stat_cat$endpoint[1])
An example using the query_option argument
stat_df <- get_odata(
endpoint = stat_cat$endpoint[1]
, query_option = "
$filter=(Label2 eq 'Actual' and Duration eq 'P1M')
&$select=Label1,Label2,Unit,Measure,Value
&$apply=groupby((Label1,Label2,Unit,Measure))
&$top=10
"
)
query_option sends a SQL like argument to the API for finer control on data returned.
Get available meta-data (entity) options for each endpoint
stat_ent <- get_odata_entities()
Get meta-data on an endpoint
endpoint_metadata <- get_odata(endpoint = stat_cat$endpoint[1], entity = stat_ent$name[1])