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 0.6.1 #833

Merged
merged 5 commits into from
Nov 16, 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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Remotes: vincentarelbundock/tinytable
Type: Package
Package: modelsummary
Title: Summary Tables and Plots for Statistical Models and Data: Beautiful, Customizable, and Publication-Ready
Expand Down Expand Up @@ -181,7 +182,6 @@ Collate:
'modelsummary_cbind.R'
'modelsummary_list.R'
'modelsummary_rbind.R'
'modelsummary_wide.R'
'poorman.R'
'reexport.R'
'rename_statistics.R'
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export(gof_map)
export(labelSubset)
export(modelplot)
export(modelsummary)
export(modelsummary_wide)
export(msummary)
export(rowLabels)
export(supported_models)
Expand Down
43 changes: 22 additions & 21 deletions R/datasummary.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
#' `datasummary` can use any summary function which produces one numeric or
#' character value per variable. The examples section of this documentation
#' shows how to define custom summary functions.
#'
#' `modelsummary` also supplies several shortcut summary functions which can be used in `datasummary()` formulas: Min, Max, Mean, Median, Var, SD, NPercent, NUnique, Ncol, P0, P25, P50, P75, P100.
#'
#' See the Details and Examples sections below, and the vignettes on the `modelsummary` website:
#'
#' * https://modelsummary.com/
#' `modelsummary` also supplies several shortcut summary functions which can be used in `datasummary()` formulas: Min, Max, Mean, Median, Var, SD, NPercent, NUnique, Ncol, P0, P25, P50, P75, P100.
#'
#' See the Details and Examples sections below, and the vignettes on the `modelsummary` website:
#'
#' * https://modelsummary.com/
#' * https://modelsummary.com/articles/datasummary.html
#'
#' @template kableExtra2tinytable
#'
#' @inheritParams modelsummary
#' @import tables
#' @param formula A two-sided formula to describe the table: rows ~ columns.
Expand All @@ -24,7 +26,7 @@
#' your main table.
#' @param fmt how to format numeric values: integer, user-supplied function, or `modelsummary` function.
#' * Integer: Number of decimal digits
#' * User-supplied functions:
#' * User-supplied functions:
#' - Any function which accepts a numeric vector and returns a character vector of the same length.
#' * `modelsummary` functions:
#' - `fmt = fmt_significant(2)`: Two significant digits (at the term-level)
Expand All @@ -46,7 +48,7 @@
#'
#' ```{r, eval = identical(Sys.getenv("pkgdown"), "true")}
#' library(modelsummary)
#'
#'
#' # The left-hand side of the formula describes rows, and the right-hand side
#' # describes columns. This table uses the "mpg" variable as a row and the "mean"
#' # function as a column:
Expand Down Expand Up @@ -150,7 +152,7 @@
#' @export
datasummary <- function(formula,
data,
output = 'default',
output = "default",
fmt = 2,
title = NULL,
notes = NULL,
Expand All @@ -160,15 +162,14 @@ datasummary <- function(formula,
sparse_header = TRUE,
escape = TRUE,
...) {

if (!isTRUE(list(...)[["internal_call"]])) {
## settings: don't overwrite settings on internal calls
settings_init(settings = list(
"function_called" = "datasummary"
))
}

tmp <- sanitize_output(output) # early
tmp <- sanitize_output(output) # early
output_format <- tmp$output_format
output_factory <- tmp$output_factory
output_file <- tmp$output_file
Expand Down Expand Up @@ -196,8 +197,8 @@ datasummary <- function(formula,
tab <- tryCatch(tables::tabular(formula, data), error = function(e) e)

# informative error message
if (inherits(tab, 'error')) {
if (grepl('Duplicate values:', tab$message)) {
if (inherits(tab, "error")) {
if (grepl("Duplicate values:", tab$message)) {
message('This error often occurs when the "*" nesting operator is used, but none of the nested terms are categorical variables (factor, logical or character types). You can transform your variable in the original data, or wrap it in a Factor() function in the `datasummary` formula.')
}
stop(tab$message)
Expand All @@ -210,20 +211,21 @@ datasummary <- function(formula,
data = data)

# align stub l rest r
stub_width <- attr(dse, 'stub_width')
stub_width <- attr(dse, "stub_width")
tab_width <- ncol(dse)
if (inherits(add_columns, 'data.frame')) {
if (inherits(add_columns, "data.frame")) {
tab_width <- tab_width + ncol(add_columns)
}
if (is.null(align)) {
align <- paste0(strrep('l', stub_width),
strrep('r', tab_width - stub_width))
align <- paste0(
strrep("l", stub_width),
strrep("r", tab_width - stub_width))
}
align <- paste(align, collapse = "")

# convert to numeric if fmt==NULL
if (is.null(fmt)) {
idx <- attr(dse, 'stub_width')
idx <- attr(dse, "stub_width")
for (i in (idx + 1):ncol(dse)) {
dse[[i]] <- as.numeric(dse[[i]])
}
Expand All @@ -247,16 +249,15 @@ datasummary <- function(formula,

# invisible return
if (!is.null(output_file) ||
output == "jupyter" ||
(output == "default" && settings_equal("output_default", "jupyter"))) {
output == "jupyter" ||
(output == "default" && settings_equal("output_default", "jupyter"))) {
if (!isTRUE(list(...)[["internal_call"]])) settings_rm()
return(invisible(out))
# visible return
# visible return
} else {
if (!isTRUE(list(...)[["internal_call"]])) settings_rm()
return(out)
}

}

#' `dsummary()` is a shortcut to `datasummary()`
Expand Down
Loading
Loading