| Title: | Various Utility Functions That Don’t Really Belong Anywhere Else |
|---|---|
| Description: | A "catch-all" package that holds utility functions commonly used in R programming projects that I work with. |
| Authors: | Adam H. Sparks [cre, aut] (ORCID: <https://orcid.org/0000-0002-0061-8359>), Raphael Saldanha [aut] (ORCID: <https://orcid.org/0000-0003-0652-8466>, Contributed original code adapted to `rle2()`.) |
| Maintainer: | Adam H. Sparks <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.3 |
| Built: | 2026-05-12 06:54:28 UTC |
| Source: | https://codeberg.org/adamhsparks/misc.debris |
Used to check vectors of dates mainly to ensure that weather data are
complete. Invisibly returns a NULL value if no errors are found, else
issues a warning() with an informative message providing the user with
missing date values in the sequence and the optional location name if it's
provided.
check_date_seq(x, y = NULL)check_date_seq(x, y = NULL)
x |
A |
y |
A |
No return value, called for its side-effects, validates that dates are sequential and errors if they are not in order or some are missing.
Adam H. Sparks, [email protected]
# passes x <- seq(as.Date("2021-01-01"), as.Date("2021-01-31"), by = "1 day") y <- "BA" check_date_seq(x = x, y = y) # fails x <- c( seq(as.Date("2021-01-01"), as.Date("2021-01-31"), by = "1 day"), seq(as.Date("2021-02-02"), as.Date("2021-02-28"), by = "1 day") ) y <- "BA" check_date_seq(x = x, y = y)# passes x <- seq(as.Date("2021-01-01"), as.Date("2021-01-31"), by = "1 day") y <- "BA" check_date_seq(x = x, y = y) # fails x <- c( seq(as.Date("2021-01-01"), as.Date("2021-01-31"), by = "1 day"), seq(as.Date("2021-02-02"), as.Date("2021-02-28"), by = "1 day") ) y <- "BA" check_date_seq(x = x, y = y)
Checks user-provided longitude and latitude values that they fall in Australia. Errors if any values do not.
check_oz_lonlat(longitude, latitude)check_oz_lonlat(longitude, latitude)
longitude |
user provided numeric value as decimal degrees |
latitude |
user provided numeric value as decimal degrees |
An invisible NULL, called for side-effects. Errors if the
latitude or longitude values are outside of Australia.
# No error, Perth, WA check_oz_lonlat(longitude = 115.86, latitude = -31.95) ## Not run: # Error, in northern hemisphere check_oz_lonlat(longitude = 115.86, latitude = 31.95) ## End(Not run)# No error, Perth, WA check_oz_lonlat(longitude = 115.86, latitude = -31.95) ## Not run: # Error, in northern hemisphere check_oz_lonlat(longitude = 115.86, latitude = 31.95) ## End(Not run)
Given weather data from weatherOz, create a climograph with maximum and minimum temperature on the left-hand y-axis and precipitation on the right- hand y-axis. The colours used are DPIRD's dark red (tmax), light blue (tmin) and dark blue (rain).
create_climograph(weather)create_climograph(weather)
weather |
A |
A ggplot2 object.
Adam H. Sparks, [email protected]
library(weatherOz) wd <- get_dpird_summaries( station_code = "BI", start_date = "20220101", end_date = "20221231", api_key = Sys.getenv("DPIRD_API_KEY"), interval = "daily", values = c( "airTemperatureMin", "airTemperatureMax", "rainfall" ) ) # create the {ggplot2} object c_graph <- create_climograph(wd) # add a title, subtitle and caption library(ggplot2) main <- "Binnu Weather for 2022" sub <- "Precipitation (left y-axis) and Min and Max Temperature (right y-axis)" cap <- "Data from DPIRD Weather 2.0 API" c_graph + labs( title = main, subtitle = sub, caption = cap )library(weatherOz) wd <- get_dpird_summaries( station_code = "BI", start_date = "20220101", end_date = "20221231", api_key = Sys.getenv("DPIRD_API_KEY"), interval = "daily", values = c( "airTemperatureMin", "airTemperatureMax", "rainfall" ) ) # create the {ggplot2} object c_graph <- create_climograph(wd) # add a title, subtitle and caption library(ggplot2) main <- "Binnu Weather for 2022" sub <- "Precipitation (left y-axis) and Min and Max Temperature (right y-axis)" cap <- "Data from DPIRD Weather 2.0 API" c_graph + labs( title = main, subtitle = sub, caption = cap )
A helper function that wraps two functions from weatherOz to fetch daily weather data from both the DPIRD Weather 2.0 and SILO Patched Point APIs and return a list of these objects. Values are predetermined and only those that are shared between the two APIs are returned.
get_weather_list(stations, first, last = Sys.Date(), api_keys)get_weather_list(stations, first, last = Sys.Date(), api_keys)
stations |
A |
first |
The date on which the weather data should start, provided in ISO8601 format, e.g. "2021-01-01". |
last |
The last date on which the weather data should end, provided in ISO8601 format, e.g. "2021-01-01". Defaults to the current system date. |
api_keys |
A vector of API keys for the DPIRD Weather 2.0 API and the SILO API in that order (it's alphabetical, DPIRD, SILO). |
A list of data.frame objects containing weather data from
DPIRD and SILO APIs as requested.
# Note that you must provide your own API keys to use this function get_weather_list( stations = c("4041", "BA"), first = "2021-01-01", last = "2021-01-02", api_keys = c(Sys.getenv("DPIRD_API_KEY"), Sys.getenv("SILO_API_KEY")) )# Note that you must provide your own API keys to use this function get_weather_list( stations = c("4041", "BA"), first = "2021-01-01", last = "2021-01-02", api_keys = c(Sys.getenv("DPIRD_API_KEY"), Sys.getenv("SILO_API_KEY")) )
Generic function for finding the mode, supports multiple modes.
mode(x, max = FALSE, min = FALSE)mode(x, max = FALSE, min = FALSE)
x |
An R numeric |
max |
|
min |
|
The mode, most commonly occurring value in the vector, as a
numeric value. In cases where there are more than one mode, all values
will be returned by default.
x <- c(1, 2, 3, 3, 3, 4, 4, 5, 5, 5) mode(x) mode(x, max = TRUE) mode(x, min = TRUE)x <- c(1, 2, 3, 3, 3, 4, 4, 5, 5, 5) mode(x) mode(x, max = TRUE) mode(x, min = TRUE)
Run length encoding to detect consecutive sequences of events.
rle2(x, index, l_run, value)rle2(x, index, l_run, value)
x |
A vector of values to check for consecutive sequences of events. |
index |
An index of event values, e.g. a vector of date values. |
l_run |
An |
value |
The base value to check against, for which values greater than
will be recorded as |
A data.table with three columns, the ‘index’, ‘x’,
the original value that was tested and ‘test’ a column of Boolean
values indicating whether the event was TRUE (greater than value) or
FALSE (less than value), “test”.
R already has an base::rle(), so this function is suffixed with 2
to avoid NAMESPACE clashes.
Adam Sparks, [email protected]
Raphael Saldanha, https://rfsaldanha.github.io/posts/run_length_encoding.html with modifications by Adam Sparks (DPIRD).
# Get rainfall data since 2017 for Northam library(weatherOz) library(scales) library(ggplot2) wd <- get_dpird_summaries( station_code = "NO", start_date = "20170101", end_date = "20171231", api_key = Sys.getenv("DPIRD_API_KEY"), interval = "daily", ) # Determine where runs of 2 or more days with rain over 0.5mm occur rain <- rle2( x = wd$rainfall, index = wd$date, l_run = 2, value = 0.5 ) # Plot rainfall values ggplot(data = rain, aes(x = index, y = value)) + geom_line(colour = "light blue", alpha = 0.7) + geom_point(aes(color = test), alpha = 0.85) + scale_color_manual(values = c("blue", "darkred")) + scale_x_date(labels = date_format("%m-%Y")) + xlab("Date") + ylab("Rainfall (mm)") + guides(colour = guide_legend("Two or more consecutive days of rain")) + theme(legend.position = "bottom", legend.direction = "horizontal") # Determine where runs of 2 or more days avg temp above 28 C occur tavg <- rle2( x = wd$air_tavg, index = wd$date, l_run = 2, value = 28 ) # Plot rainfall values ggplot(data = tavg, aes(x = index, y = value)) + geom_line(colour = "lightblue", alpha = 0.7) + geom_point(aes(color = test), alpha = 0.85) + scale_color_manual(values = c("blue", "darkred")) + geom_hline(yintercept = 28, colour = "darkred") + scale_x_date(labels = date_format("%m-%Y")) + xlab("Date") + ylab("Temperature (C)") + guides(colour = guide_legend("Four or more consecutive days above 28 C")) + theme(legend.position = "bottom", legend.direction = "horizontal")# Get rainfall data since 2017 for Northam library(weatherOz) library(scales) library(ggplot2) wd <- get_dpird_summaries( station_code = "NO", start_date = "20170101", end_date = "20171231", api_key = Sys.getenv("DPIRD_API_KEY"), interval = "daily", ) # Determine where runs of 2 or more days with rain over 0.5mm occur rain <- rle2( x = wd$rainfall, index = wd$date, l_run = 2, value = 0.5 ) # Plot rainfall values ggplot(data = rain, aes(x = index, y = value)) + geom_line(colour = "light blue", alpha = 0.7) + geom_point(aes(color = test), alpha = 0.85) + scale_color_manual(values = c("blue", "darkred")) + scale_x_date(labels = date_format("%m-%Y")) + xlab("Date") + ylab("Rainfall (mm)") + guides(colour = guide_legend("Two or more consecutive days of rain")) + theme(legend.position = "bottom", legend.direction = "horizontal") # Determine where runs of 2 or more days avg temp above 28 C occur tavg <- rle2( x = wd$air_tavg, index = wd$date, l_run = 2, value = 28 ) # Plot rainfall values ggplot(data = tavg, aes(x = index, y = value)) + geom_line(colour = "lightblue", alpha = 0.7) + geom_point(aes(color = test), alpha = 0.85) + scale_color_manual(values = c("blue", "darkred")) + geom_hline(yintercept = 28, colour = "darkred") + scale_x_date(labels = date_format("%m-%Y")) + xlab("Date") + ylab("Temperature (C)") + guides(colour = guide_legend("Four or more consecutive days above 28 C")) + theme(legend.position = "bottom", legend.direction = "horizontal")