Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.2
Imports:
ape,
dataRetrieval,
dplyr,
magrittr,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export(InvalidResultUnit)
export(InvalidSpeciation)
export(MediaNotWater)
export(PotentialDuplicateRowID)
export(QAPPDocAvailable)
export(QAPPapproved)
export(ResultValueSpecialCharacters)
export(UncommonAnalyticalMethodID)
export(readWQPdataTADA)
160 changes: 159 additions & 1 deletion R/ResultFlagsIndependent.R
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,162 @@ AboveNationalWQXUpperThreshold <- function(.data, clean = FALSE){
stop("clean argument must be Boolean (TRUE or FALSE)")
}
}
}
}


#' Flag or remove result without an approved QAPP
#'
#' Organizations that submit data to the Water Quality Portal (WQP) sometimes
#' include metadata with their results to indicate if the data produced has an
#' approved Quality Assurance Project Plan (QAPP). This check reviews data
#' submitted under the column "QAPPApprovedIndicator". When clean=false,
#' a column will be appended to flag results that do not have an approved QAPP.
#' When clean = TRUE, rows with values that do not have an approved QAPP are
#' removed from the dataset and no column will be appended.This function should
#' only be used to remove data if an approved QAPP is required to use data in
#' water quality assessments
#'
#' @param .data TADA dataset
#' @param clean Boolean argument; removes data without an Approved QAPP from
#' the dataset when clean = TRUE. Default is clean = FALSE.
#'
#' @return When clean = FALSE, a column is appended to the input data set that
#' flags rows without an Approved QAPP. When clean = TRUE, data without an
#' Approved QAPP is removed from the dataset.
#'
#' @export

QAPPapproved <- function(.data, clean = FALSE){

# check that .data object is compatible with TADA
# check .data is of class data.frame
if(("data.frame" %in% class(.data)) == FALSE) {
stop("Input object must be of class 'data.frame'")
}
# check .data has required columns
if(all(c("QAPPApprovedIndicator") %in% colnames(.data)) == FALSE) {
stop("The dataframe does not contain the required fields to use TADA.
Use either the full physical/chemical profile downloaded from WQP or
download the TADA profile template available on the EPA TADA webpage.")
}
# execute function after checks are passed
if(all(c("QAPPApprovedIndicator") %in% colnames(.data)) == TRUE) {

# flag data where QAPP was not approved
# make QAPPapproved.data data frame
QAPPapproved.data <- dplyr::filter(.data,
QAPPApprovedIndicator == "N")

# if there is data without an approved QAPP in the data set
if(nrow(QAPPapproved.data) != 0){
# append flag column
QAPPapproved.data$QAPPapproved <- "N"
# join QAPPapproved.data to flag.data
flag.data <- merge(.data, QAPPapproved.data, all.x = TRUE)

# flagged output
if(clean == FALSE) {
return(flag.data)
}

# clean output
if(clean == TRUE) {
# remove data without an approved QAPP
clean.data <- dplyr::filter(flag.data, !(QAPPapproved %in% "N"))

# remove QAPPapproved column
clean.data <- dplyr::select(clean.data, -QAPPapproved)

return(clean.data)
} else {
stop("clean argument must be Boolean (TRUE or FALSE)")
}
}

# if no QAPP approved indicator data is in the data set
if(nrow(QAPPapproved.data) == 0){
warning("The dataset does not contain QAPP approval indicator data.")

return(.data)
}
}
}


#' Flag or remove result without an approved QAPP
#'
#' Organizations that submit data to the Water Quality Portal (WQP) sometimes
#' include metadata with their results to indicate if the data produced has an
#' associated Quality Assurance Project Plan (QAPP). This check reviews data
#' submitted under the column "ProjectFileUrl" to determine if
#' a QAPP document is available to review. When clean=false,
#' a column will be appended to flag results that do not have an associated
#' QAPP document url provided. When clean = TRUE, rows with values that do not
#' have an associated QAPP document are removed from the dataset and no column
#' will be appended. This function should only be used to remove data if an
#' accompanying QAPP document is required to use data in assessments
#'
#' @param .data TADA dataset
#' @param clean Boolean argument; removes data without an associated QAPP
#' document from the dataset when clean = TRUE. Default is clean = FALSE.
#'
#' @return When clean = FALSE, a column is appended to the input data set that
#' flags rows without an associated QAPP document. When clean = TRUE,
#' data without an associated QAPP document is removed from the dataset.
#'
#' @export
#'
QAPPDocAvailable <- function(.data, clean = FALSE){

# check that .data object is compatible with TADA
# check .data is of class data.frame
if(("data.frame" %in% class(.data)) == FALSE) {
stop("Input object must be of class 'data.frame'")
}
# check .data has required columns
if(all(c("ProjectFileUrl") %in% colnames(.data)) == FALSE) {
stop("The dataframe does not contain the required fields to use TADA.
Use either the full physical/chemical profile downloaded from WQP or
download the TADA profile template available on the EPA TADA webpage.")
}
# execute function after checks are passed
if(all(c("ProjectFileUrl") %in% colnames(.data)) == TRUE) {

# flag data where QAPP document url is not provided
# make QAPPdoc.data data frame
QAPPdoc.data <- dplyr::filter(.data, ProjectFileUrl == c("null", "NA"))

# if there is data without an associated QAPP url in the data set
if(nrow(QAPPdoc.data) != 0){
# append flag column
QAPPdoc.data$QAPPDocAvailable <- "N"
# join QAPPdoc.data to flag.data
flag.data <- merge(.data, QAPPdoc.data, all.x = TRUE)

# flagged output
if(clean == FALSE) {
return(flag.data)
}

# clean output
if(clean == TRUE) {
# remove data without an associated QAPP url
clean.data <- dplyr::filter(flag.data, !(QAPPDocAvailable %in% "N"))

# remove QAPPDocAvailable column
clean.data <- dplyr::select(clean.data, -QAPPDocAvailable)

return(clean.data)
} else {
stop("clean argument must be Boolean (TRUE or FALSE)")
}
}

# if no associated QAPP url data is in the data set
if(nrow(QAPPdoc.data) == 0){
warning("The dataset does not contain QAPP document url data.")

return(.data)
}
}
}
31 changes: 31 additions & 0 deletions man/QAPPDocAvailable.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions man/QAPPapproved.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test_data/test_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ library(dataRetrieval)
library(dplyr)
library(stringr)

#WQP URL
#https://www.waterqualitydata.us/data/Project/search?statecode=US%3A24&siteType=Lake%2C%20Reservoir%2C%20Impoundment&siteType=Stream&sampleMedia=water&sampleMedia=Water&startDateLo=01-01-2019&startDateHi=01-01-2022&mimeType=csv&zip=yes&providers=NWIS&providers=STEWARDS&providers=STORET

# Set query parameters ####
WQPquery <- list(statecode = "US:24", Sitetype = c(
"Lake, Reservoir, Impoundment", "Stream"), Samplemedia = c("water", "Water"),
Expand All @@ -15,6 +18,8 @@ narrow.DR <- readWQPdata(WQPquery, dataProfile = "narrowResult")

sites.DR <- whatWQPsites(WQPquery)

projects.DR <- readWQPdata(WQPquery, service = "Project")

# Join station data to full phys/chem (results.DR) ####

join1 <- results.DR %>%
Expand Down