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)

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() %>%
  mutate(alum = 30) %>%
  chemdose_ph_chain() %>%
  chemdose_toc_chain()

## x_once functions apply models to a list of "waters", but output a data frame. The data can not be
## piped to a downstream tidywater function, but all the "water" parameters are now visible and
## can be manipulated as a typical data frame.

enhanced_coagulation <- water_df %>%
  define_water_chain() %>%
  mutate(alum = seq(1, 12, 1)) %>%
  chemdose_ph_once(hcl = 10)

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")

Copy Link

Version

Install

install.packages('tidywater')

Monthly Downloads

214

Version

0.7.0

License

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

Issues

Pull Requests

Stars

Forks

Maintainer

Sierra Johnson

Last Published

January 22nd, 2025

Functions in tidywater (0.7.0)

calculate_corrosion

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

Calculate chlorine decay
chemdose_chlordecay_once

Apply `chemdose_chlordecay`function within a data frame and output a data frame
chemdose_dbp

Calculate DBP formation
chemdose_chlordecay_chain

Apply `chemdose_chlordecay` within a data frame and output a column of `water` class to be chained to other tidywater functions
calculate_hardness

Calculate hardness from calcium and magnesium
calculate_corrosion_once

Apply `calculate_corrosion` to a dataframe and create new columns with up to 6 corrosion indices
calculate_dic

Calculate dissolved inorganic carbon (DIC) from total carbonate
calculate_corrosion_chain

Apply `calculate_corrosion` to a dataframe and output a column of `water` class to be chained to other tidywater functions.
chemdose_dbp_chain

Apply `chemdose_dbp` within a data frame and output a column of `water` class to be chained to other tidywater functions
chloramine_conv

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

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

Calculate new pH and ion balance after chemical addition
chemdose_ph_once

Apply `chemdose_ph` function and output a dataframe
cl2coeffs

Data frame of Cl2 decay coefficients
chemdose_toc

Determine TOC removal from coagulation
chemdose_toc_chain

Apply `chemdose_toc` within a dataframe and output a column of `water` class to be chained to other tidywater functions
chemdose_toc_once

Apply `chemdose_toc` function and output a data frame
define_water_once

Apply `define_water` and output a dataframe
discons

Dissociation constants and standard enthalpy for weak acids/bases
define_water_chain

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

Calculate new fluoride concentration after dosing alum.
chemdose_dbp_once

Apply `chemdose_dbp`function within a data frame and output a data frame
convert_watermg

Convert a `water` class object to a dataframe with ions in mg/L or ug/L
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
pac_toc_chain

Apply `pac_toc` within a data frame and output a column of `water` class to be chained to other tidywater functions PAC = powdered activated carbon
pac_toc_once

Apply `pac_toc`function within a data frame and output a data frame
pac_toc

Calculate DOC Concentration in PAC system
ozonate_bromate_once

Apply `ozonate_bromate`function within a data frame and output a data frame
mweights

Molar weights of relevant compounds
convert_units

Calculate unit conversions for common compounds
solvecost_labor

Determine labor cost
pluck_water

Pluck out a single parameter from a `water` class object
solvecost_chem

Determine chemical cost
convert_water

Convert `water` class object to a dataframe
solvecost_power

Determine power cost
leadsol_constants

Data frame of equilibrium constants for lead and copper solubility
solveresid_o3_once

Apply `solveresid_o3` to a data frame and create a new column with residual ozone dose
dissolve_pb_once

Apply `dissolve_pb` to a dataframe and create a new column with numeric dose
define_water

Create a water class object given water quality parameters
edwardscoeff

Data frame of Edwards model coefficients
dbpcoeffs

Data frame of DBP coefficients for predicting DBP formation
plot_ions

Create summary plot of ions from water class
%>%

Pipe operator
solvect_chlorine_once

Apply `solvect_chlorine` to a data frame and create new columns with ct and log removals.
solvedose_ph_once

Apply `solvedose_ph` to a dataframe and create a new column with numeric dose
solvect_o3

Determine disinfection credit from ozone.
solvemass_chem

Convert mg/L of chemical to lb/day
solvect_o3_once

Apply `solvect_o3` to a data frame and create new columns with ct and log removals.
summarize_wq

Create summary table from water class
solvedose_alk

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

tidywater: Water Quality Models for Drinking Water Treatment Processes
water_df

Data frame of water quality parameters
solvedose_alk_once

Apply `solvedose_alk` to a dataframe and create a new column with numeric dose
solvedose_ph

Calculate a desired chemical dose for a target pH
solvemass_solids

Determine solids lb/day
ozonate_bromate_chain

Apply `ozonate_bromate` within a data frame and output a column of `water` class to be chained to other tidywater functions
solvect_chlorine

Determine disinfection credit from chlorine.
solveresid_o3

Determine ozone decay
solvecost_solids

Determine solids disposal cost
ozonate_bromate

Calculate bromate formation
balance_ions_chain

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

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

Apply `biofilter_toc` within a dataframe and output a column of `water` class to be chained to other tidywater functions
balance_ions_once

Apply `balance_ions` function and output a dataframe
balance_ions

Add Na, K, Cl, or SO4 to balance overall charge in a water
blend_waters_once

Apply `blend_waters` to a dataframe and output `water` slots as a dataframe
biofilter_toc_once

Apply `biofilter_toc` function and output a data frame
biofilter_toc

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

Apply `blend_waters` within a dataframe and output a column of `water` class to be chained to other tidywater functions
bromatecoeffs

Data frame of bromate coefficients for predicting bromate formation during ozonation