---
title: "Data Profiling Report"
output:
  html_document: 
    theme: cerulean
    toc: yes
    toc_depth: 6
params:
  data: data
  fun_options: fun_options
---

```{r global_options, include = FALSE}
library(rmarkdown)

data <- params$data
num_discrete <- SplitColType(params$data)$num_discrete
num_continuous <- SplitColType(params$data)$num_continuous
num_all_missing <- SplitColType(params$data)$num_all_missing

continuous_error <- function(e) {
  if (num_continuous == 0) {
    cat("No continuous features found.")
  } else {
    cat("Continuous features contain too many missing values.\n")
    cat("Try using PlotMissing() to determine which features to drop.")
  }
}

discrete_error <- function(e) {
  if (num_discrete == 0) {
    cat("No discrete features found.")
  } else {
    cat("Discrete features contain too many categories.\n")
    cat("Try using CollapseCategory() to reduce categories.")
  }
}

opts <- params$fun_options
knitr::opts_chunk$set(fig.width = 14, fig.height = 10, echo = FALSE)
```

<script src="d3.min.js"></script>

### Basic Statistics
The data is **`r format(object.size(data), units = "auto")`** in size. There are **`r format(nrow(data), big.mark = ",")`** rows and **`r format(ncol(data), big.mark = ",")`** columns (features). Of all **`r format(ncol(data), big.mark = ",")`** columns, **`r I(num_discrete)`** are discrete, **`r I(num_continuous)`** are continuous, and **`r I(num_all_missing)`** are all missing. There are **`r format(sum(is.na(data)), big.mark = ",")`** missing values out of **`r format(nrow(data)*ncol(data), big.mark = ",")`** data points.

#### Data Structure (Text)

```{r data_structure}
str(data)
```

#### Data Structure (Network Graph)

```{r plot_data_structure}
diagonalNetwork(
  PlotStr(data, print_network = FALSE),
  fontSize = 12,
  width = 1000,
  margin = list(
    "left" = 350,
    "right" = 250
  )
)
```

### Missing Values
The following graph shows the distribution of missing values.
```{r plot_missing}
PlotMissing(data)
```

### Data Distribution

#### Continuous Features

###### Histogram
```{r histogram_continuous}
tryCatch(HistogramContinuous(data), error = continuous_error)
```

#### Discrete Features
###### Bar Charts
```{r bar_discrete}
tryCatch(BarDiscrete(data), error = discrete_error)
```

### Correlation Analysis

#### Continuous Features
```{r correlation_continuous}
tryCatch(CorrelationContinuous(data, use = "na.or.complete"), error = continuous_error)
```

#### Discrete Features
```{r correlation_discrete}
tryCatch(CorrelationDiscrete(data, use = "pairwise.complete.obs"), error = discrete_error)
```
