diff --git a/.gitignore b/.gitignore index 5fd602198..b481559f7 100644 --- a/.gitignore +++ b/.gitignore @@ -13,56 +13,9 @@ TADA.Rproj # Session Data files .RData -# tempdata folder files for TADABigdataRetrieval -AL_raw_data.rds -AR_raw_data.rds -AZ_raw_data.rds -CA_raw_data.rds -CO_raw_data.rds -CT_raw_data.rds -DC_raw_data.rds -DE_raw_data.rds -FL_raw_data.rds -GA_raw_data.rds -ID_raw_data.rds -IA_raw_data.rds -IL_raw_data.rds -IN_raw_data.rds -KS_raw_data.rds -KY_raw_data.rds -LA_raw_data.rds -ME_raw_data.rds -MD_raw_data.rds -MA_raw_data.rds -MI_raw_data.rds -MN_raw_data.rds -MS_raw_data.rds -MO_raw_data.rds -MT_raw_data.rds -NE_raw_data.rds -NV_raw_data.rds -NH_raw_data.rds -NJ_raw_data.rds -NM_raw_data.rds -NY_raw_data.rds -NC_raw_data.rds -ND_raw_data.rds -OH_raw_data.rds -OK_raw_data.rds -OR_raw_data.rds -PA_raw_data.rds -RI_raw_data.rds -SC_raw_data.rds -SD_raw_data.rds -TN_raw_data.rds -TX_raw_data.rds -UT_raw_data.rds -VT_raw_data.rds -VA_raw_data.rds -WA_raw_data.rds -WV_raw_data.rds -WI_raw_data.rds -WY_raw_data.rds .Rdata .httr-oauth .DS_Store + +# +_snaps \ No newline at end of file diff --git a/DESCRIPTION b/DESCRIPTION index f4a340f26..bebfd989b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,6 +17,12 @@ Authors@R: role = "aut"), person(given = "Laura", family = "Shumway", + role = "aut"), + person(given = "Elise", + family = "Hinman", + role = "aut"), + person(given = "Kathleen", + family = "Healy", role = "aut") ) Description: Assists data partners in performing automated assessments with @@ -30,23 +36,23 @@ RoxygenNote: 7.2.3 Imports: plyr, data.table, - dataRetrieval, dplyr, - ggplot2, grDevices, magrittr, stringr, utils, RColorBrewer, stats, - gganimate, lubridate, - maps + maps, + dataRetrieval, + ggplot2, + gganimate, + gifski Depends: R (>= 3.5.0) Suggests: readr, - gifski, rlang, remotes, tidyverse, @@ -62,6 +68,6 @@ Suggests: VignetteBuilder: knitr, rmarkdown Language: en-US Config/testthat/edition: 3 -Config/testthat/load-all: list(export_all = FALSE, helpers = FALSE +Config/testthat/load-all: list(export_all = FALSE, helpers = FALSE) URL: https://usepa.github.io/TADA/, https://github.com/USEPA/TADA LazyData: true diff --git a/NAMESPACE b/NAMESPACE index 9b5b2976e..3d9d93572 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,7 +6,6 @@ export(AggregatedContinuousData) export(BelowNationalWQXLowerThreshold) export(ConvertDepthUnits) export(ConvertResultUnits) -export(CreateAnimatedMap) export(FilterFieldReview) export(FilterFields) export(FilterParFieldReview) diff --git a/R/DataDiscoveryRetrieval.R b/R/DataDiscoveryRetrieval.R index eabc46908..17d9a9df1 100644 --- a/R/DataDiscoveryRetrieval.R +++ b/R/DataDiscoveryRetrieval.R @@ -28,6 +28,10 @@ #' to the "ResultMeasureValue" and "DetectionLimitMeasureValue" columns; #' and to provide information about the result values that is needed to address #' censored data later on (i.e., nondetections) +#' +#' Users can reference the \href{https://www.epa.gov/waterdata/storage-and-retrieval-and-water-quality-exchange-domain-services-and-downloads}{WQX domain tables} +#' to find allowable vales for queries, e.g., reference the WQX domain table to find countycode and statecode: https://cdx.epa.gov/wqx/download/DomainValues/County_CSV.zip +#' Alternatively, you can use the WQP services to find areas where data is available in the US: https://www.waterqualitydata.us/Codes/countycode #' #' See ?MeasureValueSpecialCharacters and ?autoclean documentation for more information. #' @@ -37,7 +41,7 @@ #' @param siteid Unique monitoring station identifier #' @param siteType Type of waterbody #' @param characteristicName Name of parameter -#' @param ActivityMediaName Sampling substrate such as water, air, or sediment +#' @param sampleMedia Sampling substrate such as water, air, or sediment #' @param ProjectIdentifier A string of letters and/or numbers (some additional characters also possible) used to signify a project with data in the Water Quality Portal #' @param OrganizationIdentifier A string of letters and/or numbers (some additional characters also possible) used to signify an organization with data in the Water Quality Portal #' @param endDate End Date in the format YYYY-MM-DD @@ -52,14 +56,32 @@ #' tada1 <- TADAdataRetrieval(statecode = "WI", #' countycode = "Dane", #' characteristicName = "Phosphorus") +#' +#' tada2 <- TADAdataRetrieval(ProjectIdentifier = "Anchorage Bacteria 20-21") +#' +#' tada3 <- TADAdataRetrieval(statecode = "UT", +#' characteristicName = c("Ammonia", "Nitrate", "Nitrogen"), +#' startDate = "10-01-2020") +#' +#' tada4 <- TADAdataRetrieval(statecode = "SC", countycode = "Abbeville") +#' +#' # countycode queries require a statecode +#' tada5 <- TADAdataRetrieval(countycode = "US:02:020") +#' +#' tada6 <- TADABigdataRetrieval(startDate = "2019-01-01", +#' endDate = "2021-12-31", +#' characteristicName = "Temperature, water", +#' statecode = c("AK","AL")) +#' #' } +#' TADAdataRetrieval <- function(statecode = "null", startDate = "null", countycode = "null", siteid = "null", siteType = "null", characteristicName = "null", - ActivityMediaName = "null", + sampleMedia = "null", ProjectIdentifier = "null", OrganizationIdentifier = "null", endDate = "null", @@ -104,10 +126,10 @@ TADAdataRetrieval <- function(statecode = "null", WQPquery <- c(WQPquery, characteristicName = characteristicName) } - if (length(ActivityMediaName)>1) { - WQPquery <- c(WQPquery, ActivityMediaName = list(ActivityMediaName)) - } else if (ActivityMediaName != "null") { - WQPquery <- c(WQPquery, ActivityMediaName = ActivityMediaName) + if (length(sampleMedia)>1) { + WQPquery <- c(WQPquery, sampleMedia = list(sampleMedia)) + } else if (sampleMedia != "null") { + WQPquery <- c(WQPquery, sampleMedia = sampleMedia) } if (length(ProjectIdentifier)>1) { @@ -151,6 +173,7 @@ TADAdataRetrieval <- function(statecode = "null", Sites = sites.DR, Narrow = narrow.DR, Projects = projects.DR) + # run autoclean function if(applyautoclean==TRUE){ @@ -270,7 +293,7 @@ TADAReadWQPWebServices <- function(webservice) { #' @param startDate Start Date YYYY-MM-DD format, for example, "1995-01-01" #' @param endDate end date in YYYY-MM-DD format, for example, "2020-12-31" #' @param statecode Character/character vector. State/territory abbreviations from FIPS codes consist of two letters -#' @param huc An 8-digit numeric code denoting a hydrologic unit. Example: "04030202" +#' @param huc A numeric code denoting a hydrologic unit. Example: "04030202". Different size hucs can be entered. #' @param characteristicName Name of water quality parameter #' @param siteType Name of water body type (e.g., "Stream", "Lake, Reservoir, Impoundment") #' @param sampleMedia Defaults to "Water". Refer to WQP domain tables for other options. @@ -506,7 +529,7 @@ JoinWQPProfiles <- function(FullPhysChem = "null", if(nrow(Sites.df)>0){ join1 <- FullPhysChem.df %>% # join stations to results - dplyr::left_join(Sites.df, by = "MonitoringLocationIdentifier") %>% + dplyr::left_join(Sites.df, by = "MonitoringLocationIdentifier", multiple = "all") %>% # remove ".x" suffix from column names dplyr::rename_at(dplyr::vars(dplyr::ends_with(".x")), ~ stringr::str_replace(., "\\..$", "")) %>% # remove columns with ".y" suffix @@ -545,8 +568,8 @@ JoinWQPProfiles <- function(FullPhysChem = "null", ), by = c( "OrganizationIdentifier", "OrganizationFormalName", - "ProjectIdentifier","ProjectName" - ) + "ProjectIdentifier","ProjectName"), + multiple = "all" ) }else{join3 = join2} diff --git a/R/ResultFlagsIndependent.R b/R/ResultFlagsIndependent.R index 859342fe6..8f6bafb93 100644 --- a/R/ResultFlagsIndependent.R +++ b/R/ResultFlagsIndependent.R @@ -983,6 +983,7 @@ QAPPDocAvailable <- function(.data, clean = FALSE) { #' #' # Remove data with imprecise coordinates or coordinates outside the USA from the dataframe: #' InvalidCoord_removed <- InvalidCoordinates(Nutrients_Utah, clean_outsideUSA = "remove", clean_imprecise = TRUE) +#' InvalidCoordinates <- function(.data, clean_outsideUSA = c("no", "remove", "change sign"), @@ -996,7 +997,7 @@ InvalidCoordinates <- function(.data, checkType(clean_imprecise, "logical") # check .data has required columns checkColumns(.data, c("LatitudeMeasure", "LongitudeMeasure")) - #check lat and long are "numeric" + # check lat and long are "numeric" if (class(.data$LongitudeMeasure) != "numeric") { warning("LongitudeMeasure field must be numeric") } @@ -1017,7 +1018,7 @@ InvalidCoordinates <- function(.data, LongitudeMeasure > 0 & LongitudeMeasure < 145 ~ "LONG_OutsideUSA", grepl("999", LatitudeMeasure) ~ "Imprecise_Latincludes999", grepl("999", LongitudeMeasure) ~ "Imprecise_Longincludes999", - #for below, lat and long fields must be numeric + # for below, lat and long fields must be numeric # this checks if there are at least 3 significant figures to the # right of the decimal point sapply(.data$LatitudeMeasure, decimalplaces) < 3 @@ -1043,7 +1044,7 @@ InvalidCoordinates <- function(.data, # if clean_outsideUSA is "change sign", change the sign of lat/long coordinates outside of USA if (clean_outsideUSA == "change sign") { - print("Note: This is a temporary solution. Data owner should fix the raw data to address invalid coordinates through WQX. For assistance, email the WQX helpdesk (WQX@epa.gov).") + print("When clean_outsideUSA == change sign, the sign for any lat/long coordinates flagged as outside of USA are switched. This is a temporary solution. Data owners should fix the raw data to address invalid coordinates through WQX. For assistance fixing data errors you see in the WQP, email the WQX helpdesk (WQX@epa.gov).") .data <- .data %>% dplyr::mutate( LatitudeMeasure = dplyr::case_when( @@ -1055,7 +1056,7 @@ InvalidCoordinates <- function(.data, ) } - #return only flagged data if errorsonly = true + # return only flagged data if errorsonly = true if ((errorsonly == TRUE)) { .data <- dplyr::filter(.data, is.na(TADA.InvalidCoordinates) != TRUE) } diff --git a/R/Visualizations.R b/R/Visualizations.R index 69ab88b8a..e06293cfe 100644 --- a/R/Visualizations.R +++ b/R/Visualizations.R @@ -1,71 +1,3 @@ -#' @title Generate Animated Map -#' -#' @description -#' Animated map code adapted from USGS blog: https://waterdata.usgs.gov/blog/large_sample_pull/ -#' -#' @param .data TADA dataframe -#' -#' @return Animated map -#' -#' @export -#' - -CreateAnimatedMap <- function(.data) { - - # code adapted from USGS blog: https://waterdata.usgs.gov/blog/large_sample_pull/ - - # pull the year from the date - .data$year <- base::format(as.Date(.data$ActivityStartDate, format="%Y-%m-%d"),"%Y") - - # create new data frame of summarized result data by year - n_bysite <- - .data %>% - dplyr::group_by(MonitoringLocationIdentifier, LatitudeMeasure, LongitudeMeasure, year) %>% - dplyr::summarize(mean = mean(.data$ResultMeasureValue, na.rm = TRUE), - median = stats::median(.data$ResultMeasureValue, na.rm = TRUE)) - - # create a new character column with total nitrogen acceptable range designations - n_bysite <- - n_bysite %>% dplyr::mutate(TN_mean= - dplyr::case_when(mean<2 ~ "<2 mg/l", - mean>=2 & mean<=6 ~ "2-6 mg/l", - mean>6 ~ ">6 mg/l")) - - # convert latitude, longitude, and year data to numeric form - n_bysite$LatitudeMeasure <- as.numeric(n_bysite$LatitudeMeasure) - n_bysite$LongitudeMeasure <- as.numeric(n_bysite$LongitudeMeasure) - n_bysite$year <- as.numeric(n_bysite$year) - - # first, create the base map data frame - all_state <- "usa" - - usa <- map_data("state", interior=TRUE) - base_map <- ggplot2::ggplot(data = usa, mapping = aes(x = long, - y = lat, - group = group)) + - ggplot2::geom_polygon(color = "black", fill = "white") + - ggplot2::coord_quickmap() + - ggplot2::theme_void() - - # second, plot the base map and add data to it - map_with_data <- base_map + - ggplot2::geom_point(data = n_bysite, aes(x = LongitudeMeasure, - y = LatitudeMeasure, - color = TN_mean, - group = year, - frame = year)) + - gganimate::transition_time(year) + - ggplot2::ggtitle('Year: {frame_time}', # add year to the title - subtitle = 'Frame {frame} of {nframes}') + - ggplot2::scale_colour_manual(values = c("blue", "red", "green")) - num_years <- max(n_bysite$year)-min(n_bysite$year) + 1 - - # lastly, run the animation - gganimate::animate(map_with_data, nframes = num_years, fps = 1) - - } - - #' @title Generate Map #' #' @description diff --git a/docs/404.html b/docs/404.html index 95b5619a1..68f75ade6 100644 --- a/docs/404.html +++ b/docs/404.html @@ -75,7 +75,7 @@