From d221275cc042bc34016b13c6aa97e5641a37628e Mon Sep 17 00:00:00 2001 From: Kenny Wong Date: Wed, 8 Jan 2025 17:33:19 -0500 Subject: [PATCH] Removed AU_ML crosswalk draft from this branch The next pr will focus on the AU crosswork and Defining Magnitude Summary functions. This branch and new updates reflect some modification to mod 3 vignette workflow and some edits to TADA_CreateParamUseRef() --- R/CriteriaDefine.R | 193 --------------------- R/CriteriaInputs.R | 332 ++++++++++++++++++------------------ man/TADA_CreateAURef.Rd | 37 ---- man/TADA_DefineMagnitude.Rd | 31 ---- vignettes/TADAModule3.Rmd | 8 +- 5 files changed, 170 insertions(+), 431 deletions(-) delete mode 100644 R/CriteriaDefine.R delete mode 100644 man/TADA_CreateAURef.Rd delete mode 100644 man/TADA_DefineMagnitude.Rd diff --git a/R/CriteriaDefine.R b/R/CriteriaDefine.R deleted file mode 100644 index 67490f6e..00000000 --- a/R/CriteriaDefine.R +++ /dev/null @@ -1,193 +0,0 @@ -#' Define Standards -#' @param .data A TADA dataframe. Users should run the appropriate data cleaning, -#' processing, harmonization and filtering functions prior to this step. -#' -#' @return A data frame with all allowable ATTAINS designated use values for an ATTAINS Parameter -#' -#' @export -#' -#' @examples -#' Data_Nutrients_UT_ATTAINS <- load("data.Rda") -#' Data_Nutrients_Param_Ref <- TADA_CreateParamUseRef(Data_Nutrients_UT) -#' - -TADA_DefineMagnitude <- function(.data, ref = "TADA", paramRef = NULL, paramUseRef = NULL, AUIDRef = NULL, excel = TRUE, overwrite = FALSE){ - - downloads_path <- file.path(Sys.getenv("USERPROFILE"), "Downloads", "myfileRef.xlsx") - - if(is.null(paramRef)){ - paramRef <- openxlsx::read.xlsx(downloads_path, sheet = "CreateParamRef") - } - - if(is.null(paramUseRef)){ - paramUseRef <- openxlsx::read.xlsx(downloads_path, sheet = "CreateParamUseRef") - } - - # if(is.null(AUIDRef)){ - # AUIDRef <- openxlsx::read.xlsx(downloads_path, sheet = "CreateAUIDRef") - # } - - # check to see if user-supplied parameter ref is a df with appropriate columns and filled out. - if (!is.null(paramUseRef) & !is.character(paramUseRef)) { - if (!is.data.frame(paramUseRef)) { - stop("TADA_DefineMagnitude: 'paramUseRef' must be a data frame with five columns: organization_name, ATTAINS.ParameterName, use_name, nrowsStandardsRep") - } - - if (is.data.frame(paramUseRef)) { - col.names <- c( - "organization_name", "ATTAINS.ParameterName", "use_name" - ) - - ref.names <- names(paramUseRef) - - if (length(setdiff(col.names, ref.names)) > 0) { - stop("TADA_DefineMagnitude: 'paramUseRef' must be a data frame with five columns: organization_name, ATTAINS.ParameterName, use_name") - } - } - } - - # if (!is.character(AUIDRef)) { - # if (!is.data.frame(AUIDRef)) { - # stop("TADA_DefineMagnitude: 'AUIDRef' must be a data frame with seven columns: ATTAINS.assessmentunitname, ATTAINS.assessmentunitidentifier, MonitoringLocationIdentifier, MonitoringLocationName, MonitoringLocationTypeName, LongitudeMeasure, LatitudeMeasure") - # } - # - # if (is.data.frame(AUIDRef)) { - # col.names <- c( - # "ATTAINS.assessmentunitname","ATTAINS.assessmentunitidentifier", - # "MonitoringLocationIdentifier", "MonitoringLocationName", "MonitoringLocationTypeName", "LongitudeMeasure", "LatitudeMeasure" - # ) - # - # ref.names <- names(AUIDRef) - # - # if (length(setdiff(col.names, ref.names)) > 0) { - # stop("TADA_DefineMagnitude: 'AUIDRef' must be a data frame with seven columns: ATTAINS.assessmentunitname, ATTAINS.assessmentunitidentifier, MonitoringLocationIdentifier, MonitoringLocationName, MonitoringLocationTypeName, LongitudeMeasure, LatitudeMeasure") - # } - # } - # } - # - # if(sum(is.na(AUIDRef$SiteSpecificName)) > 1){ - # print("NAs were found in column SiteSpecificName for your AUIDRef. Please ensure that you have inputted all field values of interest in the SiteSpecificName column that defines your organization's unique site-specific mapping criteria") - # } - - wb <- openxlsx::loadWorkbook(wb, downloads_path) - tryCatch({ - openxlsx::addWorksheet(wb, "DefineMagnitude") - }, - error = function(e){ - openxlsx::removeWorksheet(wb, "DefineMagnitude") - openxlsx::addWorksheet(wb, "DefineMagnitude") - } - ) - - # Format column header - header_st <- openxlsx::createStyle(textDecoration = "Bold") - # set zoom size - set_zoom <- function(x) gsub('(?<=zoomScale=")[0-9]+', x, sV, perl = TRUE) - sV <- wb$worksheets[[5]]$sheetViews - wb$worksheets[[5]]$sheetViews <- set_zoom(90) - - columns <- c( - "EPA304A.PollutantName", "ATTAINS.ParameterName", "organization_name", "use_name", - "ATTAINS.WaterType", "AcuteChronic", "BegAssessDate", "EndAssessDate", - "Season", "MinSamplePerDuration", "SiteSpecificName", "ExcludeStationReason", - "EquationBased", "StandardValue", "StandardUnit" - ) - - DefineMagnitude <- paramRef %>% - dplyr::full_join(paramUseRef, by = c("EPA304A.PollutantName", "ATTAINS.ParameterName", "organization_name"), relationship = "many-to-many") %>% - dplyr::select("EPA304A.PollutantName", "ATTAINS.ParameterName", "organization_name", "use_name") %>% - dplyr::bind_cols( - data.frame( - ATTAINS.WaterType = as.character(NA), AcuteChronic = as.character(NA), BegAssessDate = as.Date(NA), - EndAssessDate = as.Date(NA), Season = as.character(NA), MinimumSample = as.numeric(NA), SiteSpecificName = as.character(NA), ExcludeStationReason = as.character(NA), - EquationBased = as.character(NA), MagnitudeValue = as.character(NA), MagnitudeUnit = as.character(NA) - ) - ) %>% - dplyr::filter(ATTAINS.ParameterName != "Parameter not used for assessment") %>% - dplyr::distinct() %>% - dplyr::arrange(organization_name != "EPA304a", organization_name) - - CST_param <- utils::read.csv(system.file("extdata", "CST.csv", package = "EPATADA")) %>% - dplyr::select(EPA304A.PollutantName = POLLUTANT_NAME, use_name, CRITERIATYPE_ACUTECHRONIC, CRITERION_VALUE, UNIT_NAME) %>% - dplyr::mutate(organization_name = "EPA304a") - - if("EPA304a" %in% DefineMagnitude$organization_name){ - DefineMagnitude <- DefineMagnitude %>% - dplyr::left_join(CST_param, c("EPA304A.PollutantName", "use_name", "organization_name"), relationship = "many-to-many") %>% - dplyr::mutate(AcuteChronic = CRITERIATYPE_ACUTECHRONIC) %>% - dplyr::mutate(MagnitudeValue = CRITERION_VALUE) %>% - dplyr::mutate(MagnitudeUnit = UNIT_NAME) %>% - dplyr::select(-c(CRITERIATYPE_ACUTECHRONIC, CRITERION_VALUE)) %>% - dplyr::distinct() %>% - dplyr::arrange(organization_name != "EPA304a", organization_name) - - } - - paramRef$ATTAINS.ParameterName <- as.character(paramRef$ATTAINS.ParameterName) - # Pulls in all the units that are found in TADA.ResultMeasure.MeasureUnitCode as unique allowable unit column - StandardValue <- paramRef %>% - dplyr::left_join( - (dplyr::select(.data, "TADA.ComparableDataIdentifier", "TADA.ResultMeasure.MeasureUnitCode") %>% - dplyr::distinct() %>% - tidyr::drop_na(TADA.ResultMeasure.MeasureUnitCode) - ), - by = c("TADA.ComparableDataIdentifier"), relationship = "many-to-many") %>% - dplyr::select(ATTAINS.ParameterName, TADA.ResultMeasure.MeasureUnitCode) %>% - dplyr::distinct() %>% - dplyr::right_join(DefineMagnitude, by = c("ATTAINS.ParameterName"), relationship = "many-to-many") %>% - dplyr::select(TADA.ResultMeasure.MeasureUnitCode) %>% - dplyr::rename(StandardUnit = TADA.ResultMeasure.MeasureUnitCode) - - # Format column header - header_st <- openxlsx::createStyle(textDecoration = "Bold") - # Format Column widths - openxlsx::setColWidths(wb, "DefineMagnitude", cols = 1:ncol(DefineMagnitude), widths = "auto") - - # Write column names in the excel spreadsheet under the tab [DefineMagnitude] - #writeData(wb, "DefineMagnitude", startCol = 1, x = par, headerStyle = header_st) - # Export DefineMagnitude dataframe into the excel spreadsheet tab - openxlsx::writeData(wb, "DefineMagnitude", startCol = 1, x = DefineMagnitude, headerStyle = header_st) - #writeData(wb, "DefineMagnitude", startCol = 13, startRow = 1, x = StandardValue) - - openxlsx::writeData(wb, "Index", startCol = 9, startRow = 1, x = data.frame(ATTAINS.WaterType = c(unique(.data$MonitoringLocationTypeName), "All Others", "AddWaterTypeHere", "NA"))) # ATTAINS.WaterType - openxlsx::writeData(wb, "Index", startCol = 10, startRow = 1, x = data.frame(AcuteChronic = c("Acute", "Chronic", "NA"))) # AcuteChronic - openxlsx::writeData(wb, "Index", startCol = 11, startRow = 1, x = data.frame(Season = c("Summer", "Fall", "Spring", "Winter", "NA"))) #Season - - openxlsx::writeData(wb, "Index", startCol = 12, startRow = 1, x = data.frame(SiteSpecificName = c(unique(AUIDRef$SiteSpecificName), "AddSiteNameHere", "NA")))# SiteSpecificName - openxlsx::writeData(wb, "Index", startCol = 13, startRow = 1, x = data.frame(ExcludeStationReason = c(unique(AUIDRef$ExcludeStationReason), "NA"))) # ExcludeStationReason - openxlsx::writeData(wb, "Index", startCol = 14, startRow = 1, x = data.frame(EquationBased = c("Yes", "No", "NA"))) # EquationBased - - openxlsx::writeData(wb, "Index", startCol = 15, startRow = 1, x = data.frame(StandardUnit = unique(.data$TADA.ResultMeasure.MeasureUnitCode))) # StandardUnit - - # The list of allowable values for each column in excel tab [DefineMagnitude] will be defined by the [Index] tab - suppressWarnings(openxlsx::dataValidation(wb, sheet = "DefineMagnitude", cols = 5, rows = 2:1000, type = "list", value = sprintf("'Index'!$I$2:$I$1000"), allowBlank = TRUE, showErrorMsg = TRUE, showInputMsg = TRUE)) # ATTAINS.WaterType - suppressWarnings(openxlsx::dataValidation(wb, sheet = "DefineMagnitude", cols = 6, rows = 2:1000, type = "list", value = sprintf("'Index'!$J$2:$J$1000"), allowBlank = TRUE, showErrorMsg = TRUE, showInputMsg = TRUE)) # AcuteChronic - suppressWarnings(openxlsx::dataValidation(wb, sheet = "DefineMagnitude", cols = 9, rows = 2:1000, type = "list", value = sprintf("'Index'!$K$2:$K$1000"), allowBlank = TRUE, showErrorMsg = TRUE, showInputMsg = TRUE)) # Season - suppressWarnings(openxlsx::dataValidation(wb, sheet = "DefineMagnitude", cols = 11, rows = 2:1000, type = "list", value = sprintf("'Index'!$L$2:$L$1000"), allowBlank = TRUE, showErrorMsg = TRUE, showInputMsg = TRUE)) # SiteSpecificName - suppressWarnings(openxlsx::dataValidation(wb, sheet = "DefineMagnitude", cols = 13, rows = 2:1000, type = "list", value = sprintf("'Index'!$M$2:$M$1000"), allowBlank = TRUE, showErrorMsg = TRUE, showInputMsg = TRUE)) # ExcludeSiteReason - suppressWarnings(openxlsx::dataValidation(wb, sheet = "DefineMagnitude", cols = 14, rows = 2:1000, type = "list", value = sprintf("'Index'!$N$2:$N$1000"), allowBlank = TRUE, showErrorMsg = TRUE, showInputMsg = TRUE)) # EquationBased - suppressWarnings(openxlsx::dataValidation(wb, sheet = "DefineMagnitude", cols = 15, rows = 2:1000, type = "list", value = sprintf("'Index'!$O$2:$O$1000"), allowBlank = TRUE, showErrorMsg = TRUE, showInputMsg = TRUE)) # StandardUnit - suppressWarnings(openxlsx::dataValidation(wb, sheet = "DefineMagnitude", cols = 16, rows = 2:1000, type = "list", value = sprintf("'Index'!$P$2:$P$1000"), allowBlank = TRUE, showErrorMsg = TRUE, showInputMsg = TRUE)) # StandardLimit - - # Conditional Formatting - openxlsx::conditionalFormatting(wb, "DefineMagnitude", - cols = 5:16, rows = 2:(nrow(DefineMagnitude) + 1), - type = "notBlanks", style = openxlsx::createStyle(bgFill = TADA_ColorPalette()[8])) # default values or indicates good to go cells. - openxlsx::conditionalFormatting(wb, "DefineMagnitude", - cols = 5:16, rows = 2:(nrow(DefineMagnitude) + 1), - type = "blanks", style = openxlsx::createStyle(bgFill = TADA_ColorPalette()[13])) # default values or indicates good to go cells. - - # Saving of the file if overwrite = TRUE or if the file is not found in the defined folder path. If is not saved, a dataframe is still returned. - if(overwrite == TRUE){ - openxlsx::saveWorkbook(wb, downloads_path, overwrite = T) - } - - if(overwrite == FALSE){ - warning("If you would like to replace the file, use overwrite = TRUE argument in TADA_CreateParamRef") - openxlsx::saveWorkbook(wb, downloads_path, overwrite = F) - } - - cat("File saved to:", gsub("/","\\\\",downloads_path), "\n") - - return(DefineMagnitude) -} diff --git a/R/CriteriaInputs.R b/R/CriteriaInputs.R index 0be11548..80816aee 100644 --- a/R/CriteriaInputs.R +++ b/R/CriteriaInputs.R @@ -774,171 +774,171 @@ TADA_CreateParamUseRef <- function(.data, org_names = NULL, paramRef = NULL, par return(CreateParamUseRef) } - -#' Assessment Unit and MonitoringLocationName/MonitoringLocationType/MonitoringLocationId Crosswalk -#' -#' This function will pull in all MonitoringLocationName/MonitoringLocationType/MonitoringLocationId -#' for AU(s) from a TADA dataframe with ATTAINS data. This function requires users to have already -#' ran TADA_GetATTAINS(). Users are able to specify which AU(S) to pull in from this ref file -#' when creating the final CriteriaRef file to be compatible with an organization's WQS assessments. -#' -#' Users are expected to modify this AU ref file with the appropriate AU and -#' MonitoringLocationName/MonitoringLocationType/MonitoringLocationId crosswalk -#' for the current Assessment cycle. Users can decide to "Include or Exclude" a MonitoringLocation -#' within an AU if desired. This can be used if a MoniotringLocation would still like to be -#' crosswalk to the AU but may only be applicable for certain parameters. -#' -#' @param .data A TADA dataframe with TADA_GetATTAINS() geospatial function ran. #' -#' @param AU Character argument. Users can specify which AU they are interested in -#' defining WQS criteria for. If this argument is left as NULL, then all unique AU -#' records will be displayed in this ref file for users to define. +#' #' Assessment Unit and MonitoringLocationName/MonitoringLocationType/MonitoringLocationId Crosswalk +#' #' +#' #' This function will pull in all MonitoringLocationName/MonitoringLocationType/MonitoringLocationId +#' #' for AU(s) from a TADA dataframe with ATTAINS data. This function requires users to have already +#' #' ran TADA_GetATTAINS(). Users are able to specify which AU(S) to pull in from this ref file +#' #' when creating the final CriteriaRef file to be compatible with an organization's WQS assessments. +#' #' +#' #' Users are expected to modify this AU ref file with the appropriate AU and +#' #' MonitoringLocationName/MonitoringLocationType/MonitoringLocationId crosswalk +#' #' for the current Assessment cycle. Users can decide to "Include or Exclude" a MonitoringLocation +#' #' within an AU if desired. This can be used if a MoniotringLocation would still like to be +#' #' crosswalk to the AU but may only be applicable for certain parameters. +#' #' +#' #' @param .data A TADA dataframe with TADA_GetATTAINS() geospatial function ran. +#' #' +#' #' @param AU Character argument. Users can specify which AU they are interested in +#' #' defining WQS criteria for. If this argument is left as NULL, then all unique AU +#' #' records will be displayed in this ref file for users to define. +#' #' +#' #' @return A data frame with all the MonitoringLocationIdentifier Sites for a defined AU. +#' #' +#' #' @export +#' #' #' -#' @return A data frame with all the MonitoringLocationIdentifier Sites for a defined AU. -#' -#' @export -#' - -TADA_CreateAURef <- function(.data, AU = NULL, excel = TRUE, overwrite = FALSE, returnSites = c("all","matched-only")){ - - # data <- rATTAINS::assessments(organization_id = "MDE_EASP") - # - # use_assessments <- data$use_assessment - # use_attainments <- use_assessments %>% unnest(c(use_attainments), names_sep = ".") - # use_parameters <- use_attainments %>% unnest(c(parameters), names_sep = ".") - # - # use_data <- use_parameters %>% - # dplyr::select( - # organization_identifier, organization_name, organization_type_text, - # use_attainments.use_name, parameters.parameter_name) %>% - # distinct() - # - # rm(use_assessments, use_attainments, use_parameters) - # - # - # If user does not define the path, attempt to pull in the ref files from the default Downloads location. - downloads_path <- file.path(Sys.getenv("USERPROFILE"), "Downloads", "myfileRef.xlsx") - # testing out different downloads_path as an argument is needed. - if(!is.null(downloads_path)){ - downloads_path <- downloads_path - } - - library(rATTAINS) - - if(!is.data.frame(.data)){ - if (!any(c( - "TADA_with_ATTAINS", "ATTAINS_catchments", "ATTAINS_points", "ATTAINS_lines", "ATTAINS_polygons" - ) %in% names(.data))) { - stop("Your input dataframe was not produced from `TADA_GetATTAINS()` or it was modified. Please create your list of ATTAINS features using `TADA_GetATTAINS(return_sf = TRUE)`") - } - - .data <- .data[["TADA_with_ATTAINS"]] - } - - - if(is.null(AU)){ - print("Creating AURef dataframe for all unique combinations of AU found in the TADA dataframe by MonitoringLocationName/MonitoringLocationType/MonitoringLocationId.") - } - - if(!is.null(AU)){ - print(paste0("Filtering by AUs = ", AU, ". Creating a dataframe for unique combinations of MonitoringLocationName/MonitoringLocationType/MonitoringLocationId.")) - } - - # Filters by AU if desired, otherwise creates a dataframe of all unique AU in the TADA dataframe pull - CreateAURef <- .data %>% - dplyr::filter(if (is.null(AU)) TRUE - else ATTAINS.assessmentunitidentifier == AU - ) %>% - dplyr::select(tidyr::any_of(c( - "ATTAINS.assessmentunitname","ATTAINS.assessmentunitidentifier", - "MonitoringLocationIdentifier", "MonitoringLocationName", "MonitoringLocationTypeName", "ATTAINS.WaterType", "LongitudeMeasure", "LatitudeMeasure" - )) - ) %>% - as.data.frame() %>% - sf::st_drop_geometry() %>% - dplyr::distinct(.keep_all = FALSE) %>% - dplyr::mutate(IncludeOrExclude = "Include") %>% - dplyr::mutate(ExcludeStationReason = NA) %>% # Users can customize this for "warm waters", "only trout based waters" etc. - dplyr::mutate(ApplyUniqueSpatialCriteria = NA) %>% - dplyr::arrange( - #ATTAINS.assessmentunitname, ATTAINS.assessmentunitidentifier, - MonitoringLocationIdentifier - ) - - if(!"ATTAINS.assessmentunitidentifier" %in% colnames(CreateAURef)){ - print(paste0("No Monitoring Location to Assessment Unit crosswalk provided. Consider providing this crosswalk if you would like to summarize assessments on an Assessment Unit level.")) - - CreateAURef <- CreateAURef %>% - dplyr::mutate(ATTAINS.assessmentunitname = NA) %>% - dplyr::mutate(ATTAINS.assessmentunitidentifier = NA) %>% - dplyr::mutate(ATTAINS.WaterType = NA) %>% - dplyr::select(tidyr::any_of(c( - "ATTAINS.assessmentunitname","ATTAINS.assessmentunitidentifier", - "MonitoringLocationIdentifier", "MonitoringLocationName", "MonitoringLocationTypeName", "ATTAINS.WaterType", "LongitudeMeasure", "LatitudeMeasure", - "IncludeOrExclude", "ExcludeStationReason", "ApplyUniqueSpatialCriteria" - )) - ) - - } - - if (excel == TRUE) { - wb <- openxlsx::loadWorkbook(wb, downloads_path) - - tryCatch({ - openxlsx::addWorksheet(wb, "CreateAURef") - }, - error = function(e){ - openxlsx::removeWorksheet(wb, "CreateAURef") - openxlsx::addWorksheet(wb, "CreateAURef") - } - ) - - # Format column header - header_st <- openxlsx::createStyle(textDecoration = "Bold") - # Format Column widths - openxlsx::setColWidths(wb, "CreateAURef", cols = 8:ncol(CreateAURef), widths = "auto") - # set zoom size - set_zoom <- function(x) gsub('(?<=zoomScale=")[0-9]+', x, sV, perl = TRUE) - sV <- wb$worksheets[[4]]$sheetViews - wb$worksheets[[4]]$sheetViews <- set_zoom(90) - - # writes CreateAURef dataframe - openxlsx::writeData(wb, "CreateAURef", startCol = 1, x = CreateAURef, headerStyle = header_st) - - # data validation drop down list created below. - suppressWarnings(openxlsx::dataValidation(wb, sheet = "CreateAURef", cols = 9, rows = 2:1000, type = "list", value = sprintf("'Index'!$B$2:$B$5"), allowBlank = TRUE, showErrorMsg = TRUE, showInputMsg = TRUE)) - - - # Conditional Formatting - openxlsx::conditionalFormatting(wb, "CreateAURef", - cols = 9, rows = 2:(nrow(CreateAURef) + 1), - type = "contains", rule = "Include", style = openxlsx::createStyle(bgFill = TADA_ColorPalette()[9])) # default values or indicates good to go cells. - openxlsx::conditionalFormatting(wb, "CreateAURef", - cols = 9, rows = 2:(nrow(CreateAURef) + 1), - type = "contains", rule = "Exclude", style = openxlsx::createStyle(bgFill = TADA_ColorPalette()[8])) # using yellow to indicate modified cell - # conditionalFormatting(wb, "CreateAURef", - # cols = 8, rows = 2:(nrow(CreateAURef) + 1), - # type = "notContains", rule = c("Exclude","Include"), style = createStyle(bgFill = "red")) # Likely error. Invalid value is possible here. - openxlsx::conditionalFormatting(wb, "CreateAURef", - cols = 10:12, rows = 2:(nrow(CreateAURef) + 1), - type = "blanks", style = openxlsx::createStyle(bgFill = TADA_ColorPalette()[9])) # green is default values or indicates good to go cells. - openxlsx::conditionalFormatting(wb, "CreateAURef", - cols = 10:12, rows = 2:(nrow(CreateAURef) + 1), - type = "notBlanks", style = openxlsx::createStyle(bgFill = TADA_ColorPalette()[8])) # using yellow to indicate modified cell - - if(overwrite == TRUE){ - openxlsx::saveWorkbook(wb, downloads_path, overwrite = T) - } - - if(overwrite == FALSE){ - warning("If you would like to replace the file, use overwrite = TRUE argument in TADA_CreateParamRef") - openxlsx::saveWorkbook(wb, downloads_path, overwrite = F) - } - - cat("File saved to:", gsub("/","\\\\",downloads_path), "\n") - - CreateAURef <- openxlsx::read.xlsx(downloads_path, sheet = "CreateAURef") - } - - return(CreateAURef) -} +#' TADA_CreateAURef <- function(.data, AU = NULL, excel = TRUE, overwrite = FALSE, returnSites = c("all","matched-only")){ +#' +#' # data <- rATTAINS::assessments(organization_id = "MDE_EASP") +#' # +#' # use_assessments <- data$use_assessment +#' # use_attainments <- use_assessments %>% unnest(c(use_attainments), names_sep = ".") +#' # use_parameters <- use_attainments %>% unnest(c(parameters), names_sep = ".") +#' # +#' # use_data <- use_parameters %>% +#' # dplyr::select( +#' # organization_identifier, organization_name, organization_type_text, +#' # use_attainments.use_name, parameters.parameter_name) %>% +#' # distinct() +#' # +#' # rm(use_assessments, use_attainments, use_parameters) +#' # +#' # +#' # If user does not define the path, attempt to pull in the ref files from the default Downloads location. +#' downloads_path <- file.path(Sys.getenv("USERPROFILE"), "Downloads", "myfileRef.xlsx") +#' # testing out different downloads_path as an argument is needed. +#' if(!is.null(downloads_path)){ +#' downloads_path <- downloads_path +#' } +#' +#' library(rATTAINS) +#' +#' if(!is.data.frame(.data)){ +#' if (!any(c( +#' "TADA_with_ATTAINS", "ATTAINS_catchments", "ATTAINS_points", "ATTAINS_lines", "ATTAINS_polygons" +#' ) %in% names(.data))) { +#' stop("Your input dataframe was not produced from `TADA_GetATTAINS()` or it was modified. Please create your list of ATTAINS features using `TADA_GetATTAINS(return_sf = TRUE)`") +#' } +#' +#' .data <- .data[["TADA_with_ATTAINS"]] +#' } +#' +#' +#' if(is.null(AU)){ +#' print("Creating AURef dataframe for all unique combinations of AU found in the TADA dataframe by MonitoringLocationName/MonitoringLocationType/MonitoringLocationId.") +#' } +#' +#' if(!is.null(AU)){ +#' print(paste0("Filtering by AUs = ", AU, ". Creating a dataframe for unique combinations of MonitoringLocationName/MonitoringLocationType/MonitoringLocationId.")) +#' } +#' +#' # Filters by AU if desired, otherwise creates a dataframe of all unique AU in the TADA dataframe pull +#' CreateAURef <- .data %>% +#' dplyr::filter(if (is.null(AU)) TRUE +#' else ATTAINS.assessmentunitidentifier == AU +#' ) %>% +#' dplyr::select(tidyr::any_of(c( +#' "ATTAINS.assessmentunitname","ATTAINS.assessmentunitidentifier", +#' "MonitoringLocationIdentifier", "MonitoringLocationName", "MonitoringLocationTypeName", "ATTAINS.WaterType", "LongitudeMeasure", "LatitudeMeasure" +#' )) +#' ) %>% +#' as.data.frame() %>% +#' sf::st_drop_geometry() %>% +#' dplyr::distinct(.keep_all = FALSE) %>% +#' dplyr::mutate(IncludeOrExclude = "Include") %>% +#' dplyr::mutate(ExcludeStationReason = NA) %>% # Users can customize this for "warm waters", "only trout based waters" etc. +#' dplyr::mutate(ApplyUniqueSpatialCriteria = NA) %>% +#' dplyr::arrange( +#' #ATTAINS.assessmentunitname, ATTAINS.assessmentunitidentifier, +#' MonitoringLocationIdentifier +#' ) +#' +#' if(!"ATTAINS.assessmentunitidentifier" %in% colnames(CreateAURef)){ +#' print(paste0("No Monitoring Location to Assessment Unit crosswalk provided. Consider providing this crosswalk if you would like to summarize assessments on an Assessment Unit level.")) +#' +#' CreateAURef <- CreateAURef %>% +#' dplyr::mutate(ATTAINS.assessmentunitname = NA) %>% +#' dplyr::mutate(ATTAINS.assessmentunitidentifier = NA) %>% +#' dplyr::mutate(ATTAINS.WaterType = NA) %>% +#' dplyr::select(tidyr::any_of(c( +#' "ATTAINS.assessmentunitname","ATTAINS.assessmentunitidentifier", +#' "MonitoringLocationIdentifier", "MonitoringLocationName", "MonitoringLocationTypeName", "ATTAINS.WaterType", "LongitudeMeasure", "LatitudeMeasure", +#' "IncludeOrExclude", "ExcludeStationReason", "ApplyUniqueSpatialCriteria" +#' )) +#' ) +#' +#' } +#' +#' if (excel == TRUE) { +#' wb <- openxlsx::loadWorkbook(wb, downloads_path) +#' +#' tryCatch({ +#' openxlsx::addWorksheet(wb, "CreateAURef") +#' }, +#' error = function(e){ +#' openxlsx::removeWorksheet(wb, "CreateAURef") +#' openxlsx::addWorksheet(wb, "CreateAURef") +#' } +#' ) +#' +#' # Format column header +#' header_st <- openxlsx::createStyle(textDecoration = "Bold") +#' # Format Column widths +#' openxlsx::setColWidths(wb, "CreateAURef", cols = 8:ncol(CreateAURef), widths = "auto") +#' # set zoom size +#' set_zoom <- function(x) gsub('(?<=zoomScale=")[0-9]+', x, sV, perl = TRUE) +#' sV <- wb$worksheets[[4]]$sheetViews +#' wb$worksheets[[4]]$sheetViews <- set_zoom(90) +#' +#' # writes CreateAURef dataframe +#' openxlsx::writeData(wb, "CreateAURef", startCol = 1, x = CreateAURef, headerStyle = header_st) +#' +#' # data validation drop down list created below. +#' suppressWarnings(openxlsx::dataValidation(wb, sheet = "CreateAURef", cols = 9, rows = 2:1000, type = "list", value = sprintf("'Index'!$B$2:$B$5"), allowBlank = TRUE, showErrorMsg = TRUE, showInputMsg = TRUE)) +#' +#' +#' # Conditional Formatting +#' openxlsx::conditionalFormatting(wb, "CreateAURef", +#' cols = 9, rows = 2:(nrow(CreateAURef) + 1), +#' type = "contains", rule = "Include", style = openxlsx::createStyle(bgFill = TADA_ColorPalette()[9])) # default values or indicates good to go cells. +#' openxlsx::conditionalFormatting(wb, "CreateAURef", +#' cols = 9, rows = 2:(nrow(CreateAURef) + 1), +#' type = "contains", rule = "Exclude", style = openxlsx::createStyle(bgFill = TADA_ColorPalette()[8])) # using yellow to indicate modified cell +#' # conditionalFormatting(wb, "CreateAURef", +#' # cols = 8, rows = 2:(nrow(CreateAURef) + 1), +#' # type = "notContains", rule = c("Exclude","Include"), style = createStyle(bgFill = "red")) # Likely error. Invalid value is possible here. +#' openxlsx::conditionalFormatting(wb, "CreateAURef", +#' cols = 10:12, rows = 2:(nrow(CreateAURef) + 1), +#' type = "blanks", style = openxlsx::createStyle(bgFill = TADA_ColorPalette()[9])) # green is default values or indicates good to go cells. +#' openxlsx::conditionalFormatting(wb, "CreateAURef", +#' cols = 10:12, rows = 2:(nrow(CreateAURef) + 1), +#' type = "notBlanks", style = openxlsx::createStyle(bgFill = TADA_ColorPalette()[8])) # using yellow to indicate modified cell +#' +#' if(overwrite == TRUE){ +#' openxlsx::saveWorkbook(wb, downloads_path, overwrite = T) +#' } +#' +#' if(overwrite == FALSE){ +#' warning("If you would like to replace the file, use overwrite = TRUE argument in TADA_CreateParamRef") +#' openxlsx::saveWorkbook(wb, downloads_path, overwrite = F) +#' } +#' +#' cat("File saved to:", gsub("/","\\\\",downloads_path), "\n") +#' +#' CreateAURef <- openxlsx::read.xlsx(downloads_path, sheet = "CreateAURef") +#' } +#' +#' return(CreateAURef) +#' } diff --git a/man/TADA_CreateAURef.Rd b/man/TADA_CreateAURef.Rd deleted file mode 100644 index bc9bd4dc..00000000 --- a/man/TADA_CreateAURef.Rd +++ /dev/null @@ -1,37 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/CriteriaInputs.R -\name{TADA_CreateAURef} -\alias{TADA_CreateAURef} -\title{Assessment Unit and MonitoringLocationName/MonitoringLocationType/MonitoringLocationId Crosswalk} -\usage{ -TADA_CreateAURef( - .data, - AU = NULL, - excel = TRUE, - overwrite = FALSE, - returnSites = c("all", "matched-only") -) -} -\arguments{ -\item{.data}{A TADA dataframe with TADA_GetATTAINS() geospatial function ran.} - -\item{AU}{Character argument. Users can specify which AU they are interested in -defining WQS criteria for. If this argument is left as NULL, then all unique AU -records will be displayed in this ref file for users to define.} -} -\value{ -A data frame with all the MonitoringLocationIdentifier Sites for a defined AU. -} -\description{ -This function will pull in all MonitoringLocationName/MonitoringLocationType/MonitoringLocationId -for AU(s) from a TADA dataframe with ATTAINS data. This function requires users to have already -ran TADA_GetATTAINS(). Users are able to specify which AU(S) to pull in from this ref file -when creating the final CriteriaRef file to be compatible with an organization's WQS assessments. -} -\details{ -Users are expected to modify this AU ref file with the appropriate AU and -MonitoringLocationName/MonitoringLocationType/MonitoringLocationId crosswalk -for the current Assessment cycle. Users can decide to "Include or Exclude" a MonitoringLocation -within an AU if desired. This can be used if a MoniotringLocation would still like to be -crosswalk to the AU but may only be applicable for certain parameters. -} diff --git a/man/TADA_DefineMagnitude.Rd b/man/TADA_DefineMagnitude.Rd deleted file mode 100644 index 5b06bc9e..00000000 --- a/man/TADA_DefineMagnitude.Rd +++ /dev/null @@ -1,31 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/CriteriaDefine.R -\name{TADA_DefineMagnitude} -\alias{TADA_DefineMagnitude} -\title{Define Standards} -\usage{ -TADA_DefineMagnitude( - .data, - ref = "TADA", - paramRef = NULL, - paramUseRef = NULL, - AUIDRef = NULL, - excel = TRUE, - overwrite = FALSE -) -} -\arguments{ -\item{.data}{A TADA dataframe. Users should run the appropriate data cleaning, -processing, harmonization and filtering functions prior to this step.} -} -\value{ -A data frame with all allowable ATTAINS designated use values for an ATTAINS Parameter -} -\description{ -Define Standards -} -\examples{ -Data_Nutrients_UT_ATTAINS <- load("data.Rda") -Data_Nutrients_Param_Ref <- TADA_CreateParamUseRef(Data_Nutrients_UT) - -} diff --git a/vignettes/TADAModule3.Rmd b/vignettes/TADAModule3.Rmd index a77b3cce..729d6a90 100644 --- a/vignettes/TADAModule3.Rmd +++ b/vignettes/TADAModule3.Rmd @@ -358,7 +358,7 @@ add_data <- data.frame("organization_name" = "Maryland" ,"ATTAINS.ParameterName" # The output of this will not reflect changes to the ATTAINS.FlagUseName column. To do so, we need to re run TADA_CreateParamUseRef() with paramUseRef = ParamUseRef as an argument. ParamUseRef <- NCTC_ParamUseRef3 %>% dplyr::left_join(add_data, by = c("organization_name", "ATTAINS.ParameterName"), keep = FALSE) %>% - dplyr::mutate(use_name = coalesce(use_name.x, use_name.y)) %>% + dplyr::mutate(use_name = dplyr::coalesce(use_name.x, use_name.y)) %>% dplyr::select(-c(use_name.x, use_name.y)) # PH will now reflect the changes @@ -367,8 +367,8 @@ NCTC_ParamUseRef3.1 <- TADA_CreateParamUseRef( paramUseRef = ParamUseRef, # Edits were made to paramUseRef, updates flag column org_names = c("EPA304a", "Maryland"), paramRef = NCTC_ParamRef2, - # excel = FALSE # comment out 'excel = FALSE' and uncomment 'excel = TRUE, overwrite = TRUE' to run the excel file - excel = TRUE, overwrite = TRUE + excel = FALSE # comment out 'excel = FALSE' and uncomment 'excel = TRUE, overwrite = TRUE' to run the excel file + # excel = TRUE, overwrite = TRUE ) ``` @@ -389,6 +389,6 @@ responsible for filling out this unique spatial criteria under "ApplyUniqueSpati In this example below, no ML to AU crosswalk was provided. ```{r} -NCTC_AUIDRef <- TADA_CreateAURef(Data_NCTC, excel = TRUE, overwrite = TRUE) +# NCTC_AUIDRef <- TADA_CreateAURef(Data_NCTC, excel = TRUE, overwrite = TRUE) # NCTC_AUIDRef <- TADA_CreateAUIDRef(Data_NCTC, overwrite = TRUE) ```