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")
#> Warning: `chemdose_toc_chain()` was deprecated in tidywater 0.10.0.
#> ℹ Please use `chemdose_toc_df()` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: `chemdose_ph_chain()` was deprecated in tidywater 0.10.0.
#> ℹ Please use `chemdose_ph_df()` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: `define_water_chain()` was deprecated in tidywater 0.10.0.
#> ℹ Please use `define_water_df()` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

## 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

234

Version

0.10.0

License

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

Issues

Pull Requests

Stars

Forks

Maintainer

Sierra Johnson

Last Published

August 24th, 2025

Functions in tidywater (0.10.0)

dbpcoeffs

Data frame of DBP coefficients for predicting DBP formation
convert_units

Calculate unit conversions for common compounds
correct_k

Correct acid dissociation constants
convert_water

Convert water class object to a dataframe
chemdose_ph

Calculate new pH and ion balance after chemical addition
decarbonate_ph

Apply decarbonation to a water
dbp_correction

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

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

Data frame of Cl2 decay coefficients
chemdose_toc

Determine TOC removal from coagulation
dissolve_cu_df

Calculate Dissolved Copper Concentration
gacbv_toc

Calculate maximum bed volumes to stay below target DOC
edwardscoeff

Data frame of Edwards model coefficients
dissolve_pb

Simulate contributions of various lead solids to total soluble lead
gac_toc

Calculate TOC Concentration in GAC system
gacrun_toc

Calculate TOC Concentration in GAC system
dissolve_cu

Calculate Dissolved Copper Concentration
discons

Dissociation constants and standard enthalpy for weak acids/bases
plot_ions

Create summary plot of ions from water class
ozonate_bromate

Calculate bromate formation
pac_toc

Calculate DOC Concentration in PAC system
plot_lead

Create dissolved lead and DIC contour plot given input data frame
pactoccoeffs

Data frame of PAC TOC model coefficients
%>%

Pipe operator
opensys_ph

Calculate pH for water in an open system
mweights

Molar weights of relevant compounds
solvecost_solids

Determine solids disposal cost
solvect_chlorine

Determine disinfection credit from chlorine.
solvect_o3

Determine disinfection credit from ozone.
solvedose_alk

Calculate a desired chemical dose for a target alkalinity
solvecost_power

Determine power cost
solvecost_labor

Determine labor cost
solvecost_chem

Determine chemical cost
solvedose_ph

Calculate a desired chemical dose for a target pH
summarize_wq

Create summary table from water class
water_df

Data frame of water quality parameters
rename_helpers

Functions renamed in tidywater 0.10.0
solvemass_chem

Convert mg/L of chemical to lb/day
toc_compliance_table

Data frame of TOC removal requirements per Stage 1 DBP Rule
tidywater-package

tidywater: Water Quality Models for Drinking Water Treatment Processes
define_water_df

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

Pluck out a single parameter from a water class object
solvemass_solids

Determine solids lb/day
regulate_toc

Determine if TOC removal meets Stage 1 DBP Rule requirements
define_water

Create a water class object given water quality parameters
vlog_removalcts

Data frame of virus log removal CT values
leadsol_constants

Data frame of equilibrium constants for lead and copper solubility
modify_water

Modify slots in a water class object
solveresid_o3

Determine ozone decay
blend_waters

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

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

Calculate activity coefficients
calculate_corrosion

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

Calculate DBP formation
chemdose_chlordecay

Calculate chlorine decay
biofilter_toc

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

Add an ion to balance overall charge in a water
calculate_hardness

Calculate hardness from calcium and magnesium
chemdose_chloramine

Calculate chlorine and chloramine Concentrations with the breakpoint cblorination approach