Skip to content

Commit 74fc070

Browse files
authored
Make display() visible (#535)
* Make `display()` visible * fix * typo, tests
1 parent 6bd0ab6 commit 74fc070

File tree

7 files changed

+116
-11
lines changed

7 files changed

+116
-11
lines changed

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: modelbased
33
Title: Estimation of Model-Based Predictions, Contrasts and Means
4-
Version: 0.12.0.1
4+
Version: 0.12.0.2
55
Authors@R:
66
c(person(given = "Dominique",
77
family = "Makowski",
@@ -98,6 +98,7 @@ Suggests:
9898
see (>= 0.11.0),
9999
survival,
100100
testthat (>= 3.2.1),
101+
tinytable,
101102
vdiffr,
102103
withr
103104
VignetteBuilder:
@@ -111,3 +112,4 @@ Roxygen: list(markdown = TRUE)
111112
Config/Needs/check: stan-dev/cmdstanr
112113
Config/Needs/website: easystats/easystatstemplate
113114
LazyData: true
115+
Remotes: easystats/insight

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* `estimate_expectation()` and `estimate_relation()` now support objects of
66
class `htest`.
77

8+
* Documentation of the `display()` method for *modelbased* objects has been
9+
added.
10+
811
# modelbased 0.12.0
912

1013
## Changes

R/display.R

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
1+
#' @rdname print.estimate_contrasts
12
#' @export
2-
display.estimate_contrasts <- function(object, format = "markdown", ...) {
3-
insight::validate_argument(format, c("md", "markdown", "html"))
4-
if (format == "html") {
5-
print_html(object, ...)
3+
display.estimate_contrasts <- function(object,
4+
select = NULL,
5+
include_grid = NULL,
6+
full_labels = NULL,
7+
format = "markdown",
8+
...) {
9+
# Process argument ---------------------------------------------------------
10+
# --------------------------------------------------------------------------
11+
12+
format <- insight::validate_argument(format, c("md", "markdown", "html", "tt"))
13+
14+
# set defaults
15+
if (is.null(select)) {
16+
select <- getOption("modelbased_select", NULL)
17+
}
18+
if (is.null(include_grid)) {
19+
include_grid <- getOption("modelbased_include_grid", FALSE)
20+
}
21+
if (is.null(full_labels)) {
22+
full_labels <- getOption("modelbased_full_labels", TRUE)
23+
}
24+
25+
fun_args <- list(
26+
x = object,
27+
select = select,
28+
include_grid = include_grid,
29+
full_labels = full_labels
30+
)
31+
32+
if (format %in% c("html", "tt")) {
33+
fun_args$backend <- format
34+
do.call(print_html, c(fun_args, list(...)))
635
} else {
7-
print_md(object, ...)
36+
do.call(print_md, c(fun_args, list(...)))
837
}
938
}
1039

R/print.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' `print()` method for **modelbased** objects. Can be used to tweak the output
44
#' of tables.
55
#'
6-
#' @param x An object returned by the different `estimate_*()` functions.
6+
#' @param x,object An object returned by the different `estimate_*()` functions.
77
#' @param include_grid Logical, if `TRUE`, the data grid is included in the
88
#' table output. Only applies to prediction-functions like `estimate_relation()`
99
#' or `estimate_link()`. Default is `NULL`, which will set the value based on
@@ -12,6 +12,11 @@
1212
#' are shown. If `FALSE`, redundant (duplicated) labels are removed from rows.
1313
#' Default is `NULL`, which will set the value based on
1414
#' `options(modelbased_full_labels)`, and use `TRUE` is no option is set.
15+
#' @param format String, indicating the output format. Can be `"markdown"`
16+
#' `"html"`, or `"tt"`. `format = "html"` create a HTML table using the *gt*
17+
#' package. `format = "tt"` creates a `tinytable` object, which is either
18+
#' printed as markdown or HTML table, depending on the environment. See
19+
#' [`insight::export_table()`] for details.
1520
#' @param ... Arguments passed to `insight::format_table()` or
1621
#' `insight::export_table()`.
1722
#'

man/print.estimate_contrasts.Rd

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

tests/testthat/_snaps/print.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
# estimate_means - using display() to print multiple by's
2+
3+
Code
4+
display(estimate_means(fit, c("c12hour = c(50, 100)", "c172code", "c161sex")),
5+
format = "tt")
6+
Output
7+
8+
+---------+---------------------------------+---------+-------+------+----------------+--------+
9+
| c12hour | c172code | c161sex | Mean | SE | 95% CI | t(791) |
10+
+=========+=================================+=========+=======+======+================+========+
11+
| 50 | low level of education | Male | 11.05 | 0.84 | ( 9.40, 12.69) | 13.16 |
12+
+---------+---------------------------------+---------+-------+------+----------------+--------+
13+
| 100 | low level of education | Male | 10.38 | 1.98 | ( 6.49, 14.26) | 5.25 |
14+
+---------+---------------------------------+---------+-------+------+----------------+--------+
15+
| 50 | intermediate level of education | Male | 12.35 | 0.59 | (11.20, 13.50) | 21.05 |
16+
+---------+---------------------------------+---------+-------+------+----------------+--------+
17+
| 100 | intermediate level of education | Male | 13.31 | 1.26 | (10.83, 15.78) | 10.54 |
18+
+---------+---------------------------------+---------+-------+------+----------------+--------+
19+
| 50 | high level of education | Male | 11.70 | 0.67 | (10.39, 13.02) | 17.45 |
20+
+---------+---------------------------------+---------+-------+------+----------------+--------+
21+
| 100 | high level of education | Male | 12.43 | 1.24 | (10.00, 14.87) | 10.01 |
22+
+---------+---------------------------------+---------+-------+------+----------------+--------+
23+
| 50 | low level of education | Female | 11.99 | 0.33 | (11.35, 12.63) | 36.73 |
24+
+---------+---------------------------------+---------+-------+------+----------------+--------+
25+
| 100 | low level of education | Female | 11.72 | 0.48 | (10.79, 12.66) | 24.61 |
26+
+---------+---------------------------------+---------+-------+------+----------------+--------+
27+
| 50 | intermediate level of education | Female | 12.09 | 0.21 | (11.69, 12.50) | 58.05 |
28+
+---------+---------------------------------+---------+-------+------+----------------+--------+
29+
| 100 | intermediate level of education | Female | 12.91 | 0.35 | (12.23, 13.60) | 37.19 |
30+
+---------+---------------------------------+---------+-------+------+----------------+--------+
31+
| 50 | high level of education | Female | 12.84 | 0.42 | (12.01, 13.67) | 30.36 |
32+
+---------+---------------------------------+---------+-------+------+----------------+--------+
33+
| 100 | high level of education | Female | 13.21 | 0.84 | (11.56, 14.85) | 15.75 |
34+
+=========+=================================+=========+=======+======+================+========+
35+
| Variable predicted: neg_c_7<br>Predictors modulated: c12hour = c(50, 100), c172code, |
36+
| c161sex<br>Predictors averaged: barthtot (65) |
37+
+=========+=================================+=========+=======+======+================+========+
38+
Table: Estimated Marginal Means
39+
140
# estimate_contrasts - by with special character
241

342
Code

tests/testthat/test-print.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ test_that("estimate_means - print multiple by's", {
4141
})
4242

4343

44+
test_that("estimate_means - using display() to print multiple by's", {
45+
skip_if_not_installed("tinytable")
46+
data(efc, package = "modelbased")
47+
48+
# make categorical
49+
efc <- datawizard::to_factor(efc, c("c161sex", "c172code", "e16sex"))
50+
fit <- lm(neg_c_7 ~ c12hour * barthtot * c161sex * c172code, data = efc)
51+
expect_snapshot(display(estimate_means(fit, c("c12hour = c(50, 100)", "c172code", "c161sex")), format = "tt"))
52+
})
53+
54+
4455
test_that("estimate_means - full labels", {
4556
data(efc, package = "modelbased")
4657
# make categorical

0 commit comments

Comments
 (0)