This report is automatically generated with the R
package knitr
(version 1.39
)
.
--- title: "05_Urban Runoff" author: "Mercury Program and Basin Planning Unit" date: "6/1/2022" output: html_document: code_folding: show toc: TRUE toc_float: TRUE toc_depth: 3 runtime: shiny assets: css: - "http://fonts.googleapis.com/css?family=Raleway:300" - "http://fonts.googleapis.com/css?family=Oxygen" --- <style> body{ font-family: 'Oxygen', sans-serif; font-size: 16px; line-height: 24px; } h1,h2,h3,h4 { font-family: 'Raleway', sans-serif; } .container { width: 1250px; } h3 { background-color: #D4DAEC; text-indent: 50px; } h4 { text-indent: 75px; margin-top: 35px; margin-bottom: 5px; } </style> ```{r setup, include=FALSE} knitr::opts_chunk$set(echo=TRUE, warning=FALSE, message=FALSE, fig.width=9.5) ``` ```{r Libraries, echo=FALSE} library(kableExtra) # better formatting of tables library(shiny) # Had issue trying to set WD with Shiny in R project, reset working directory of rproj wd <- rstudioapi::getActiveProject() setwd(wd) source("R Functions/functions_estimate NDDNQ values.R") source("R Functions/functions_QA data.R") ``` # Load and Tidy Data ## MeHg: MS4 Phase 1 Data ###Define columns variable for repeat use later ```{r} # Columns to select columns_select <- c("SourceID", "StationName", "SampleDate", "SampleTime", "Result", "Unit", "ResultQualCode","MDL", "RL", "QACode", "Comments", "EventType", "SiteType", "WaterYear", "DaysInMonth", "Month", "Year", "TargetLatitude", "TargetLongitude", "SiteType") ``` ###Load data ```{r} ms4_data <- readxl::read_xlsx(paste0(wd, "/Reeval_Source_Analysis/Source Data/05_Urban Runoff/MS4_MeHg_Data_Clean.xlsx"), sheet = "Clean_All") %>% chara_to_NumDate ``` ###Tidy data ```{r} ms4_clean_format <- ms4_data %>% #Clean up dates mutate(SampleDate = as.Date(SampleDate, origin = "1899-12-30")) %>% #Clean up units standardizeUnits ``` ####Pull Port of Stockton Data from all data since blank results are in Port of Stockton data only, in order to run RobinNDDNQ script to assign blank values based on best fit models. ```{r} ms4_PoS <- ms4_clean_format %>% filter(SourceID %in% c("Port of Stockton MS4 Annual Report Data", "Port of Stockton Email")) ``` ####Create dataframe with all other MS4 sources besides Port of Stockton to join with estimated blank values for Port of Stockton later. ```{r} ms4_noPoS <- ms4_clean_format %>% filter(SourceID %are not% c("Port of Stockton MS4 Annual Report Data", "Port of Stockton Email")) ``` ####Run Port of Stockton through NDDNQ. ```{r} ms4_PoS_nddnq <- ms4_PoS %>% robinNDDNQ(., ResultQualCode) #.=short cut for df within dplyr first part of function is dataframe and then the column to look at separated by column) #plot(ms4_nddnq$weibull$fitmodel) ``` ####View Fitted Distribution Graphs (output recommend llogis) ```{r, echo=FALSE} ui <- fluidPage( selectInput('distName', label='Select Distribution', choices=names(ms4_PoS_nddnq), selected=1), plotOutput('distPlot') ) server <- function(input, output, session) { output$distPlot <- renderPlot({ plot(ms4_PoS_nddnq[[input$distName]]$fitmodel) # view plot fits title(input$distName, outer=T) }) } shinyApp(ui, server, options=list(height=500)) ``` ####Set Final ND/DNQ Estimation ```{r} ms4_PoS_final <- ms4_PoS_nddnq$llogis$fitted # Selected distribution - primarily based on CDF & P-P plot showing best fit to data ``` ####Combine processed PoS and other MS4 data together ```{r} ms4_all <- ms4_noPoS %>% bind_rows(ms4_PoS_final) ``` ###Create a column for MS4 Phase 1 Discharger ```{r} ecc <- c("CCCWP Email") pos <- c("Port of Stockton Email", "Port of Stockton MS4 Annual Report Data") sjc <- c("SJC-StocktonCity MS4 Annual Report Data", "SJC-StocktonCity Control Study Data", "SJC-StocktonCity Email") ssqp <- c("SSQP CEDEN", "SSQP Email") ms4_all_ph1 <- ms4_all %>% mutate(MS4Ph1Discharger = if_else(SourceID %in% ecc, "CCCWP", if_else(SourceID %in% pos, "Port of Stockton", if_else(SourceID %in% sjc, "SJC and City of Stockton", if_else(SourceID %in% ssqp, "SSQP", "Error", missing = NULL), missing =NULL), missing = NULL), missing = NULL)) ``` ###Find Daily MeHg Median and Mean Concentrations ```{r} ms4_dailymedian <- ms4_all_ph1 %>% group_by(MS4Ph1Discharger, SampleDate) %>% mutate(DailyMedian = median(Result, na.rm=T), DailyMean = mean(Result, na.rm=T)) %>% distinct(SampleDate, MS4Ph1Discharger, DailyMedian, .keep_all = TRUE) %>% select(MS4Ph1Discharger, SampleDate, WaterYear, DaysInMonth, Month, Year, AtmoType, DailyMedian, DailyMean) #Alternative code that spits out only columns group_by and new column name: #ms4_dailymedian <- ms4_all_ph1 %>% #group_by(MS4Ph1Discharger, SampleDate) %>% #summarise(DailyMedian = median(Result, na.rm=T), .groups = "drop") ``` ###Find Seasonal MeHg Median and Mean Concentrations ```{r} ms4_seasonalmedian <- ms4_dailymedian %>% group_by(MS4Ph1Discharger, AtmoType) %>% summarise(SeasonalMedian = median(DailyMedian, na.rm=T), SeasonalMean = mean(DailyMean, na.rm=T), N = n(), DailyMedianMax = max(DailyMedian, na.rm=T), DailyMedianMin = min(DailyMedian, na.rm=T)) # ms4_seasonalmedian <- ms4_dailymedian %>% # group_by(MS4Ph1Discharger, AtmoType) %>% # mutate(SeasonalMedian = median(DailyMedian, na.rm=T), SeasonalMean = mean(DailyMean, na.rm=T)) %>% # distinct(AtmoType, MS4Ph1Discharger, SeasonalMedian, .keep_all = TRUE) %>% # select(MS4Ph1Discharger, AtmoType, SeasonalMedian, SeasonalMean) ms4_seasonalmedian %>% kbl(align='lcc', caption="Urban Runoff MS4 Phase 1 Data Clean") %>% kable_paper('hover', full_width=T) %>% kable_styling(fixed_thead = T) ``` ### Export to excel ```{r} writexl::write_xlsx(ms4_seasonalmedian, paste0(wd, "/Reeval_Source_Analysis/Source Data/05_Urban Runoff/05_Urban Runoff MS4 Phase 1 Data Clean", today(), ".xlsx")) ```
## Error: <text>:18:1: unexpected '<' ## 17: ## 18: < ## ^
The R session information (including the OS info, R version and all packages used):
sessionInfo()
## R version 4.2.2 (2022-10-31 ucrt) ## Platform: x86_64-w64-mingw32/x64 (64-bit) ## Running under: Windows 10 x64 (build 22621) ## ## Matrix products: default ## ## locale: ## [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 ## [3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C ## [5] LC_TIME=English_United States.utf8 ## ## attached base packages: ## [1] stats graphics grDevices utils datasets methods base ## ## other attached packages: ## [1] lubridate_1.8.0 plotly_4.10.0 readxl_1.4.0 actuar_3.2-2 ## [5] NADA_1.6-1.1 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.9 ## [9] purrr_0.3.4 readr_2.1.2 tidyr_1.2.0 tibble_3.1.7 ## [13] ggplot2_3.3.6 tidyverse_1.3.1 fitdistrplus_1.1-8 survival_3.4-0 ## [17] MASS_7.3-58.1 ## ## loaded via a namespace (and not attached): ## [1] lattice_0.20-45 assertthat_0.2.1 digest_0.6.29 utf8_1.2.2 ## [5] R6_2.5.1 cellranger_1.1.0 backports_1.4.1 reprex_2.0.1 ## [9] evaluate_0.15 httr_1.4.3 highr_0.9 pillar_1.7.0 ## [13] rlang_1.0.2 lazyeval_0.2.2 rstudioapi_0.13 data.table_1.14.2 ## [17] Matrix_1.5-1 rmarkdown_2.14 labeling_0.4.2 splines_4.2.2 ## [21] htmlwidgets_1.5.4 munsell_0.5.0 broom_0.8.0 compiler_4.2.2 ## [25] modelr_0.1.8 xfun_0.31 pkgconfig_2.0.3 htmltools_0.5.2 ## [29] tidyselect_1.1.2 viridisLite_0.4.0 fansi_1.0.3 crayon_1.5.1 ## [33] tzdb_0.3.0 dbplyr_2.2.0 withr_2.5.0 grid_4.2.2 ## [37] jsonlite_1.8.0 gtable_0.3.0 lifecycle_1.0.1 DBI_1.1.2 ## [41] magrittr_2.0.3 scales_1.2.0 writexl_1.4.0 cli_3.3.0 ## [45] stringi_1.7.6 farver_2.1.0 fs_1.5.2 xml2_1.3.3 ## [49] ellipsis_0.3.2 generics_0.1.2 vctrs_0.4.1 expint_0.1-7 ## [53] RColorBrewer_1.1-3 tools_4.2.2 glue_1.6.2 hms_1.1.1 ## [57] yaml_2.3.5 fastmap_1.1.0 colorspace_2.0-3 rvest_1.0.2 ## [61] knitr_1.39 haven_2.5.0
Sys.time()
## [1] "2023-12-27 10:31:17 PST"