A lightweight R package for converting between common units of temperature, length, and weight. Designed for clarity, ease of use, and reliability — with unit tests and full documentation.
Final project for CS50’s Introduction to Programming with R.
| Function | Description | Example |
|---|---|---|
C_to_F() |
Convert Celsius to Fahrenheit | C_to_F(0) → 32 °F |
F_to_C() |
Convert Fahrenheit to Celsius | F_to_C(32) → 0 °C |
kg_to_lbs() |
Convert kilograms to pounds | kg_to_lbs(1) → 2.2 lbs |
lbs_to_kg() |
Convert pounds to kilograms | lbs_to_kg(2.20462) → 1 kg |
cm_to_inches() |
Convert centimetres to inches | cm_to_inches(2.54) → 1 inch |
inches_to_cm() |
Convert inches to centimetres | inches_to_cm(1) → 2.54 cm |
Each function checks for:
- Missing or null inputs
- Non-numeric inputs
- Precision rounding
Unit tests are provided using the testthat framework. Run all tests using:
devtools::test()Test coverage includes:
- Correct conversions
- Handling of NA/missing/null inputs
- Type safety (e.g., strings, lists)
unitconverter/
├── R/ # Source functions (e.g., C_to_F.R)
├── man/ # Manual .Rd documentation files
├── tests/
│ ├── testthat.R # Load test suite
│ └── testthat/ # Unit tests per function
├── NAMESPACE # Export definitions
└── README.md # This file
You can load the functions into your R session as follows:
source("R/C_to_F.R")
C_to_F(25) # Output: 77 °FOr if packaging via devtools:
library(devtools)
load_all("unitconverter")
kg_to_lbs(70)> C_to_F(100)
[1] "212 °F"
> cm_to_inches(10)
[1] "3.94 inches"
> lbs_to_kg(220)
[1] "99.79 kg"- How to write modular, reusable R functions
- Robust input validation and error handling
- Documenting with .Rd files
- Testing using testthat
- Building a basic R package structure
MIT