---
title: "Summary file from rwty analysis"
output:
  html_document:
    toc: TRUE
    toc_float: TRUE
params:
    input: "nothing here"
    chain.names: "chains"
---

```{r echo = FALSE}
library(knitr)
library(plotly)
library(DT)
```

# Single-chain diagnostics

```{r single.chain, echo = FALSE, results = "asis", warning = FALSE, message = FALSE}
results <- params$input
chains <- params$chain.names

# This bit seems weird but what it's actually doing is
# dynamically generating headers for each chain.  The
# arrangement of spaces and newlines is touchy as hell
# so don't mess with it unless you've got a super good reason

template <- "
## %s


" # dont't forget the newline

for (i in chains) {
  cat(sprintf(template, i))
  thisplot <- results[[paste0(i, ".correlations")]]
  print(thisplot)
  cat("\n\n")
}

```

<br>
<br>

# Multi-chain diagnostics

## Likelihood
```{r message = FALSE, echo = FALSE}
ggplotly(results$LnL.trace$trace.plot)
```

<br>
<br>

```{r message = FALSE, echo = FALSE}
ggplotly(results$LnL.trace$density.plot)
```


<br>
<br>

## Topology

### Distance from reference tree
```{r message = FALSE, echo = FALSE}
ggplotly(results$topology.trace.plot$trace.plot)
```

<br>
<br>

```{r message = FALSE, echo = FALSE}
ggplotly(results$topology.trace.plot$density.plot)
```


<br>
<br>

### Autocorrelation
```{r message = FALSE, echo = FALSE}
ggplotly(results$autocorr.plot)
```

<br>
<br>

### Split frequencies
```{r message = FALSE, echo = FALSE}
ggplotly(results$splitfreqs.sliding.plot)
```

<br>
<br>

```{r message = FALSE, echo = FALSE}
ggplotly(results$acsf.sliding.plot)
```


<br>
<br>

```{r message = FALSE, echo = FALSE}
ggplotly(results$splitfreqs.cumulative.plot)
```

<br>
<br>

```{r message = FALSE, echo = FALSE}
ggplotly(results$acsf.cumulative.plot)
```

<br>
<br>


### Agreement between chains
```{r message = FALSE, echo = FALSE, warning = FALSE}
ggplotly(results$splitfreq.matrix)
```

<br>
<br>

```{r message = FALSE, echo = FALSE, warning = FALSE}
ggplotly(results$asdsf.plot)
```

<br>
<br>

```{r message = FALSE, echo = FALSE}
print(results$asdsf.tree)
```

<br>
<br>

### Treespace plots
```{r message = FALSE, echo = FALSE}
ggplotly(results$treespace.heatmap)
```

<br>
<br>

```{r message = FALSE, echo = FALSE}
ggplotly(results$treespace.points.plot)
```

<br>
<br>

## Parameters



```{r message = FALSE, echo = FALSE, results = "asis"}
# Here we're going to create a subheading for each parameter
# that has a trace plot associated with it

# Get names of all "trace" plots
traces <- names(results)[grep(names(results), pattern = "trace")]

# Take out just the ones that aren't already plotted elsewhere
traces <- traces[which(!traces %in% c("LnL.trace", "topology.trace.plot"))]

# As above, this bit is super touchy so don't mess with it
# unless you have a compelling reason and several hours to kill

template <- "
### %s


" # dont't forget the newline

for (i in traces) {
  shortname <- sub(i, pattern = ".trace$", replacement = "", perl = TRUE)
  cat(sprintf(template, shortname))
  trace.plot <- results[[i]]
  print(trace.plot$trace.plot)
  print(trace.plot$density.plot)
  cat("\n\n")
}

```

<br>
<br>


### ESS table
```{r echo = FALSE}

datatable(results$ess.table, extensions = 'Buttons',
            options = list(dom = 'Blfrtip',
                           buttons = c('copy', 'csv', 
                                       'excel', 'pdf', 'print'),
                           lengthMenu = list(c(10,25,50,-1),
                                             c(10,25,50,"All"))))
```

