Skip to content

Commit

Permalink
Merge pull request #543 from USEPA/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
cristinamullin authored Nov 8, 2024
2 parents 3cd90e2 + 851e168 commit 2420ddc
Show file tree
Hide file tree
Showing 37 changed files with 68,647 additions and 68,608 deletions.
24 changes: 13 additions & 11 deletions R/CensoredDataSuite.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#' 4) Non-Detect
#' 5) Over-Detect
#' 6) Other Condition/Limit Populated
#' If user has not previously run TADA_FlagMeasureQualifierCode, this function will also run that
#' If user has not previously run TADA_FlagMeasureQualifierCode, this function will also run that
#' function to add the columns TADA.MeasureQualifierCode.Flag and TADA.MeasureQualifierCode.Def
#' because user-supplied Result Measure Qualifier codes are also used to ID censored results.
#'
Expand Down Expand Up @@ -287,18 +287,20 @@ TADA_IDCensoredData <- function(.data) {
#' # and in instances where the measurement is non-detect, set the result value
#' # to half of the detection limit value. For over-detect measurements, retain
#' # the detection limit value as the result value as-is.
#' Data_Nutrients_UT_CensoredFlag <- TADA_SimpleCensoredMethods(Data_Nutrients_UT,
#' nd_method = "multiplier",
#' nd_multiplier = 0.5,
#' od_method = "as-is",
#' od_multiplier = "null")
#' Data_Nutrients_UT_CensoredFlag <- TADA_SimpleCensoredMethods(Data_Nutrients_UT,
#' nd_method = "multiplier",
#' nd_multiplier = 0.5,
#' od_method = "as-is",
#' od_multiplier = "null"
#' )
#'
#' # Check for agreement between detection condition and detection limit type, and in instances where the measurement is non-detect, set the result value to a random value between 0 and the detection limit value. For over-detect measurements, retain the detection limit value as the result value as-is.
#' Data_Nutrients_UT_CensoredFlag <- TADA_SimpleCensoredMethods(Data_Nutrients_UT,
#' nd_method = "randombelowlimit",
#' nd_multiplier = "null",
#' od_method = "as-is",
#' od_multiplier = "null")
#' Data_Nutrients_UT_CensoredFlag <- TADA_SimpleCensoredMethods(Data_Nutrients_UT,
#' nd_method = "randombelowlimit",
#' nd_multiplier = "null",
#' od_method = "as-is",
#' od_multiplier = "null"
#' )
#'
TADA_SimpleCensoredMethods <- function(.data, nd_method = "multiplier", nd_multiplier = 0.5, od_method = "as-is", od_multiplier = "null") {
# check .data has all of the required columns
Expand Down
15 changes: 5 additions & 10 deletions R/CriteriaComparison.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@
#'
TADA_CreatePairRef <- function(.data, ph = TRUE, hardness = TRUE, temp = TRUE,
chloride = TRUE, salinity = TRUE, other_char = "null") {

# create data frame to store pair refs
pair.ref <- data.frame(matrix(ncol = 6, nrow = 0))

# name columns in pair.ref df
colnames(pair.ref) <- c(
"TADA.CharacteristicName", "TADA.ResultMeasure.MeasureUnitCode",
Expand All @@ -66,7 +65,8 @@ TADA_CreatePairRef <- function(.data, ph = TRUE, hardness = TRUE, temp = TRUE,
dplyr::select(
TADA.CharacteristicName, TADA.ResultMeasure.MeasureUnitCode,
TADA.MethodSpeciationName, TADA.ResultSampleFractionText,
NCount) %>%
NCount
) %>%
# retain only distinct rows
dplyr::distinct() %>%
# arrange from largest to smallest number of results
Expand All @@ -79,7 +79,6 @@ TADA_CreatePairRef <- function(.data, ph = TRUE, hardness = TRUE, temp = TRUE,


if (hardness == TRUE) {

# create character reference from WQX characteristics containing "HARDNESS" in name
char.ref <- TADA_GetCharacteristicRef() %>%
dplyr::mutate(CharacteristicName = toupper(CharacteristicName)) %>%
Expand All @@ -99,7 +98,6 @@ TADA_CreatePairRef <- function(.data, ph = TRUE, hardness = TRUE, temp = TRUE,
}

if (ph == TRUE) {

# filter TADA df for pH results
ph.ref <- .data %>%
dplyr::filter(TADA.CharacteristicName == "PH") %>%
Expand All @@ -114,7 +112,6 @@ TADA_CreatePairRef <- function(.data, ph = TRUE, hardness = TRUE, temp = TRUE,
}

if (temp == TRUE) {

# filter TADA df for temperature results
temp.ref <- .data %>%
dplyr::filter(TADA.CharacteristicName %in% c("TEMPERATURE", "TEMPERATURE, WATER")) %>%
Expand All @@ -129,7 +126,6 @@ TADA_CreatePairRef <- function(.data, ph = TRUE, hardness = TRUE, temp = TRUE,
}

if (salinity == TRUE) {

# filter TADA df for salinity results
salinity.ref <- .data %>%
dplyr::filter(TADA.CharacteristicName %in% c("SALINITY")) %>%
Expand All @@ -145,7 +141,6 @@ TADA_CreatePairRef <- function(.data, ph = TRUE, hardness = TRUE, temp = TRUE,


if (chloride == TRUE) {

# filter TADA df for chloride results
chloride.ref <- .data %>%
dplyr::filter(TADA.CharacteristicName %in% c("CHLORIDE")) %>%
Expand All @@ -155,7 +150,7 @@ TADA_CreatePairRef <- function(.data, ph = TRUE, hardness = TRUE, temp = TRUE,
# add chloride to pair.ref
pair.ref <- rbind(pair.ref, chloride.ref)

#remove intermediate object
# remove intermediate object
rm(chloride.ref)
}

Expand Down Expand Up @@ -199,7 +194,7 @@ TADA_CreatePairRef <- function(.data, ph = TRUE, hardness = TRUE, temp = TRUE,
#' @param .data TADA dataframe
#'
#' @param ref Write description of what columns need to be in this ref or null option
#'
#'
#' @param hours_range Numeric argument. The time difference allowed between the paired characteristic
#' and the result.
#
Expand Down
12 changes: 8 additions & 4 deletions R/DataDiscoveryRetrieval.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,16 @@
#' @examples
#' \dontrun{
#' # example for WI
#' tada1 <- TADA_DataRetrieval(statecode = "WI", countycode = "Dane",
#' characteristicName = "Phosphorus")
#' tada1 <- TADA_DataRetrieval(
#' statecode = "WI", countycode = "Dane",
#' characteristicName = "Phosphorus"
#' )
#'
#' # example for UT
#' tada2 <- TADA_DataRetrieval(statecode = "UT",
#' characteristicName = c("Ammonia", "Nitrate", "Nitrogen"))
#' tada2 <- TADA_DataRetrieval(
#' statecode = "UT",
#' characteristicName = c("Ammonia", "Nitrate", "Nitrogen")
#' )
#'
#' # example for SC
#' tada3 <- TADA_DataRetrieval(statecode = "SC", countycode = "Abbeville")
Expand Down
103 changes: 51 additions & 52 deletions R/Figures.R
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ TADA_TwoCharacteristicScatterplot <- function(.data, id_cols = "TADA.ComparableD
#'
#' @param group_col The column in the dataset used to identify the groups
#' plotted. Defaults to MonitoringLocationName. This input is flexible, and allows for the use of
#' other identifiers such as StateCode, CountyCode or user-created groups based on concatenation
#' other identifiers such as StateCode, CountyCode or user-created groups based on concatenation
#' of other variables (e.g. characteristic name, site type, site name, year, organization, etc.)
#'
#' @param groups A vector of up to four identifiers from the id_cols column
Expand All @@ -1164,7 +1164,7 @@ TADA_TwoCharacteristicScatterplot <- function(.data, id_cols = "TADA.ComparableD
#' 'Upper Red Lake: West', 'Upper Red Lake: West-Central', and 'Upper Red Lake: East Central'.
#'
#' @return A plotly scatterplot(s) figure with one x-axis (Date/Time) and a
#' left axis showing the units of a single TADA.ComparableDataIdentifier plotted on the same
#' left axis showing the units of a single TADA.ComparableDataIdentifier plotted on the same
#' figure area with. Groups are identified by different colored circle markers and are displayed
#' in a legend.
#'
Expand Down Expand Up @@ -1272,15 +1272,14 @@ TADA_GroupedScatterplot <- function(.data, group_col = "MonitoringLocationName",
" ", group_col, "s by number of results will be plotted: ", groups.string, ".",
sep = ""
))

# remove intermediate objects
rm(groups.string, n.groups.plotted)
}

# check that groups are in group_col
id <- unlist(unique(.data[, group_col]))
if (any(!groups %in% id)) {

# identify any groups missing from "groups" param
missing.groups <- setdiff(groups, id)

Expand All @@ -1292,7 +1291,7 @@ TADA_GroupedScatterplot <- function(.data, group_col = "MonitoringLocationName",
missing.groups.string, ". Revise param 'groups' before re-running function.",
sep = ""
)

# remove intermediate objects
rm(missing.group, missing.groups.string, id)
}
Expand All @@ -1311,7 +1310,7 @@ TADA_GroupedScatterplot <- function(.data, group_col = "MonitoringLocationName",
param.data <- list()
for (i in 1:length(unique(groups))) {
param.data[[i]] <- subset(plot.data, plot.data[, group_col] %in% groups[i])
#assign(paste0("param", as.character(i)), subset(plot.data, plot.data[, group_col] %in% groups[i]))
# assign(paste0("param", as.character(i)), subset(plot.data, plot.data[, group_col] %in% groups[i]))
}

# create empty list to store scatterplots
Expand Down Expand Up @@ -1344,7 +1343,7 @@ TADA_GroupedScatterplot <- function(.data, group_col = "MonitoringLocationName",
plot.data.y <- subset(plot.data, plot.data[, "TADA.ComparableDataIdentifier"] %in% unique(plot.data$TADA.ComparableDataIdentifier)[i])
plot.data.y$name <- gsub("_NA", "", plot.data.y[, "TADA.ComparableDataIdentifier"])
plot.data.y$name <- gsub("_", " ", plot.data.y$name)

scatterplot <-
plotly::plot_ly(type = "scatter", mode = "markers") %>%
plotly::layout(
Expand All @@ -1367,68 +1366,68 @@ TADA_GroupedScatterplot <- function(.data, group_col = "MonitoringLocationName",
plot_bgcolor = "#e5ecf6",
margin = mrg,
legend = list(
title = list(text = paste0('<b>', group_col,'<b>'), x = 0.5, y= 100),
title = list(text = paste0("<b>", group_col, "<b>"), x = 0.5, y = 100),
orientation = "h",
xanchor = "center",
x = 0.5
)
) %>%
# config options https://plotly.com/r/configuration-options/
plotly::config(displaylogo = FALSE) # , displayModeBar = TRUE) # TRUE makes bar always visible

param <- list()
for (j in 1:length(groups)) {
if ( length(groups) >= j){
param[[j]] <- subset(param.data[[j]], param.data[[j]][, "TADA.ComparableDataIdentifier"] %in% unique(plot.data$TADA.ComparableDataIdentifier)[i])

scatterplot <- scatterplot %>%
plotly::add_trace(
data = param[[j]],
x = ~ as.Date(ActivityStartDate),
y = ~TADA.ResultMeasureValue,
name = groups[j],
marker = list(
size = 10,
color = tada.pal[j, 1],
line = list(color = tada.pal[j, 2], width = 2)
),
hoverinfo = "text",
hovertext = paste(
"Result:", paste0(param[[j]]$TADA.ResultMeasureValue, " ", param[[j]]$TADA.ResultMeasure.MeasureUnitCode), "<br>",
"Activity Start Date:", param[[j]]$ActivityStartDate, "<br>",
"Activity Start Date Time:", param[[j]]$ActivityStartDateTime, "<br>",
"Monitoring Location Name:", param[[j]]$MonitoringLocationName, "<br>",
"Media:", param[[j]]$TADA.ActivityMediaName, "<br>",
"Media Subdivision:", param[[j]]$ActivityMediaSubdivisionName, "<br>",
"Result Depth:", paste0(
param[[j]]$TADA.ResultDepthHeightMeasure.MeasureValue, " ",
param[[j]]$TADA.ResultDepthHeightMeasure.MeasureUnitCode
), "<br>",
"Activity Relative Depth Name:", param[[j]]$ActivityRelativeDepthName, "<br>",
"Activity Depth:", paste0(
param[[j]]$TADA.ActivityDepthHeightMeasure.MeasureValue, " ",
param[[j]]$TADA.ActivityDepthHeightMeasure.MeasureUnitCode
), "<br>",
"Activity Top Depth:", paste0(
param[[j]]$TADA.ActivityTopDepthHeightMeasure.MeasureValue, " ",
param[[j]]$TADA.ActivityTopDepthHeightMeasure.MeasureUnitCode
), "<br>",
"Activity Bottom Depth:", paste0(
param[[j]]$TADA.ActivityBottomDepthHeightMeasure.MeasureValue, " ",
param[[j]]$TADA.ActivityBottomDepthHeightMeasure.MeasureUnitCode
), "<br>"
if (length(groups) >= j) {
param[[j]] <- subset(param.data[[j]], param.data[[j]][, "TADA.ComparableDataIdentifier"] %in% unique(plot.data$TADA.ComparableDataIdentifier)[i])

scatterplot <- scatterplot %>%
plotly::add_trace(
data = param[[j]],
x = ~ as.Date(ActivityStartDate),
y = ~TADA.ResultMeasureValue,
name = groups[j],
marker = list(
size = 10,
color = tada.pal[j, 1],
line = list(color = tada.pal[j, 2], width = 2)
),
hoverinfo = "text",
hovertext = paste(
"Result:", paste0(param[[j]]$TADA.ResultMeasureValue, " ", param[[j]]$TADA.ResultMeasure.MeasureUnitCode), "<br>",
"Activity Start Date:", param[[j]]$ActivityStartDate, "<br>",
"Activity Start Date Time:", param[[j]]$ActivityStartDateTime, "<br>",
"Monitoring Location Name:", param[[j]]$MonitoringLocationName, "<br>",
"Media:", param[[j]]$TADA.ActivityMediaName, "<br>",
"Media Subdivision:", param[[j]]$ActivityMediaSubdivisionName, "<br>",
"Result Depth:", paste0(
param[[j]]$TADA.ResultDepthHeightMeasure.MeasureValue, " ",
param[[j]]$TADA.ResultDepthHeightMeasure.MeasureUnitCode
), "<br>",
"Activity Relative Depth Name:", param[[j]]$ActivityRelativeDepthName, "<br>",
"Activity Depth:", paste0(
param[[j]]$TADA.ActivityDepthHeightMeasure.MeasureValue, " ",
param[[j]]$TADA.ActivityDepthHeightMeasure.MeasureUnitCode
), "<br>",
"Activity Top Depth:", paste0(
param[[j]]$TADA.ActivityTopDepthHeightMeasure.MeasureValue, " ",
param[[j]]$TADA.ActivityTopDepthHeightMeasure.MeasureUnitCode
), "<br>",
"Activity Bottom Depth:", paste0(
param[[j]]$TADA.ActivityBottomDepthHeightMeasure.MeasureValue, " ",
param[[j]]$TADA.ActivityBottomDepthHeightMeasure.MeasureUnitCode
), "<br>"
)
)
)
}
}
}

# create plots and store as list
all_scatterplots[[i]] <- scatterplot

# rename scatterplots to reflect TADA.ComparbaleDataIdentifier (with NAs removed)
names(all_scatterplots)[i] <- unique(TADA_CharStringRemoveNA(plot.data$TADA.ComparableDataIdentifier))[i]
}

# filter to return one scatterplot, if only one was generated
if (length(all_scatterplots) == 1) {
all_scatterplots <- all_scatterplots[[1]]
Expand Down
14 changes: 9 additions & 5 deletions R/Filtering.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,17 @@ TADA_FieldValuesTable <- function(.data, field = "null", characteristicName = "n
#' @examples
#' data(Data_6Tribes_5y_Harmonized)
#' # Returns data with ONLY surface water results retained and no TADA.UseForAnalysis.Flag column
#' Data_6Tribes_Assessment1 <- TADA_AnalysisDataFilter(Data_6Tribes_5y_Harmonized, clean = TRUE,
#' surface_water = TRUE, ground_water = FALSE, sediment = FALSE)
#' Data_6Tribes_Assessment1 <- TADA_AnalysisDataFilter(Data_6Tribes_5y_Harmonized,
#' clean = TRUE,
#' surface_water = TRUE, ground_water = FALSE, sediment = FALSE
#' )
#'
#' # Returns data frame with ONLY surface water results identified as usable and adds
#' # Returns data frame with ONLY surface water results identified as usable and adds
#' # TADA.UseForAnalysis.Flag column.
#' Data_6Tribes_Assessment2 <- TADA_AnalysisDataFilter(Data_6Tribes_5y_Harmonized, clean = FALSE,
#' surface_water = TRUE, ground_water = FALSE, sediment = FALSE)
#' Data_6Tribes_Assessment2 <- TADA_AnalysisDataFilter(Data_6Tribes_5y_Harmonized,
#' clean = FALSE,
#' surface_water = TRUE, ground_water = FALSE, sediment = FALSE
#' )
#' unique(Data_6Tribes_Assessment2$TADA.UseForAnalysis.Flag)
#'
TADA_AnalysisDataFilter <- function(.data,
Expand Down
Loading

0 comments on commit 2420ddc

Please sign in to comment.