## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
    collapse = FALSE,
    comment = NULL
)

options(cli.unicode = FALSE)
options(crayon.enabled = TRUE)

ansi_aware_handler = function(x, options) {
    paste0(
        "<pre class=\"r-output\"><code>",
        fansi::sgr_to_html(x = x, warn = FALSE, term.cap = "256"),
        "</code></pre>"
    )
}

knitr::knit_hooks$set(
    output = ansi_aware_handler, 
    message = ansi_aware_handler, 
    warning = ansi_aware_handler,
    error = ansi_aware_handler
)

knitr::opts_chunk$set(
    collapse = TRUE,
    # comment = "#>",
    comment = NA, 
    # fig.path = "man/figures/README-",
    out.width = "100%"
)

library(tabstats)

## ----eval = FALSE-------------------------------------------------------------
# install.packages("tabstats")

## ----eval = FALSE-------------------------------------------------------------
# # install.packages("pak")
# pak::pak("joshuamarie/tabstats")
# ## devtools::install_github("joshuamarie/tabstats")

## -----------------------------------------------------------------------------
head(mtcars[, 1:5], 5)

## -----------------------------------------------------------------------------
table_default(head(mtcars[, 1:5], 5))

## -----------------------------------------------------------------------------
df = data.frame(
    Statistic = c("N", "Mean", "SD", "Min", "Max"),
    Value = c("100", "3.45", "1.20", "1.00", "6.00")
)

table_summary(
    df, 
    title = "Descriptive Statistics", 
    header = TRUE
)

## -----------------------------------------------------------------------------
corr_matrix(cor(mtcars[, 1:4]), method = "Pearson")

## -----------------------------------------------------------------------------
cor_mat = 
    iris |> 
    rstatix::cor_test(Sepal.Width, Sepal.Length, Petal.Length) |> 
    dplyr::mutate(
        var1,
        var2,
        cor = format(cor, digits = 2),
        statistic = format(statistic, digits = 2),
        conf_int = paste0(
            "[",
            format(conf.low, digits = 2), 
            ", ",
            format(conf.high, digits = 2),
            "]"
        ),
        
        .keep = "unused"
    ) 

cor_mat |> 
    with({
        corr_matrix(
            new_corr_data(
                var1 = var1,
                var2 = var2,
                corr = cor,
                statistic = statistic,
                pval = p,
                conf_int = conf_int
            ),
            title = "Pearson Correlation Matrix"
        )
    })

## -----------------------------------------------------------------------------
m = matrix(
    c(10, 20, 30, 40), 
    nrow = 2,
    dimnames = list(
        c("A", "B"), 
        c("X", "Y")
    )
)

cross_table(m, percentage = "all")

## -----------------------------------------------------------------------------
table_summary(
    df, 
    title = "Descriptive Statistics", 
    header = TRUE, 
    style = sm_style(
        left_col = "blue_bold",
        right_col = "green",
        title = "bold", 
        sep = ":  "
    )
)

## -----------------------------------------------------------------------------
table_summary(
    df, 
    title = "Descriptive Statistics", 
    header = TRUE, 
    style = sm_style(
        left_col = \(x) cli::col_red(cli::style_bold(x)),
        right_col = \(x) cli::col_cyan(x), 
        title = "bold", 
        sep = ":  "
    )
)

## -----------------------------------------------------------------------------
table_summary(
    df, 
    title = "Descriptive Statistics", 
    header = TRUE, 
    style = sm_style(
        left_col = function(x) {
            red = cli::make_ansi_style("#BD4444")
            red(cli::style_bold(x))
        },
        right_col = \(x) {
            bluish_color = cli::make_ansi_style("#67A2C5")
            bluish_color(x)
        }, 
        title = "bold", 
        sep = ":  "
    )
)

## -----------------------------------------------------------------------------
cor_mat |> 
    with({
        corr_matrix(
            new_corr_data(
                var1 = var1,
                var2 = var2,
                corr = cor,
                statistic = statistic,
                pval = p,
                conf_int = conf_int
            ),
            title = "Pearson Correlation Matrix",
            style = cm_style(
                pval = function(x) {
                    x_num = as.numeric(x)
                    if (is.na(x_num) || x_num > 0.05) {
                        cli::style_italic(x) 
                    } else if (x_num > 0.01) {
                        reddish = cli::make_ansi_style("#CE2626")
                        reddish(x)
                        # or just cli::col_red(x)
                    } else {
                        cli::style_bold("<0.001")
                    }
                }
            )
        )
    })

