Learn R Programming

CSHShydRology

Installing this package

The package may be downloaded directly from CRAN with:

install.packages("CSHShydRology")

Previous versions of the package and manuals can be found by clicking on releases. The latest version of the package may also be installed directly from this repository. The procedure is

  1. Install the package "devtools" - you only have to do this once. Note that this will also install several dependancies
  2. Load the devtools library
  3. Install the package.

The commands are:

if (!require(devtools)) install.packages("devtools")
library(devtools)
install_github("CSHS-CWRA/CSHShydRology")

What is CSHS Hydrology?

This is an R package of functions used by Canadian hydrologists. The name is in recognition of the support provided by the Canadian Association Society for Hydrological Sciences (CSHS) which is an affiliated society of the Canadian Water Resources Association (CWRA). The CSHS website is https://cwra.org/en/affiliates-programs/cshs/.

Themes

This package contains functions which are grouped into themes. Currently the themes include:

  • Statistical hydrology (trend detection, data screening, frequency analysis, regionalization),
  • Basic data manipulations (input/conversion/adapter functions, missing data infilling),
  • Visualization (data visualization, standardized plotting functions),
  • Spatial hydrology (basin delineation, landscape data analysis, working with GIS),
  • Streamflow measurement analysis (rating curve analysis, velocity profiles, naturalization).

Other themes, such as Network design/analysis (homogeneity assessment), and Ecohydrology (fisheries and ecological analysis) will also be added if there is interest.

Coding standards

General standards

The general coding standards are as specified in the Google R coding guide at https://google.github.io/styleguide/Rguide.xml.

External packages

To reduce errors, it is a good idea to reduce reliance on other packages as much as is possible. Please check through the functions already referenced in the DESCRIPTION file before adding additional functions. When it is necessary to add a reference to an additional package, please do the following: Make the package suggested, rather than required. Make a note in the function’s Roxygen documentation that the function uses the package. Make sure that the version number of the package is referenced properly. When more than one function requires a given package, it can be made a required packages.

Devtools

To make package development easier, we will use the package devtools, which provides several tools. This package is explained at https://github.com/hadley/devtools.

Checking

The devtools check function will be used check each function for errors in syntax and style. Each function must pass the checks with zero warnings, errors or notes. Note that once you have loaded devtools, the check function can be accessed from a menu or from the build tab in Rstudio. It is also a good idea to manually run the check function from the command line, i.e.

R CMD check

as this can catch other problems.

Documentation

All functions need to be documented using the Roxygen package, which allows you to create the help files and documentation using comments in your function. The package is explained at https://cran.r-project.org/web/packages/roxygen2/vignettes/roxygen2.html. Rstudio has a very nice quick reference for Roxygen which you can access with Help|Roxygen Quick Reference. Each function must include the name of the authors(s). Please include your email address, and any publications that you want to reference. Note that the package itself also has a function. This contains documentation about the package as a whole, as well as settings which are used to create the package NAMESPACE file. Note that this means that the NAMESPACE file should never be edited directly by anyone. When the package is compiled and tested, Roxygen will create a .Rd file for each function, the package, and for the included data files. These files should never be edited manually. If you want to check how your .Rd file will look, you can load it into Rstudio, which can render it.

Vignettes

Vignettes are long form documentation which can be included in R packages. They can include long descriptions and worked examples, and are very useful to thoroughly explain functions to the users. The creation process is explained at http://r-pkgs.had.co.nz/vignettes.html. Even if you don’t write code, you can contribute to writing vignettes for functions.

Testing

The testthat package allows unit tests to be created for each function. The function is executed, using specified data, and the results are compared to specified values. Unit tests should be used whenever possible, as they will identify problems caused by changes in upstream packages. The testthat package is explained in detail in http://r-pkgs.had.co.nz/tests.html.

Test data

Each function is required to have an example, which should be executable code. The only exception should be for code which is intended to access data which may not be available, such as data downloaded from a server. The example, vignettes, and unit tests will all require test data sets. The data are stored in the /data folder of the package, and should be stored as .Rdata (binary) files, unless reading in data from another format is the point of the function. CRAN has a maximum limit of 1 MB for test data, so we will need to minimise the datasets we include. We should minimise the size of each test dataset and re-use the same data in as many functions as possible. If a function requires data output by another function, both functions can be included in the example. Each dataset needs to be documented in the same way as a function. The creation of datasets and their documentation is explained at http://r-pkgs.had.co.nz/data.html.

Working with GitHub

There is a lot of information available on using git and GitHub in R. The best starting point is https://support.rstudio.com/hc/en-us/articles/200532077-Version-Control-with-Git-and-SVN which shows how to integrate git and GitHub with RStudio.

