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

---
title: "05_MS4 Ph2 MeHg Median Conc"
author: "Mercury Program and Basin Planning Unit"
date: '2022-06-15'
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: 2010 Urban Waterways Data

###Define columns variable for repeat use later
```{r}
# Columns to select
columns_select <- c("SourceID", "SourceRow", "StationName", "Watershed", "WatershedSampleNumber", "SiteNumber", "SampleDate", "SampleTime", "Month", "SeasonCollected", "EventType",  "Result", "Unit", "WeatherType")
```

###Load data
```{r}
urban2010_data <- readxl::read_xlsx(paste0(wd, "/Reeval_Source_Analysis/Source Data/05_Urban Runoff/2010_Urban_Waterways_MeHg_Data.xlsx"), sheet = "2010_Data_Clean") %>%
  chara_to_NumDate
```

###Tidy data 

```{r}
urban2010_clean_format <- urban2010_data %>%
  #Clean up dates
  mutate(SampleDate = as.Date(SampleDate, origin = "1899-12-30"))
```

###Find Site-Specific MeHg Median and Mean Concentrations by Weather Type

```{r}
urban2010_sitemedian <- urban2010_clean_format %>%  
  group_by(StationName, WeatherType) %>% 
  mutate(SiteMedian = median(Result, na.rm=T), SiteMean = mean(Result, na.rm=T)) %>% 
  distinct(StationName, WeatherType, SiteMedian, .keep_all = TRUE) %>% 
  select(StationName, Watershed, WeatherType, SiteMedian, SiteMean, Unit)
```

###Find Seasonal MeHg Median and Mean Concentrations

```{r}
urban2010_weathermedian <- urban2010_sitemedian %>%  
  group_by(WeatherType) %>% 
  summarise(WeatherMedian = median(SiteMedian, na.rm=T),
            WeatherMean = mean(SiteMean, na.rm=T),
            N = n(),
            WeatherMedianMax = max(SiteMedian, na.rm=T),
            WeatherMedianMin = min(SiteMedian, na.rm=T))

urban2010_weathermedian %>% 
  kbl(align='lcc', caption="Urban MS4 Ph2 MeHg Median Concentration") %>%
  kable_paper('hover', full_width=T) %>% 
  kable_styling(fixed_thead = T)
```

### Export to excel
```{r}
 writexl::write_xlsx(urban2010_weathermedian, paste0(wd, "/Reeval_Source_Analysis/Source Data/05_Urban Runoff/05_Urban MS4 Ph2 MeHg Median Concentration", 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:29:38 PST"