This report is automatically generated with the R package knitr (version 1.39) .

---
title: '03_Wastewater Treatment Plants'
subtitle: 'Data Fixes Associated with Control Study'
author: "Mercury Program and Basin Planning Unit"
date: "3/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(lubridate)
library(readxl)
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")


# Read MeHg Data set
esmr <- read_excel("Reeval_Source_Analysis/Source Data/03a_Municipal WWTPs (NPDES)/Data/03_NPDES_eSMR MeHg Data.xlsx", guess_max = 300000, sheet = "OIMA eSMR FixErrors-AddLathrop")
```


## Summarize Suspected Unit Error Fixes
```{r}
StationErrorSummary <- esmr %>% 
  filter(!is.na(ErrorCheck)) %>%
  group_by(StationName, ErrorCheck) %>% 
  summarize(CountedErrors = n(),
            YearRange = ifelse(year(min(SampleDate)) == year(max(SampleDate)), paste0(year(min(SampleDate))), paste(year(min(SampleDate)), year(max(SampleDate)), sep="-")),
            Dates = grammaticList(SampleDate))

StationErrorSummary %>% 
  kbl(align='lccc', caption='Fixed Errors by StationName') %>% 
  kable_paper('hover', full_width=T) %>% 
  kable_styling(fixed_thead = T)

# setwd(wd)
# writexl::write_xlsx(StationErrorSummary, paste0('Source Data/03a_Municipal WWTPs (NPDES)/Data/03_NPDES Unit Error Summary by SatationName_', today(), '.xlsx'))



esmr %>% 
  filter(`LOCATION PLACE TYPE` == 'Effluent Monitoring') %>% 
  mutate(Result = if_else(Unit == 'ug/L', Result*1000, Result),
         Result = if_else(Unit == 'mg/L', Result*1000000, Result),
         MDL = if_else(Unit == 'ug/L', MDL*1000, MDL),
         MDL = if_else(Unit == 'mg/L', MDL*1000000, MDL),
         RL = if_else(Unit == 'ug/L', RL*1000, RL),
         RL = if_else(Unit == 'mg/L', RL*1000000, RL),
         origResult = if_else(origUnit == 'ug/L', origResult*1000, origResult),
         origResult = if_else(origUnit == 'mg/L', origResult*1000000, origResult),
         origMDL = if_else(origUnit == 'ug/L', origMDL*1000, origMDL),
         origMDL = if_else(origUnit == 'mg/L', origMDL*1000000, origMDL),
         origRL = if_else(origUnit == 'ug/L', origRL*1000, origRL),
         origRL = if_else(origUnit == 'mg/L', origRL*1000000, origRL)
         ) %>%
  group_by(StationName) %>% 
  summarize(n = n(),
            ResultAvg_ngL = mean(Result, na.rm=T),
            OrigResultAvg_ngL = mean(origResult, na.rm=T),
            MDLAvg_ngL = mean(MDL, na.rm=T),
            OrigMDLAvg_ngL = mean(origMDL, na.rm=T),
            RLAvg_ngL = mean(RL, na.rm=T),
            OrigRLAvg_ngL = mean(origRL, na.rm=T),
            ResultNAs = sum(is.na(cur_data()$Result)),
            OrigResultNAs = sum(is.na(cur_data()$origResult))) %>% 
  kbl(align='lccc', caption='NPDES MeHg Data Summary') %>% 
  kable_paper('hover', full_width=T) %>% 
  kable_styling(fixed_thead = T)
```

## Show Suspect Data Based on Filter
```{r}
esmr %>% 
  filter(`LOCATION PLACE TYPE` == 'Effluent Monitoring') %>%
  filter(Unit != origUnit | ((Unit != 'ng/L' | origUnit != 'ng/L') & ((Result > 0.01 & Result < 5) | (origResult > 0.01 & origResult < 5)))) %>%
  mutate(Result = if_else(Unit == 'ug/L', Result*1000, Result),
         Result = if_else(Unit == 'mg/L', Result*1000000, Result),
         MDL = if_else(Unit == 'ug/L', MDL*1000, MDL),
         MDL = if_else(Unit == 'mg/L', MDL*1000000, MDL),
         RL = if_else(Unit == 'ug/L', RL*1000, RL),
         RL = if_else(Unit == 'mg/L', RL*1000000, RL),
         Unit = 'ng/L',
         origResult = if_else(origUnit == 'ug/L', origResult*1000, origResult),
         origResult = if_else(origUnit == 'mg/L', origResult*1000000, origResult),
         origMDL = if_else(origUnit == 'ug/L', origMDL*1000, origMDL),
         origMDL = if_else(origUnit == 'mg/L', origMDL*1000000, origMDL),
         origRL = if_else(origUnit == 'ug/L', origRL*1000, origRL),
         origRL = if_else(origUnit == 'mg/L', origRL*1000000, origRL),
         origUnit = 'ng/L'
         ) %>% 
  group_by(StationName) %>% 
  select(StationName, SampleDate, Unit, Result, MDL, RL, origUnit, origResult, origMDL, origRL) %>%  
  kbl(align='lccc', caption='NPDES MeHg Data Summary') %>% 
  kable_paper('hover', full_width=T) %>% 
  kable_styling(fixed_thead = T)
```
## Error: <text>:20:1: unexpected '<'
## 19: 
## 20: <
##     ^

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:09:00 PST"