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

---
title: "FRE Flow Gage"
author: "Mercury Program and Basin Planning Unit"
date: '2022-07-18'
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(shiny)

# Had issue with runtime:shiny resetting the WD of rproj so used getActiveProject()
wd <- rstudioapi::getActiveProject()

source(paste0(wd, "/R Functions/functions_estimate NDDNQ values.R"))
source(paste0(wd, "/R Functions/functions_QA data.R"))
```

# Load and Tidy Data
## Flow Data

###Define columns variable for repeat use later
```{r}
# Columns to select
columns_select <- c("SampleDate", "VALUE", "Day", "Month", "Year")
```

###Load data
```{r}
fre_data <- readxl::read_xlsx(paste0(wd, "/Reeval_Source_Analysis/Source Data/01_Tributaries/Flow/FRE.xlsx"), sheet = "FRE") %>% 
  chara_to_NumDate
```

###Tidy data 

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

###Find Daily Flow Median and Mean Concentrations

```{r}
fre_dailymedian <- fre_clean_format %>%  
  group_by(SampleDate) %>% 
  mutate(DailyMedian = median(VALUE, na.rm=T), DailyMean = mean(VALUE, na.rm=T)) %>% 
  distinct(SampleDate, DailyMedian, .keep_all = TRUE) %>% 
  select(SampleDate, DailyMedian, DailyMean, Month, Year)
```

###Find Monthly Flow Median and Mean

```{r}
fre_median <- fre_dailymedian %>%  
  group_by(Month, Year) %>% 
  summarise(FREMedian = median(DailyMedian, na.rm=T),
            FREMean = mean(DailyMean, na.rm=T),
            N = n(),
            FREMedianMax = max(DailyMedian, na.rm=T),
            FREMedianMin = min(DailyMedian, na.rm=T))
```

### Group By Year and Month
```{r}
# added this code to recreate a Pivot Table created in excel after saving output
fre_byMonth <- fre_median %>%
  select(Year, Month, FREMean) %>%
  mutate(Month = month(Month, label=T, abbr=T)) %>% # convert numeric months to 3 letter abbreviation
  pivot_wider(names_from=Month, values_from=FREMean) %>%
  arrange(Year) %>%
  mutate(Year = as.character(Year)) %>% 
  mutate(`Grand Total` = rowSums(across(Jan:Dec), na.rm = TRUE)) %>% 
  add_row(Year = "Grand Total", summarize(., across(Jan:`Grand Total`, ~sum(.x, na.rm=T))))
```

# Export to excel
```{r}
writexl::write_xlsx(list("FRE"=fre_median, "Totals Across Month and Year"=fre_byMonth), paste0(wd, '/Reeval_Source_Analysis/Source Data/01_Tributaries/Flow/Output/01_Tributaries FRE Flow', 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     
## 
## loaded via a namespace (and not attached):
##  [1] rstudioapi_0.13    knitr_1.39         magrittr_2.0.3     tidyselect_1.1.2  
##  [5] R6_2.5.1           rlang_1.0.2        fastmap_1.1.0      fansi_1.0.3       
##  [9] stringr_1.4.0      highr_0.9          dplyr_1.0.9        tools_4.2.2       
## [13] xfun_0.31          utf8_1.2.2         DBI_1.1.2          cli_3.3.0         
## [17] htmltools_0.5.2    ellipsis_0.3.2     assertthat_0.2.1   yaml_2.3.5        
## [21] readxl_1.4.0       digest_0.6.29      tibble_3.1.7       lifecycle_1.0.1   
## [25] crayon_1.5.1       RColorBrewer_1.1-3 purrr_0.3.4        vctrs_0.4.1       
## [29] glue_1.6.2         evaluate_0.15      rmarkdown_2.14     stringi_1.7.6     
## [33] compiler_4.2.2     pillar_1.7.0       cellranger_1.1.0   generics_0.1.2    
## [37] writexl_1.4.0      pkgconfig_2.0.3
    Sys.time()
## [1] "2023-12-26 16:26:39 PST"