Citation

The package citation may be generated with the following command in R.

citation("CSHShydRology")

Copy Link

Version

Install

install.packages('CSHShydRology')

Monthly Downloads

261

Version

1.4.2

License

AGPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Kevin Shook

Last Published

July 16th, 2024

Functions in CSHShydRology (1.4.2)

CAN05AA008

CAN05AA008
Spatial_hydrology_functions

Spatial Hydrology functions
HYDAT_list

List of Water Survey of Canada hydrometic stations.
ch_axis_doy

Generates the x axis beginning on specified day of year
ch_cut_block

Extracts a specified time period from a longer record
ch_checkchannels

Check Channels
ch_checkcatchment

Check Catchments
ch_create_wd

Create working directory
ch_color_gradient

ch_color_gradient
ch_contours

Create Contours
ch_circ_mean_reg

Calculates the circular mean, median, and regularity
ch_booth_plot

Create Booth plot of peaks over a threshold
ch_doys

Days of year and water year
ch_binned_MannWhitney

Compares two time periods of data using Mann-Whitney test
ch_clear_wd

Clear Working Directory
ch_get_ECDE_metadata

Reads Environment Canada Date Explorer (ECDE) meta data file
StatisticalHydrology-functions

Statistical analysis functions
Visualization-functions

Visualization functions
ch_catchment_hyps

Catchment hypsometry
ch_fdcurve

Plot Flow Duration Curve
ch_get_wscstation

Reads station information from a data file produced by ECDE
ch_read_AHCCD_monthly

Reads AHCCD monthly file
ch_flow_raster_trend

Raster plot and simple trends of observed streamflows by periods
ch_get_peaks

Extracts peak flows over a threshold
ch_read_AHCCD_daily

Reads AHCCD daily file
ch_get_url_data

Gets remote data sets
ch_decades_plot

Plots output from ch_binned_MannWhitney for decades
ch_rfa_julianplot

Circular plotting by day of year
ch_date_subset

Subsets dates by string
ch_hydrograph_plot

Hydrograph plot
ch_wbt_catchment_onestep

Delineates a catchment in a single step
ch_tr_signif

ch_tr_signif()
ch_rfa_seasonstat

Seasonal statistics for flood peaks
ch_polar_plot

Polar plot of daily streamflows
ch_col_gradient

Creates a colour gradient
ch_col_transparent

Add Transparency to plot colours
ch_rfa_extractamax

Extracts the annual maxima of a daily time series
ch_volcano_raster

Create Test Raster
ch_rfa_distseason

Distance in seasonal space
ch_tidyhydat_ECDE_meta

Creates an ECDE-like dataframe of metadata from tidyhydat
ch_sh_get_amax

Extracts annual maximum values from ECDE dataframe.
ch_slice

Converts doy or dwy into a factor that is used to bin data
ch_polar_plot_peaks

Polar / circular plots of peak flows
ch_wbt_channels

Generate stream network
ch_tr_sign

ch_tr_sign
ch_volcano_pourpoints

Creates a sample file of pour points
ch_wbt_check_whitebox

Checks for WhiteboxTools executable
ch_wbt_filenames

Creates names for Whitebox function input and output files
ch_sub_set_Years

Helper function for selecting points for an axis
ch_flow_raster_qa

Raster plot of daily streamflows with WSC quality flags
ch_polar_plot_prep

Creates a data structure to be passed to ch_polar_plot
ch_flow_raster

Raster plot of daily streamflows
ch_stack_EC

Stacks EC values
ch_qa_hydrograph

Plots a hydrograph with the data quality symbols and returns a report on qa symbols and missing data.
ch_read_ECDE_flows

Reads a file of WSC daily flows from ECDataExplorer (ECDE)
ch_regime_plot

Plots the regime of daily streamflows using quantiles
ch_tidyhydat_ECDE

Converts a tidyhydat daily flow data tibble to ECDE format
ch_wtr_yr

Designation of the water year
ch_test_url_file

Tests url to see if it will work
ch_wbt_removesinks

Removes sinks from a DEM
ch_wbt_pourpoints

Snap pour points to channels
flowAtlantic

Annual maxima from sites in the Atlantic region of Canada
ch_wbt_catchment

Delineate catchment boundaries
ch_wbt_flow_accumulation

Creates flow accumulation grid file
ch_wbt_flow_direction

Creates flow direction grid file
CAN01AD002

Streamflow data
Basic_data_manipulation_functions

Basic data manipulation functions
CSHShydRology-package

Functions for Canadian hydrological analyses