This R package is aimed at accessing the openaq API. OpenAQ is a community of scientists, software developers, and lovers of open environmental data who are building an open, real-time database that provides programmatic and historical access to air quality data. The package contains 5 functions that correspond to the 5 different types of query offered by the openaq API: cities, countries, latest, locations, and measurements. The package uses the dplyr package: all output tables are data.frame (dplyr “tbl_df”) objects that can be further processed and analyzed.
PACKAGE INSTALLATION & DATA FRAME
The first thing you have to do is install and load the packages.
# For dev version
if (!require("devtools")) install.packages("devtools")
The aq_countries function allows you to see which countries’ information is available within the platform. It is the easiest function because it does not have any argument. The code for each country is its ISO 3166-1 alpha-2 code.
(countriesTable <- aq_countries())
Using the aq_cities function, you can get all cities, for which information is available, within the platform. For each city, one gets the number of locations and the count of measures for the city, the URL encoded string, and the country it is in.
(citiesTable <- aq_cities())
The optional country argument allows you to do this for a given country, instead of the whole world.
(citiesTableIndia <- aq_cities(country="IN"))
If one inputs a country that is not in the platform (or misspells a code), then an error message is thrown.
The aq_locations function has far more arguments than the first two functions. You can filter locations in a given country, city, location, for a given parameter (valid values are “pm25”, “pm10”, “so2”, “no2”, “o3”, “co” and “bc”), from a given date and/or up to a given date, for values between a minimum and a maximum, for a given circle outside a central point by the use of the latitude, longitude and radius arguments. In the output table, you also get URL encoded strings for the city and the location. Below are several examples. Here we only look for locations with PM2.5 information in India.
(locationsIndia <- aq_locations(country="IN", parameter="pm25"))
Two functions allow you to retrieve data: the “aq_measurement” and the “aq_latest.” In both of them, the arguments’ city and location needs to be given as a URL encoded strings. The aq_measurements function has many arguments for getting a query specific to, say, a given parameter in a given location or for a given circle outside a central point by the use of the latitude, longitude and radius arguments. Below we get the PM2.5 measures for Anand Vihar in Delhi, India.
(tableResults <- aq_measurements(country="IN", city="Delhi", location="Anand+Vihar", parameter="pm25"))
You could also get all possible parameters in the same table. The aq_latest function gives a table with all the newest measures for the locations that are chosen by the arguments. If all arguments are NULL, it gives you all the newest measures for all locations.
(tableLatest <- aq_latest())
Below are the latest values for Anand Vihar at the time this vignette was compiled (cache=FALSE).
(tableLatest <- aq_latest(country="IN", city="Delhi", location="Anand+Vihar"))
Now, let’s move on to the first set of real exercises on the ropenaq package!
Manuel Leiva says
Hi, i try to fallow your example and have the next error
(countriesTable <- aq_countries())
data frame with 0 columns and 0 rows
You know how to fix