---
title: "BCGcalc"
subtitle: "Results Fuzzy Thermal Summary"
author: "`r Sys.getenv('USERNAME')`"
date: "`r format(Sys.time(), '%Y-%m-%d %H:%M:%S')`"
output:
  html_document:
    toc: yes
    toc_depth: 3
    toc_float: no
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(results = 'asis', echo = FALSE, warning = FALSE)
```

```{r}
cat("**Filename:** ", inFile$name, "\n\n", sep = "")
```

# Plot

```{r plot}
# use results before trimmed L1:6
df_plot <- df_lev_flags 
alpha_rect <- 0.25
plot_mean <- mean(df_plot$Primary_Therm)

p1 <- ggplot2::ggplot(df_plot, ggplot2::aes(x = Primary_Therm)) + 
  ggplot2::geom_histogram(bins = 50) + 
  ggplot2::annotate("rect"
                    , xmin = 5.5
                    , xmax = 6.25
                    , ymin = -Inf
                    , ymax = Inf
                    , alpha = alpha_rect
                    , fill = "red") + 
  ggplot2::annotate("rect"
                    , xmin = 4.5
                    , xmax = 5.5
                    , ymin = -Inf
                    , ymax = Inf
                    , alpha = alpha_rect
                    , fill = "dark orange") + 
  ggplot2::annotate("rect"
                    , xmin = 3.5
                    , xmax = 4.5
                    , ymin = -Inf
                    , ymax = Inf
                    , alpha = alpha_rect
                    , fill = "yellow") + 
  ggplot2::annotate("rect"
                    , xmin = 2.5
                    , xmax = 3.5
                    , ymin = -Inf
                    , ymax = Inf
                    , alpha = alpha_rect
                    , fill = "gray") + 
  ggplot2::annotate("rect"
                    , xmin = 1.5
                    , xmax = 2.5
                    , ymin = -Inf
                    , ymax = Inf
                    , alpha = alpha_rect
                    , fill = "green") + 
  ggplot2::annotate("rect"
                    , xmin = 1.25
                    , xmax = 1.5
                    , ymin = -Inf
                    , ymax = Inf
                    , alpha = alpha_rect
                    , fill = "blue") +
  ggplot2::geom_histogram(bins = 50) + 
  ggplot2::geom_vline( ggplot2::aes(xintercept = plot_mean)
                       , color = "black"
                       , linetype = "dashed") +
  ggplot2::labs(caption = paste0("mean (black dashed line) = "
                                 , round(plot_mean, 2))
       , x = "Primary Thermal") + 
  ggplot2::theme_light()

# plotly::ggplotly(p1)
p1
```

# Flags

```{r flags}
cap_flags <- "Flag summary."
knitr::kable(table(df_flags[, "CHECKNAME"]
                   , df_flags[, "FLAG"]
                   , useNA = "ifany")
             , caption = cap_flags)
```

```{r upset}
# Data
df_plot2 <- df_flags_wide
# Munge
## SampleID to row names
row.names(df_plot2) <- df_plot2[, "SAMPLEID"]
## Remove columns
df_plot2 <- df_plot2[, -c(1:2)]
## Change terminology; NA/flag to 0/1
df_plot2[!is.na(df_plot2)] <- 1
df_plot2[is.na(df_plot2)] <- 0
## Convert character to numeric
col2fix <- 1:ncol(df_plot2)
df_plot2[, col2fix] <- apply(df_plot2[, col2fix]
                  , 2
                  , function(x) as.numeric(as.character(x)))

# plot
p2 <- ComplexUpset::upset(df_plot2
                          , intersect = names(df_plot2)
                          , name = "Flags")
p2
```

# Results

```{r results}
cap_data <- "Summary results."
DT::datatable(df_lev_flags 
              , filter = "top"
              , caption = cap_data
              , options = list(scrollX = TRUE
                               , lengthMenu = c(5, 10, 25, 50, 100, 1000)
                               , autoWidth = TRUE
              ))
```
