knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
R Interface for Singapore's Weather, UV and Pollutants Data from National Environment Agency (NEA) APIs.
No API key is required for these APIs. The library can hence be installed without any authentication required.
As a caveat, this API client will be significantly slower than other APIs, as the NEA APIs allow only for queries of individual dates, one at a time. This interface helps to automate the querying of a list of dates, and the tidying of appropriate data into tidy data frames.
library(devtools) install_github("andrew-loh/neaSG", force = TRUE)
To list all functions supported by this package:
library(neaSG) ls(package:neaSG)
Air temperature, humidity and rainfall are given by weather station IDs. To get data frame of weather stations in Singapore by name, geographic coordinates and ID:
get_weatherstns()
Temperature is provided in degrees Celsius. Documentation is provided here.
To get data frame of per-minute air temperature in Singapore for specific dates:
#Dates must be input as characters in a "YYYY-MM-DD" format data <- get_airtemp(from = "2017-12-31", to = "2018-01-04") head(data)
Relative humidity is given as percentage, as is convention. Documentation is provided here.
To get data frame of per-minute relative humidity readings in Singapore for specific dates:
#Dates must be input as characters in a "YYYY-MM-DD" format data <- get_humidity(from = "2017-12-31", to = "2018-01-04") head(data)
Rainfall is given by mm/5-minutes. Documentation is provided here.
To get data frame of 5-minute rainfall readings in Singapore for specific dates:
#Dates must be input as characters in a "YYYY-MM-DD" format data <- get_rainfall(from = "2017-12-31", to = "2018-01-04") head(data)
UV Index provides data for hours when the sun is up - from 0700 to 1900hrs. As Singapore is a tropical country, sunrise and sunset times do not vary significantly through the year. Documentation is provided here.
#Dates must be input as characters in a "YYYY-MM-DD" format data <- get_UV(from = "2017-12-31", to = "2018-01-04") head(data)
Pollutants are given by hourly measures for respective indices. Data is provided for national and 5 subnational regions, in the form of 12 pollutant indices: O3 Sub-Index, PM10 24-hourly, PM10 Sub-Index, CO Sub-Index, PM2.5 24-Hourly, SO2 Sub-Index, CO 8-Hour-Max, NO2 1-Hour-Max, SO2 24-Hourly, PM2.5 Sub-Index, PSI 24-Hourly and O3 8-Hour Max. Documentation is provided here.
Additional argument natl_only
is, by default, TRUE
. If true, provides only national readings. If the user desires subnational readings, set natl_only = FALSE
#Dates must be input as characters in a "YYYY-MM-DD" format data <- get_pollutants(from = "2017-12-31", to = "2018-01-04") head(data)
The final function aims to provide an hourly weather summary for specified dates. Hourly aggregated weather data on air temperature, humidity, rainfall, UV and PSI 24-Hourly readings will be returned as a data frame. As each separate API returns data of varying granularity - geographical or temporal, this summary will aggregate across all of Singapore and over each hour. Documentation for each separate index can be acquired from each of the separate functions above.
data <- get_weathersumm(from = "2017-12-31", to = "2018-01-04") head(data)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.