diff --git a/DESCRIPTION b/DESCRIPTION index fe77f04cb..f100506cf 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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) . -Version: 1.4.1.9010 +Version: 1.4.2 Authors@R: c(person("Vincent", "Arel-Bundock", email = "vincent.arel-bundock@umontreal.ca", role = c("aut", "cre"), @@ -52,7 +52,7 @@ Imports: data.table, generics, glue, - kableExtra, + kableExtra (>= 1.3.4), insight (>= 0.19.1), parameters (>= 0.21.0), performance (>= 0.10.2), @@ -63,7 +63,6 @@ Suggests: betareg, bookdown, brms, - rstanarm, broom, broom.mixed, car, @@ -74,7 +73,6 @@ Suggests: DT, estimatr, fixest, - tictoc, flextable, future, future.apply, @@ -82,11 +80,11 @@ Suggests: ggdist, ggplot2, gt (>= 0.8.0), + gtExtras, haven, huxtable, IRdisplay, ivreg, - kableExtra (>= 1.3.4), knitr, lavaan, lfe, @@ -106,17 +104,19 @@ Suggests: pscl, remotes, rmarkdown, + rstanarm, rsvg, sandwich, spelling, survey, survival, tibble, + tictoc, tidyselect, - tinytex, tidyverse, tinysnapshot, tinytest, + tinytex, wesanderson License: GPL-3 Encoding: UTF-8 diff --git a/NEWS.md b/NEWS.md index eaf8770aa..df9565b87 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,10 +1,11 @@ -# modelsummary 1.4.1 (development) +# modelsummary 1.4.2 New: * Minimal support for `Typst` output, with auto-detection in Quarto documents. * `strip` argument in `dvnames`. * `s.value` statistic is now available whenever `p.value` is available. See Greenland (2019). +* `datasummary_skim()` now includes histograms in `gt` tables. Bugs: diff --git a/R/datasummary_skim.R b/R/datasummary_skim.R index 00a0bf47a..89e703a3d 100644 --- a/R/datasummary_skim.R +++ b/R/datasummary_skim.R @@ -161,7 +161,7 @@ datasummary_skim_numeric <- function( if (histogram) { # histogram is a kableExtra-specific option - if (!settings_equal("output_factory", "kableExtra")) { + if (!settings_equal("output_factory", c("kableExtra", "gt"))) { histogram <- FALSE } @@ -174,12 +174,16 @@ datasummary_skim_numeric <- function( # interactive or Rmarkdown/knitr } else { if (isTRUE(check_dependency("knitr"))) { - if (!settings_equal("output_format", c("default", "jupyter", "html", "kableExtra")) && + if (!settings_equal("output_format", c("default", "jupyter", "html", "kableExtra", "gt")) && !knitr::is_latex_output()) { histogram <- FALSE } + # gt cannot print histograms in latex + if (knitr::is_latex_output() && settings_equal("output_format", "gt")) { + histogram <- FALSE + } } else { - if (!settings_equal("output_format", c("default", "jupyter", "html", "kableExtra"))) { + if (!settings_equal("output_format", c("default", "jupyter", "html", "kableExtra", "gt"))) { histogram <- FALSE } } @@ -187,7 +191,7 @@ datasummary_skim_numeric <- function( # if flag was flipped if (!histogram) { - insight::format_warning('The histogram argument is only supported for (a) output types "default", "html", or "kableExtra"; (b) writing to file paths with extensions ".html", ".jpg", or ".png"; and (c) Rmarkdown or knitr documents compiled to PDF or HTML. Use `histogram=FALSE` to silence this warning.') + insight::format_warning('The histogram argument is only supported for (a) output types "default", "html", "kableExtra", or "gt"; (b) writing to file paths with extensions ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use `histogram=FALSE` to silence this warning.') } } @@ -254,25 +258,39 @@ datasummary_skim_numeric <- function( output_fmt <- output } - # draw table cache <- settings_cache(c("output_format", "output_file", "output_factory")) - out <- datasummary(formula = f, - data = dat_lab, - output = "kableExtra", - # output = output_fmt, - title = title, - align = align, - notes = notes, - escape = escape, - internal_call = TRUE) - settings_restore(cache) - out <- kableExtra::column_spec(out, + out <- datasummary( + formula = f, + data = dat_lab, + output = output_fmt, + title = title, + align = align, + notes = notes, + escape = escape, + internal_call = TRUE) + + if (identical(cache$output_factory, "gt")) { + insight::check_if_installed("gtExtras") + tmp <- data.table::data.table(a = histogram_list) + out[["_data"]][, ncol(out[["_data"]])] <- tmp[, 1, drop = FALSE] + out <- gtExtras::gt_plt_dist(out, + column = ncol(out[["_data"]]), + type = "histogram", + fill_color = "black", + line_color = "black", + same_limit = FALSE) + + } else { + out <- kableExtra::column_spec(out, column = 9, image = kableExtra::spec_hist(histogram_list, - col = "black", - same_lim = FALSE)) + col = "black", + same_lim = FALSE)) + } + + settings_restore(cache) # don't use output=filepath.html when post-processing if (!is.null(settings_get("output_file"))) { diff --git a/R/factory_typst.R b/R/factory_typst.R index a1235622f..c6c081372 100644 --- a/R/factory_typst.R +++ b/R/factory_typst.R @@ -66,6 +66,15 @@ typstable <- function( } else { cn <- NULL } + cn <- gsub("\\*", "\\\\*", cn) + cn <- gsub("\\#", "\\\\#", cn) + + # escape asterisks, which have a special delimiter meaning in Typst + for (i in seq_along(x)) { + x[[i]] <- gsub("\\*", "\\\\*", x[[i]]) + x[[i]] <- gsub("\\#", "\\\\#", x[[i]]) + } + # cells tab <- data.frame(apply(x, 1:2, function(z) sprintf("[%s]", z))) @@ -79,6 +88,7 @@ typstable <- function( inset: %s, columns: %s, align: %s, +stroke: none, %s )", inset, columns, align, tab) diff --git a/inst/tinytest/test-datasummary_skim.R b/inst/tinytest/test-datasummary_skim.R index faa494f0e..d4d2c6347 100644 --- a/inst/tinytest/test-datasummary_skim.R +++ b/inst/tinytest/test-datasummary_skim.R @@ -107,6 +107,10 @@ tab <- datasummary_skim(penguins) expect_inherits(tab, "kableExtra") +# Issue #627: histograms in gt +expect_inherits(datasummary_skim(mtcars, output = "gt"), "gt_tbl") + + # # RDatasets tests: must be commented out # any_categorical <- function(x) { # # datasummary_skim ignores characters with more than 10 levels