## Load library library(data.table) library(rmarkdown) library(knitr) ## Get user data data <- params$data report_config <- params$report_config response <- params$response ## Create helper functions do_call <- function(fun_name, na_omit = FALSE) { if (na_omit) data <- na.omit(data) do.call(fun_name, c(list("data" = data), report_config[[fun_name]])) } na.convert <- function(x,y = 0) { x[is.na(x)] <- y x } na.convert2 = function(x) na.convert(x,mean(x,na.rm = T)) ## Set knitr options opts_chunk$set(fig.width = 14, fig.height = 10, echo = FALSE, results = "asis")
if ("introduce" %in% names(report_config)) { intro <- do_call("introduce") memory_usage <- intro[["memory_usage"]] class(memory_usage) <- "object_size" intro_df <- data.frame( "Name" = c("Rows", "Columns", "Discrete columns", "Continuous columns", "All missing columns", "Missing observations", "Total observations", "Memory allocation"), "Value" = c( format(intro[["rows"]], big.mark = ","), format(intro[["columns"]], big.mark = ","), format(intro[["discrete_columns"]], big.mark = ","), format(intro[["continuous_columns"]], big.mark = ","), format(intro[["all_missing_columns"]], big.mark = ","), format(intro[["total_missing_values"]], big.mark = ","), format(intro[["total_observations"]], big.mark = ","), format(memory_usage, unit = "auto") ) ) ## Render content cat("### Basic Statistics", fill = TRUE) kable(intro_df) }
if ("plot_str" %in% names(report_config)) { str_object <- do.call(plot_str, c(list("data" = data, "max_level" = report_config[["plot_str"]][["max_level"]], "print_network" = FALSE))) plot_option <- report_config[["plot_str"]][setdiff(names(report_config[["plot_str"]]), c("type", "max_level", "print_network"))] ## Render content cat("### Data Structure", fill = TRUE) if (report_config[["plot_str"]][["type"]] == "radial") { do.call(radialNetwork, c(list("List" = str_object), plot_option)) } else { do.call(diagonalNetwork, c(list("List" = str_object), plot_option)) } }
if ("plot_missing" %in% names(report_config)) { cat("### Missing Data Profile", fill = TRUE) do_call("plot_missing") }
if (any(c("plot_bar", "plot_histogram", "plot_density") %in% names(report_config))) { cat("### Univariate Distribution", fill = TRUE) }
if ("plot_histogram" %in% names(report_config)) { if (intro[["continuous_columns"]] > 0) { cat("#### Histogram", fill = TRUE) do_call("plot_histogram") } }
if ("plot_density" %in% names(report_config)) { if (intro[["continuous_columns"]] > 0) { cat("#### Density Estimates", fill = TRUE) do_call("plot_density") } }
if ("plot_bar" %in% names(report_config)) { if (intro[["discrete_columns"]] > 0) { bar_option <- report_config[["plot_bar"]][setdiff(names(report_config[["plot_bar"]]), "with")] cat("#### Bar Chart (by frequency)", fill = TRUE) do.call(plot_bar, c(list("data" = data), bar_option)) } }
if ("plot_bar" %in% names(report_config)) { if (intro[["discrete_columns"]] > 0) { if (!is.null(response)) { if (is.numeric(data[[response]])) { cat(paste0("#### Bar Chart (by ", response, ")"), fill = TRUE) do.call(plot_bar, c(list("data" = data, "with" = response), bar_option)) } } } }
if ("plot_bar" %in% names(report_config)) { if (intro[["discrete_columns"]] > 0) { with_value <- report_config[["plot_bar"]][["with"]] if (!is.null(with_value)) { if (response != with_value) { cat(paste0("#### Bar Chart (by ", with_value, ")"), fill = TRUE) do_call("plot_bar") } } } }
if ("plot_correlation" %in% names(report_config)) { cat("### Correlation Analysis", fill = TRUE) do_call("plot_correlation", na_omit = FALSE) }
if ("plot_prcomp" %in% names(report_config)) { cat("### Principle Component Analysis", fill = TRUE) if (!is.null(response)) { #dt <- na.omit(data[, -response, with = FALSE]) dt = data[,lapply(.SD[, -response, with = FALSE],na.convert2)] do.call(plot_prcomp, c(list("data" = dt), report_config[["plot_prcomp"]])) } else { dt = data[,lapply(.SD,na.convert2)] do.call(plot_prcomp, c(list("data" = dt), report_config[["plot_prcomp"]])) } }
if (any(c("plot_boxplot", "plot_scatterplot") %in% names(report_config)) & !is.null(response)) { cat("### Bivariate Distribution", fill = TRUE) }
if ("plot_boxplot" %in% names(report_config)) { boxplot_option <- report_config[["plot_boxplot"]][setdiff(names(report_config[["plot_boxplot"]]), "by")] if (!is.null(response)) { cat(paste0("#### Boxplot (by ", response, ")"), fill = TRUE) do.call(plot_boxplot, c(list("data" = data, "by" = response), boxplot_option)) } }
if ("plot_boxplot" %in% names(report_config)) { by_value <- report_config[["plot_boxplot"]][["by"]] if (!is.null(by_value)) { if (response != by_value) { cat(paste0("#### Boxplot (by ", by_value, ")"), fill = TRUE) do_call("plot_boxplot") } } }
if ("plot_scatterplot" %in% names(report_config)) { scatterplot_option <- report_config[["plot_scatterplot"]][setdiff(names(report_config[["plot_scatterplot"]]), "by")] if (!is.null(response)) { cat(paste0("#### Scatterplot (by ", response, ")"), fill = TRUE) do.call(plot_scatterplot, c(list("data" = data, "by" = response), scatterplot_option)) } }
if ("plot_scatterplot" %in% names(report_config)) { by_value <- report_config[["plot_scatterplot"]][["by"]] if (!is.null(by_value)) { if (response != by_value) { cat(paste0("#### Scatterplot (by ", by_value, ")"), fill = TRUE) do_call("plot_scatterplot") } } }
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.