Skip to content

Commit 3f35d37

Browse files
Add display() methods and support for tinytable (#647)
* Add `display()` methods and support for tinytable * add to suggests * fix * fix * fix * fix * fix * comment code * typo * add tests * update snaps * add tests * fix for tt * add test * typo * fix * docs * Update R/data_tabulate.R Co-authored-by: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> * address comments --------- Co-authored-by: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com>
1 parent be8875c commit 3f35d37

15 files changed

+550
-16
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: datawizard
33
Title: Easy Data Wrangling and Statistical Transformations
4-
Version: 1.2.0.4
4+
Version: 1.2.0.5
55
Authors@R: c(
66
person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut",
77
comment = c(ORCID = "0000-0003-1995-6531")),
@@ -69,6 +69,7 @@ Suggests:
6969
testthat (>= 3.2.1),
7070
tibble,
7171
tidyr,
72+
tinytable (>= 0.13.0),
7273
withr
7374
VignetteBuilder:
7475
knitr

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ S3method(describe_distribution,factor)
7474
S3method(describe_distribution,grouped_df)
7575
S3method(describe_distribution,list)
7676
S3method(describe_distribution,numeric)
77+
S3method(display,data_codebook)
7778
S3method(display,datawizard_crosstab)
7879
S3method(display,datawizard_crosstabs)
7980
S3method(display,datawizard_table)

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ CHANGES
1616
* Argument `values_from` in `data_to_wide()` now supports select-helpers like
1717
the `select` argument in other `{datawizard}` functions (#645).
1818

19+
* Added a `display()` method for `data_codebook()` (#646).
20+
21+
* `display()` methods now support the `{tinytable}` package. Use `format = "tt"`
22+
to export tables as `tinytable` objects (#646).
23+
1924
BUG FIXES
2025

2126
* Fixed an issue when `demean()`ing nested structures with more than 2 grouping

R/data_codebook.R

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#' @param row_color For HTML tables, the fill color for odd rows.
2424
#' @inheritParams standardize.data.frame
2525
#' @inheritParams extract_column_names
26+
#' @inheritParams data_tabulate
2627
#'
2728
#' @return A formatted data frame, summarizing the content of the data frame.
2829
#' Returned columns include the column index of the variables in the original
@@ -414,7 +415,6 @@ print_html.data_codebook <- function(x,
414415
line_padding = 3,
415416
row_color = "#eeeeee",
416417
...) {
417-
insight::check_if_installed("gt")
418418
caption <- .get_codebook_caption(x)
419419
attr(x, "table_caption") <- caption
420420
# since we have each value at its own row, the HTML table contains
@@ -427,12 +427,20 @@ print_html.data_codebook <- function(x,
427427
odd_rows <- (x$.row_id %% 2 == 1)
428428
x$.row_id <- NULL
429429
# create basic table
430+
backend <- .check_format_backend(...)
430431
out <- insight::export_table(
431432
format(x, format = "html"),
432433
title = caption,
433-
format = "html",
434+
format = backend,
434435
align = .get_codebook_align(x)
435436
)
437+
438+
# for tiny table output, we don't need to do any further formatting
439+
if (identical(backend, "tt")) {
440+
return(out)
441+
}
442+
443+
insight::check_if_installed("gt")
436444
# no border for rows which are not separator lines
437445
out <- gt::tab_style(
438446
out,
@@ -455,6 +463,34 @@ print_html.data_codebook <- function(x,
455463
}
456464

457465

466+
#' @rdname data_codebook
467+
#' @export
468+
display.data_codebook <- function(object,
469+
format = "markdown",
470+
font_size = "100%",
471+
line_padding = 3,
472+
row_color = "#eeeeee",
473+
...) {
474+
format <- .display_default_format(format)
475+
476+
fun_args <- list(
477+
x = object,
478+
font_size = font_size,
479+
line_padding = line_padding,
480+
row_color = row_color,
481+
...
482+
)
483+
484+
# print table in HTML or markdown format
485+
if (format %in% c("html", "tt")) {
486+
fun_args$backend <- format
487+
do.call(print_html, fun_args)
488+
} else {
489+
do.call(print_md, fun_args)
490+
}
491+
}
492+
493+
458494
#' @export
459495
print_md.data_codebook <- function(x, ...) {
460496
caption <- .get_codebook_caption(x)

R/data_tabulate.R

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
#' input. Else, always for multiple table inputs or when `simplify = FALSE`, a
3838
#' list of tables is returned. This is only relevant for the `as.table()`
3939
#' methods. To ensure consistent output, the default is `FALSE`.
40+
#' @param object An object returned by `data_tabulate()`.
41+
#' @param format String, indicating the output format. Can be `"markdown"`
42+
#' `"html"`, or `"tt"`. `format = "html"` create an HTML table using the *gt*
43+
#' package. `format = "tt"` creates a `tinytable` object, which is either
44+
#' printed as markdown or HTML table, depending on the environment. See
45+
#' [`insight::export_table()`] for details.
4046
#' @param verbose Toggle warnings and messages.
4147
#' @param ... not used.
4248
#' @inheritParams extract_column_names
@@ -794,14 +800,23 @@ print.datawizard_tables <- function(x, big_mark = NULL, ...) {
794800

795801
# display --------------------
796802

803+
#' @rdname data_tabulate
797804
#' @export
798805
display.datawizard_table <- function(object, big_mark = NULL, format = "markdown", ...) {
799-
format <- insight::validate_argument(format, c("markdown", "html", "md"))
806+
format <- .display_default_format(format)
807+
808+
fun_args <- list(
809+
x = object,
810+
big_mark = big_mark,
811+
...
812+
)
813+
800814
# print table in HTML or markdown format
801-
if (format == "html") {
802-
print_html(object, big_mark = big_mark, ...)
815+
if (format %in% c("html", "tt")) {
816+
fun_args$backend <- format
817+
do.call(print_html, fun_args)
803818
} else {
804-
print_md(object, big_mark = big_mark, ...)
819+
do.call(print_md, fun_args)
805820
}
806821
}
807822

@@ -814,6 +829,11 @@ display.datawizard_crosstab <- display.datawizard_table
814829
#' @export
815830
display.datawizard_crosstabs <- display.datawizard_table
816831

832+
.display_default_format <- function(format) {
833+
format <- getOption("easystats_display_format", format)
834+
insight::validate_argument(format, c("markdown", "html", "md", "tt"))
835+
}
836+
817837

818838
# print_html --------------------
819839

@@ -847,7 +867,7 @@ print_html.datawizard_tables <- function(x, big_mark = NULL, ...) {
847867
out,
848868
missing = "<NA>",
849869
caption = ifelse(is_weighted, "Frequency Table (weighted)", "Frequency Table"),
850-
format = "html",
870+
format = .check_format_backend(...),
851871
group_by = "Group"
852872
)
853873
}
@@ -919,17 +939,36 @@ print_md.datawizard_tables <- function(x, big_mark = NULL, ...) {
919939
x$Variable <- NULL
920940
x$Group <- NULL
921941

942+
# this function is used by all four supported format, markdown, text, html
943+
# and tt (tinytable). For tt, we sometimes have format "html" and backend = "tt",
944+
# so we need to check for this special case
945+
backend <- switch(format,
946+
html = ,
947+
tt = .check_format_backend(...),
948+
format
949+
)
922950
# print table
923951
insight::export_table(
924952
format(x, format = format, big_mark = big_mark, ...),
925953
title = caption,
926954
footer = footer,
927955
missing = "(NA)",
928-
format = format
956+
format = backend
929957
)
930958
}
931959

932960

961+
# we allow exporting HTML format based on "gt" or "tinytable"
962+
.check_format_backend <- function(...) {
963+
dots <- list(...)
964+
if (identical(dots$backend, "tt")) {
965+
"tt"
966+
} else {
967+
"html"
968+
}
969+
}
970+
971+
933972
.table_header <- function(x, format = "text") {
934973
a <- attributes(x)
935974

R/data_xtabulate.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,21 @@ print_html.datawizard_crosstab <- function(x, big_mark = NULL, ...) {
226226
x$Group <- NULL
227227
}
228228

229+
# this function is used by all four supported format, markdown, text, html
230+
# and tt (tinytable). For tt, we sometimes have format "html" and backend = "tt",
231+
# so we need to check for this special case
232+
backend <- switch(format,
233+
html = ,
234+
tt = .check_format_backend(...),
235+
format
236+
)
229237
# prepare table arguments
230238
fun_args <- list(
231239
format(x, big_mark = big_mark, format = format, ...),
232240
caption = caption,
233-
format = format
241+
format = backend
234242
)
235-
if (format != "html") {
243+
if (!format %in% c("html", "tt")) {
236244
fun_args$cross <- "+"
237245
fun_args$empty_line <- "-"
238246
}

R/describe_distribution.R

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -606,17 +606,42 @@ print_html.parameters_distribution <- function(x, digits = 2, ci_brackets = c("(
606606
...
607607
)
608608

609-
insight::export_table(formatted_table, format = "html", align = "firstleft", ...)
609+
# determine backend
610+
backend <- .check_format_backend(...)
611+
612+
# pass arguments to export_table
613+
fun_args <- list(
614+
formatted_table,
615+
format = backend,
616+
...
617+
)
618+
619+
# no "align" for format "tt" - this currently gives an error. Not sure
620+
# if related to insight::export_table or tinytable
621+
if (identical(backend, "html")) {
622+
fun_args$align <- "firstleft"
623+
}
624+
625+
do.call(insight::export_table, fun_args)
610626
}
611627

612628

613629
#' @export
614630
display.parameters_distribution <- function(object, format = "markdown", digits = 2, ...) {
615-
format <- insight::validate_argument(format, c("md", "markdown", "html"))
616-
if (format == "html") {
617-
print_html(x = object, digits = digits, ...)
631+
format <- .display_default_format(format)
632+
633+
fun_args <- list(
634+
x = object,
635+
digits = digits,
636+
...
637+
)
638+
639+
# print table in HTML or markdown format
640+
if (format %in% c("html", "tt")) {
641+
fun_args$backend <- format
642+
do.call(print_html, fun_args)
618643
} else {
619-
print_md(x = object, digits = digits, ...)
644+
do.call(print_md, fun_args)
620645
}
621646
}
622647

man/data_codebook.Rd

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/data_tabulate.Rd

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)