Introduction

Packages are the fundamental unit of shareable code in R. A package bundles together code, data, documentation, and tests making it easy to share with others. You can read more information about R packages here: http://r-pkgs.had.co.nz/intro.html

A great way to find packages is simply searching online using key words that include “R” or “R packages”.

Ordinarly the user needs to install and load “packages” in their active work session. Beacuase we are working in R Studio Cloud, all packages, code, and data have been preloaded to your workspace

Here is an example of installing the R package ‘dataRetrieval’: install.packages(“dataRetieval”)

Load package(s)

Packages need to be loaded each time you open a work session

For this exercise we will the USGS’s “dataRetrieval” package to download streamflow data from USGS gaging station. Learn morea bout ‘dataRetrieval’ at https://owi.usgs.gov/R/dataRetrieval.html#1

After installing a package(s), they need to be loaded

library(dataRetrieval)

Packages often include vignettes (user manual/help file) and/or tutorials

vignette("dataRetrieval")
## starting httpd help server ... done

Acquire daily streamflow from USGS stream gage stations using dataRetrieval package

The first step is locating a USGS station. A handy URL for identifying USGS streamgage stations is http://maps.waterdata.usgs.gov/mapper/index.html

In this course, we will be analyzing climate and streamflow data, and building hydrologic models for the Big Sandy Creek watershed. The USGS gage is the Big Sandy Creek at Rockville, WV. The USGS Station number is 03070500.

Download data for a single USGS station

# Assign generic site number object to station number. Using generic objects is an efficient way to apply a single code to different stations. I this case, you only need to change the numerical station number 

siteNumber="03070500"

# The parameterCD object is used to call a specific variable measured at a gaging station. In our case, we are intersted in streamflow. SOme USGS stations measure water quality variabes such as temerature, dissolved oxygen, and tubidity and climate data such as precipitation. 

parameterCd<- "00060"           
parameterINFO <- readNWISpCode(parameterCd)
colnames(parameterINFO)
## [1] "parameter_cd"       "parameter_group_nm" "parameter_nm"      
## [4] "casrn"              "srsname"            "parameter_units"

Assign start data, end data, and statistics code, specified here as “00003”, mean daily data (streamflow in our case)

startDate <- "1980-01-01"
endDate <- "2010-12-31"
statCd <- "00003"

Call daily data from USGS server

# Here we name our object 'QDaily'
QDaily = readNWISdv(siteNumber, parameterCd, startDate, endDate)

Plot daily streamflow timeseries

# In order to plot the streamflow data, you need to understand what it is called. Here we use the 'names' command. The streamflow column is called 'X_00060_00003'

names(QDaily)
## [1] "agency_cd"        "site_no"          "Date"             "X_00060_00003"   
## [5] "X_00060_00003_cd"
# 'QDaily$Date' and 'QDaily$X_00060_00003' tell R to extract the 'Date' and streamflow ('X_00060_00003') columns from the 'QDaily" dataset. 

plot(QDaily$Date,QDaily$X_00060_00003,type="l",xlab="Date",ylab="Discharge ft3/s")
title('Big Sandy River, Rockville, WV')

Save daily data to .csv

write.csv(QDaily, "QDaily_raw.csv")