Learn R Programming

onemapsgapi (version 1.1.0)

get_pop_queries: Get Population Data (Multiple)

Description

This function is a wrapper for the Population Query API. It allows for querying of multiple Popquery data types for multiple towns and years.

Usage

get_pop_queries(
  token,
  data_types,
  planning_areas,
  years,
  gender = NULL,
  parallel = FALSE
)

Value

A tibble with each row representing a town in a particular year for a particular gender, and columns with the variables returned by the API endpoint. If any API call returns no data, the values will be NA but the row will be returned. However, if all data_types do not return data for that town and year, no row will be returned for it.

Arguments

token

User's API token. This can be retrieved using get_token

data_types

Type of data to be retrieved, should correspond to one of the API endpoints. E.g. to get economic status data, data_type = "getEconomicStatus". The API endpoints can be found on the documentation page.

planning_areas

Town for which the data should be retrieved.

years

Year for which the data should be retrieved.

gender

Optional, if specified only records for that gender will be returned. This parameter is only valid for the "getEconomicStatus", "getEthnicGroup", "getMaritalStatus" and "getPopulationAgeGroup" endpoints. If specified for other endpoints, the parameter will be dropped.

parallel

Default = FALSE. Whether to run API calls in parallel or sequentially (default). Enabling parallel iterations is highly recommended for when querying multiple data types/years/towns.

Examples

Run this code
# output with no NA
if (FALSE) get_pop_queries(token, c("getOccupation", "getLanguageLiterate"),
    c("Bedok", "Yishun"), "2010")
if (FALSE) get_pop_queries(token, c("getEconomicStatus", "getEthnicGroup"),
    "Yishun", "2010", "female")

## note behaviour if data types is a mix of those that accept gender params
### only total will have all records
if (FALSE) get_pop_queries(token, c("getEconomicStatus", "getOccupation", "getLanguageLiterate"),
    "Bedok", "2010")
### data type that does not accept gender params will be in gender = Total
if (FALSE) get_pop_queries(token, c("getEconomicStatus", "getOccupation", "getLanguageLiterate"),
    "Bedok", "2010", gender = "female")

# output with some town-year queries without record due to no data
# warning message will show data_type/town/year/gender for which an error occurred
if (FALSE) get_pop_queries(token, c("getEconomicStatus", "getOccupation"),
    "Bedok", c("2010", "2012")) # no records for 2012

Run the code above in your browser using DataLab