Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tinytable as default output #706

Merged
merged 22 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Description: Create beautiful and customizable tables to summarize several
RTF, JPG, or PNG. Tables can easily be embedded in 'Rmarkdown' or 'knitr'
dynamic documents. Details can be found in Arel-Bundock (2022)
<doi:10.18637/jss.v103.i01>.
Version: 1.4.5
Version: 1.9.9.9999
Authors@R: c(person("Vincent", "Arel-Bundock",
email = "[email protected]",
role = c("aut", "cre"),
Expand Down Expand Up @@ -52,12 +52,11 @@ Imports:
data.table,
generics,
glue,
kableExtra (>= 1.3.4),
insight (>= 0.19.6),
parameters (>= 0.21.2),
performance (>= 0.10.5),
tables (>= 0.9.17),
tinytable (>= 0.0.4)
tinytable (>= 0.0.5)
Suggests:
AER,
altdoc,
Expand Down Expand Up @@ -88,6 +87,7 @@ Suggests:
huxtable,
IRdisplay,
ivreg,
kableExtra,
knitr,
lavaan,
lfe,
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ install: document ## devtools::install()
deploy: ## pkgdown::deploy_to_branch()
Rscript -e "pkgdown::deploy_to_branch()"

document: install ## devtools::document()
document: ## devtools::document()
Rscript -e "devtools::document()"

website: document ## render vignettes and website
Expand Down
15 changes: 15 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# News

## 1.9.9.9999

BREAKING CHANGE: The default output format is now `tinytable` instead of `kableExtra`. Learn more about `tinytable` here:

https://vincentarelbundock.github.io/tinytable/

To revert to the previous behavior persistently, users can call:

library(modelsummary)
config_modelsummary(factory_default = "kableExtra")

Other breaking changes:

* The `statistic_override` argument was replaced by `vcov` over 1 year ago, with appropriate deprecation warnings. It is now fully removed.

## 1.4.5

* `tinytable` supports histograms in `datasummary_skim()`
Expand Down
2 changes: 1 addition & 1 deletion R/config_modelsummary.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Persistent user settings for the `modelsummary` package
#'
#' @param factory_default Default output format: "kableExtra", "gt", "flextable", "huxtable", "DT", or "markdown"
#' @param factory_default Default output format: "tinytable", "kableExtra", "gt", "flextable", "huxtable", "DT", or "markdown"
#' @param factory_latex Name of package used to generate LaTeX output when `output="latex"`.
#' @param factory_markdown Name of package used to generate LaTeX output when `output="markdown"`.
#' @param factory_html Name of package used to generate LaTeX output when `output="html"`.
Expand Down
13 changes: 4 additions & 9 deletions R/datasummary.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#' `x~mean*z` will print the `mean`-related header above the `z`-related
#' header.`
#' @param ... all other arguments are passed through to the table-making
#' functions [kableExtra::kbl], [gt::gt], [DT::datatable], etc. depending on the `output` argument.
#' functions [tinytable::tt], [kableExtra::kbl], [gt::gt], [DT::datatable], etc. depending on the `output` argument.
#' This allows users to pass arguments directly to `datasummary` in order to
#' affect the behavior of other functions behind the scenes.
#' @template citation
Expand Down Expand Up @@ -124,6 +124,7 @@
#' #datasummary(f, data = mtcars, output = 'latex')
#'
#' # Return a table object to customize using a table-making package
#' #datasummary(f, data = mtcars, output = 'tinytable')
#' #datasummary(f, data = mtcars, output = 'gt')
#' #datasummary(f, data = mtcars, output = 'kableExtra')
#' #datasummary(f, data = mtcars, output = 'flextable')
Expand All @@ -144,7 +145,7 @@
#' found in the 'tables' package documentation: ?tables::tabular
#'
#' Hierarchical or "nested" column labels are only available for these output
#' formats: kableExtra, gt, html, rtf, and LaTeX. When saving tables to other
#' formats: tinytable, kableExtra, gt, html, rtf, and LaTeX. When saving tables to other
#' formats, nested labels will be combined to a "flat" header.
#' @export
datasummary <- function(formula,
Expand Down Expand Up @@ -222,13 +223,6 @@ datasummary <- function(formula,
}
}

## escape stub
sw <- attr(dse, "stub_width")
for (i in 1:sw) {
dse[, i] <- escape_string(dse[, i])
}
colnames(dse)[1:sw] <- escape_string(colnames(dse)[1:sw])

# build
out <- factory(dse,
align = align,
Expand All @@ -239,6 +233,7 @@ datasummary <- function(formula,
title = title,
add_columns = add_columns,
add_rows = add_rows,
escape = escape,
...)

# invisible return
Expand Down
8 changes: 0 additions & 8 deletions R/datasummary_balance.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,6 @@ datasummary_balance <- function(formula,
strrep("r", ncol(tab) - attr(tab, "stub_width")))
}

## escape stub: re-sanitize because we called `datasummary()` a few times
sanitize_output(output) # before sanitize_escape
sanitize_escape(escape) # after sanitize_output
for (i in 1:attr(tab, "stub_width")) {
tab[[i]] <- escape_string(tab[[i]])
}

## weights warning
if (isTRUE(any_factor) && "weights" %in% colnames(data)) {
msg <- 'When the `data` used in `datasummary_balance` contains a "weights" column, the means, standard deviations, difference in means, and standard errors of numeric variables are adjusted to account for weights. However, the counts and percentages for categorical variables are not adjusted.'
Expand Down Expand Up @@ -339,7 +332,6 @@ DinM <- function(lhs, rhs, data, fmt, statistic, stars = TRUE, escape = TRUE) {

if (identical(statistic, "p.value")) {
out[[statistic]] <- rounding(out[[statistic]], pval = TRUE)
out[[statistic]] <- escape_string(out[[statistic]]) # <0.001 interpreted as html tag
} else {
out[[statistic]] <- rounding(out[[statistic]])
}
Expand Down
3 changes: 0 additions & 3 deletions R/datasummary_correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,6 @@ datasummary_correlation <- function(data,
align <- paste0('l', strrep('r', ncols - 1))
}

out[, 1] <- escape_string(out[, 1])
colnames(out) <- escape_string(colnames(out))

# labelled data
dict <- get_variable_labels_data(data)
out[, 1] <- replace_dict(out[, 1], dict)
Expand Down
3 changes: 0 additions & 3 deletions R/datasummary_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ datasummary_df <- function(data,

checkmate::assert_data_frame(data)

notes <- escape_string(notes)
title <- escape_string(title)

for (n in colnames(data)) {
fmt <- sanitize_fmt(fmt)
data[[n]] <- fmt(data[[n]])
Expand Down
3 changes: 0 additions & 3 deletions R/datasummary_extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ datasummary_extract <- function(tab,
# stub_width attribute before return
attr(main, 'stub_width') <- stub_width

# escape latex column names
colnames(main) <- escape_string(colnames(main))

# 1 header level means colnames are sufficient. return output immediately.
# this needs to go before definition of header_nocolnames
if (nrow(header) == 1) {
Expand Down
2 changes: 1 addition & 1 deletion R/datasummary_skim.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @param histogram include a histogram (TRUE/FALSE). Supported for:
#' \itemize{
#' \item type = "numeric"
#' \item output is "html", "default", "jpg", "png", or "kableExtra"
#' \item output is "html", "default", "jpg", "png", "tinytable", or "kableExtra"
#' \item PDF and HTML documents compiled via Rmarkdown or knitr
#' \item See the examples section below for an example of how to use
#' `datasummary` to include histograms in other formats such as markdown.
Expand Down
44 changes: 20 additions & 24 deletions R/factory_kableExtra.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ factory_kableExtra <- function(tab,

insight::check_if_installed("kableExtra")

output_format <- settings_get("output_format")

span_list <- get_span_kableExtra(tab)

# escape
if (isTRUE(escape) && isTRUE(output_format %in% c("latex", "html", "typst"))) {
tmp <- escape_everything(
tab = tab,
output_format = output_format,
span_list = span_list,
title = title,
notes = notes)
tab <- tmp$tab
title <- tmp$title
notes <- tmp$notes
span_list <- tmp$span_list
}

# new variable "kable_format" because "kableExtra" and "html" both produce
# html, but we need to distinguish the two.
if (settings_equal("output_format", c("latex", "latex_tabular"))) {
Expand All @@ -34,27 +52,16 @@ factory_kableExtra <- function(tab,
"position", "centering", "vline", "toprule", "bottomrule",
"midrule", "caption.short", "table.envir", "col.names")

# kableExtra::kbl and knitr::kable do not respect the `escape` argument for captions.
# this will never be fixed upstream because of backward compatibility
title <- escape_string(title)

# don't escape \\label{} calls
if (isTRUE(kable_format == "latex")) {
title <- sub("\\\\textbackslash\\{\\}label\\\\\\{(.*)\\\\}", "\\\\label{\\1}", title)
}

arguments <- c(
list(...),
"caption" = title,
"format" = kable_format,
"booktabs" = TRUE,
"escape" = FALSE, # never use kableExtra's default escape; inconsistent
"linesep" = "",
"row.names" = NULL
)

## never use `kableExtra`'s default escape
arguments$escape <- FALSE

## siunitx in preamble
extra_siunitx <- "
\\newcolumntype{d}{S[
Expand Down Expand Up @@ -84,7 +91,6 @@ factory_kableExtra <- function(tab,
tab[[i]] <- ifelse(grepl("[0-9]", tab[[i]]), sprintf("$%s$", tab[[i]]), tab[[i]])
}
}

}
if (any(grepl("d", align))) {
## protect column labels
Expand All @@ -93,17 +99,6 @@ factory_kableExtra <- function(tab,
arguments[["align"]] <- align
}

# span: compute
span_list <- get_span_kableExtra(tab)
if (!is.null(span_list) && settings_equal("output_format", c("kableExtra", "html", "latex"))) {
column_names <- attr(span_list, "column_names")
if (!is.null(column_names)) {
colnames(tab) <- column_names
}
} else {
colnames(tab) <- gsub("\\|{4}", " / ", colnames(tab))
}

# Issue #669: <0.001 gets printed as a tag in HTML
if (settings_equal("output_format", c("kableExtra", "html"))) {
for (i in seq_along(tab)) {
Expand Down Expand Up @@ -181,6 +176,7 @@ factory_kableExtra <- function(tab,
# span: apply (not supported in markdown)
if (!is.null(span_list) && settings_equal("output_format", c("kableExtra", "latex", "html"))) {
for (i in 1:length(span_list)) {
sp <- span_list[[i]]
names(span_list[[i]]) <- gsub("&nbsp;", " ", names(span_list[[i]]))
out <- kableExtra::add_header_above(out, span_list[[i]], escape = TRUE)
}
Expand Down
Loading
Loading