Learn R Programming

tidywater

Overview

Tidywater incorporates published water chemistry and empirical models in a standard format. The modular functions allow for building custom, comprehensive drinking water treatment processes. Functions are designed to work in a tidyverse workflow.

Installation

# Install tidywater from CRAN:
install.packages("tidywater")

# Alternatively, install the development version from GitHub:
# install.packages("devtools")
devtools::install_github("BrownandCaldwell-Public/tidywater")

Examples

In this first example, acid-base chemistry and TOC removal models are demonstrated. This example uses tidywater base functions to model a single water quality scenario.

library(tidywater)
library(tidyverse)
## Use base tidywater functions to model water quality for a single scenario.
base_coagulation <- define_water(ph = 8, alk = 90, tds = 50, toc = 3, doc = 2.8, uv254 = 0.08) %>%
  chemdose_ph(alum = 30) %>%
  chemdose_toc(alum = 30)
#> Warning in chemdose_ph(., alum = 30): Sulfate-containing chemical dosed, but
#> so4 water slot is NA. Slot not updated because background so4 unknown.

To model multiple water quality scenarios, use tidywater’s helper functions (x_chain or x_once) to apply the models to a dataframe.

## x_chain functions apply models to a list of "waters", and output a list of "waters" so that
## the data can be piped into the next tidywater model.
coagulation <- water_df %>%
  define_water_chain(output_water = "raw") %>%
  mutate(alum = 30) %>%
  chemdose_ph_chain(input_water = "raw", output_water = "phchange") %>%
  chemdose_toc_chain(input_water = "phchange", output_water = "coag")

## To get out individual parameters, use `pluck_water`
coagulation <- coagulation %>%
  pluck_water(input_waters = c("raw", "coag"), parameter = c("ph", "doc"))

Note that these functions use a “water” class. The “water” class is the foundation of the package; it provides a mechanism for linking models in any order while maintaining water quality information. The define_water function takes water quality inputs, but define_water_chain may be used to convert a dataframe to a list of “waters”.

For more detailed examples on tidywater functions and how to use “water” class data, please see the tidywater vignettes: browseVignettes("tidywater")

Limitations

This project is maintained by volunteers and is provided without warranties or guarantees of any kind.

Use at your own risk. For official support, please contact Brown and Caldwell.

Please read our CONTRIBUTING.md and SECURITY.md before submitting issues or pull requests.

Copy Link

Version

Install

install.packages('tidywater')

Monthly Downloads

449

Version

0.9.0

License

Apache License (>= 2) | MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Sierra Johnson

Last Published

July 3rd, 2025

Functions in tidywater (0.9.0)

define_water_once

Apply define_water and output a dataframe
convert_water

Convert water class object to a dataframe
bromatecoeffs

Data frame of bromate coefficients for predicting bromate formation during ozonation
dbpcoeffs

Data frame of DBP coefficients for predicting DBP formation
correct_k

Correct acid dissociation constants
define_water

Create a water class object given water quality parameters
dbp_correction

Data frame of correction factors for estimating DBP formation as a function of location
dissolve_pb

Simulate contributions of various lead solids to total soluble lead
biofilter_toc

Determine TOC removal from biofiltration using Terry & Summers BDOC model
define_water_chain

Apply define_water within a dataframe and output a column of water class to be chained to other tidywater functions
balance_ions

Add an ion to balance overall charge in a water
summarize_wq

Create summary table from water class
edwardscoeff

Data frame of Edwards model coefficients
water_df

Data frame of water quality parameters
tidywater-package

tidywater: Water Quality Models for Drinking Water Treatment Processes
decarbonate_ph

Apply decarbonation to a water
pac_toc

Calculate DOC Concentration in PAC system
pactoccoeffs

Data frame of PAC TOC model coefficients
solvedose_ph

Calculate a desired chemical dose for a target pH
solvemass_chem

Convert mg/L of chemical to lb/day
discons

Dissociation constants and standard enthalpy for weak acids/bases
%>%

Pipe operator
ozonate_bromate

Calculate bromate formation
solvecost_solids

Determine solids disposal cost
solvect_chlorine

Determine disinfection credit from chlorine.
modify_water

Modify a single slot in a water class object
leadsol_constants

Data frame of equilibrium constants for lead and copper solubility
plot_ions

Create summary plot of ions from water class
solvemass_solids

Determine solids lb/day
solvecost_labor

Determine labor cost
solvecost_power

Determine power cost
mweights

Molar weights of relevant compounds
solvecost_chem

Determine chemical cost
pluck_water

Pluck out a single parameter from a water class object
dissolve_cu_once

Calculate Dissolved Copper Concentration
dissolve_cu

Calculate Dissolved Copper Concentration
solvect_o3

Determine disinfection credit from ozone.
solveresid_o3

Determine ozone decay
solvedose_alk

Calculate a desired chemical dose for a target alkalinity
chemdose_chloramine

Calculate chlorine and chloramine Concentrations with the breakpoint cblorination approach
chemdose_chlordecay

Calculate chlorine decay
calculate_corrosion

Calculate six corrosion and scaling indices (AI, RI, LSI, LI, CSMR, CCPP)
blend_waters

Determine blended water quality from multiple waters based on mass balance and acid/base equilibrium
calculate_hardness

Calculate hardness from calcium and magnesium
chemdose_dbp

Calculate DBP formation
calculate_activity

Calculate activity coefficients
chemdose_ph

Calculate new pH and ion balance after chemical addition
convert_units

Calculate unit conversions for common compounds
chemdose_toc

Determine TOC removal from coagulation
chloramine_conv

Data frame of conversion factors for estimating DBP formation from chloramines
cl2coeffs

Data frame of Cl2 decay coefficients