Learn R Programming

⚠️There's a newer version (0.10.0) of this package.Take me there.

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)

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

234

Version

0.8.2

License

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

Issues

Pull Requests

Stars

Forks

Maintainer

Sierra Johnson

Last Published

May 17th, 2025

Functions in tidywater (0.8.2)

blend_waters

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

Calculate DBP formation
chemdose_chlordecay

Calculate chlorine decay
bromatecoeffs

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

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

Convert water class object to a dataframe
define_water_chain

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

Calculate unit conversions for common compounds
chemdose_toc

Determine TOC removal from coagulation
define_water

Create a water class object given water quality parameters
chemdose_ph

Calculate new pH and ion balance after chemical addition
dbpcoeffs

Data frame of DBP coefficients for predicting DBP formation
dbp_correction

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

Dissociation constants and standard enthalpy for weak acids/bases
ozonate_bromate

Calculate bromate formation
define_water_once

Apply define_water and output a dataframe
cl2coeffs

Data frame of Cl2 decay coefficients
pactoccoeffs

Data frame of PAC TOC model coefficients
%>%

Pipe operator
pac_toc

Calculate DOC Concentration in PAC system
dissolve_pb

Simulate contributions of various lead solids to total soluble lead
edwardscoeff

Data frame of Edwards model coefficients
solvect_chlorine

Determine disinfection credit from chlorine.
solvect_o3

Determine disinfection credit from ozone.
mweights

Molar weights of relevant compounds
plot_ions

Create summary plot of ions from water class
pluck_water

Pluck out a single parameter from a water class object
leadsol_constants

Data frame of equilibrium constants for lead and copper solubility
solvemass_chem

Convert mg/L of chemical to lb/day
solvecost_solids

Determine solids disposal cost
solvecost_power

Determine power cost
solvecost_chem

Determine chemical cost
solveresid_o3

Determine ozone decay
solvecost_labor

Determine labor cost
summarize_wq

Create summary table from water class
solvedose_alk

Calculate a desired chemical dose for a target alkalinity
solvedose_ph

Calculate a desired chemical dose for a target pH
tidywater-package

tidywater: Water Quality Models for Drinking Water Treatment Processes
water_df

Data frame of water quality parameters
solvemass_solids

Determine solids lb/day
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
biofilter_toc

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

Calculate dissolved inorganic carbon (DIC) from total carbonate
calculate_corrosion

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