$ show r-quantities/units
This R package defines an S3 class `units` that attaches measurement units (e.g., meters, seconds, kg) to numeric vectors, matrices, and data frame columns. It provides automatic unit propagation, conversion, derivation, and simplification through arithmetic operations, built on the UNIDATA udunits2 C library. The package integrates with ggplot2 for axis/scale labeling, supports conversion to/from difftime and POSIXct, and offers helpers like `set_units()`, `mixed_units()`, and `keep_units()` for preserving units across operations that would otherwise strip them. It is actively maintained, with comprehensive tests, CI workflows, reverse dependency checks, and vignettes demonstrating its use in scientific and geospatial computing. While the core unit logic is delegated to udunits2, the R implementation demonstrates clean S3 method dispatch, careful handling of unit symbols, and a reusable pattern for attribute preservation.
The LLM identified 2 files worth highlighting for reuse:
Implements `keep_units()` which wraps a function call and reapplies the units from the input to the output. This is a clean, self-contained pattern for preserving S3 attributes across functions that strip them. It can be extracted and reused for any R class that relies on attributes, not just units.
Implements `mixed_units()` to create vectors where each element can have a different measurement unit. The code handles parsing, arithmetic, and printing of such vectors, demonstrating a non-trivial pattern for managing heterogeneous unit data. While coupled to the units S3 class, the approach could inform similar mixed-type vector implementations.