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

Allow export_table() to split to more than 3 tables #952

Merged
merged 15 commits into from
Nov 1, 2024

Conversation

mattansb
Copy link
Member

@mattansb mattansb commented Oct 30, 2024

Closes #951

It's not set to auto, because sometimes a horizontal scrollbar is better than a split table, when it's just one column too wide.

Originally posted by @strengejacke in #951 (comment)

The current implementation will never make a split that leaves a single column table.

z <- mtcars[1,] |> 
  insight::export_table(table_width = NULL) |> 
  strsplit(split = "\n") |> _[[1]]

nchar(z[1], type = "width")
#> [1] 68

# output is too wide, but it will not split a single column
mtcars[1:5,] |> 
  insight::export_table(table_width = 67)
#>   mpg | cyl | disp |  hp | drat |   wt |  qsec | vs | am | gear | carb
#> ----------------------------------------------------------------------
#> 21.00 |   6 |  160 | 110 | 3.90 | 2.62 | 16.46 |  0 |  1 |    4 |    4
#> 21.00 |   6 |  160 | 110 | 3.90 | 2.88 | 17.02 |  0 |  1 |    4 |    4
#> 22.80 |   4 |  108 |  93 | 3.85 | 2.32 | 18.61 |  1 |  1 |    4 |    1
#> 21.40 |   6 |  258 | 110 | 3.08 | 3.21 | 19.44 |  1 |  0 |    3 |    1
#> 18.70 |   8 |  360 | 175 | 3.15 | 3.44 | 17.02 |  0 |  0 |    3 |    2

Created on 2024-10-30 with reprex v2.1.1

So I think it's safe to set table_width = "auto" as the default. WDYT?

@bwiernik
Copy link
Contributor

I think that would be better.

One other thing I wonder—could we repeat the model label columns on each line so that output with many models are able to be read easily on subsequent rows?

@strengejacke
Copy link
Member

This PR is not fully working yet. See this example (from https://easystats.github.io/parameters/articles/model_parameters_print.html#splitting-wide-tables-into-multiple-table-parts):

library(parameters)
data(iris)
lm1 <- lm(Sepal.Length ~ Species, data = iris)
lm2 <- lm(Sepal.Length ~ Species + Petal.Length, data = iris)
lm3 <- lm(Sepal.Length ~ Species * Petal.Length, data = iris)
lm4 <- lm(Sepal.Length ~ Species * Petal.Length + Petal.Width, data = iris)

tab <- compare_parameters(lm1, lm2, lm3, lm4)
print(tab, table_width = 80)

The first column should be repeated for each table.

Current PR

library(parameters)
data(iris)
lm1 <- lm(Sepal.Length ~ Species, data = iris)
lm2 <- lm(Sepal.Length ~ Species + Petal.Length, data = iris)
lm3 <- lm(Sepal.Length ~ Species * Petal.Length, data = iris)
lm4 <- lm(Sepal.Length ~ Species * Petal.Length + Petal.Width, data = iris)

tab <- compare_parameters(lm1, lm2, lm3, lm4)
print(tab, table_width = 80)
#> Parameter                           |               lm1 |                  lm2
#> ------------------------------------------------------------------------------
#> (Intercept)                         | 5.01 (4.86, 5.15) |  3.68 ( 3.47,  3.89)
#> Species [versicolor]                | 0.93 (0.73, 1.13) | -1.60 (-1.98, -1.22)
#> Species [virginica]                 | 1.58 (1.38, 1.79) | -2.12 (-2.66, -1.58)
#> Petal Length                        |                   |  0.90 ( 0.78,  1.03)
#> Species [versicolor] × Petal Length |                   |                     
#> Species [virginica] × Petal Length  |                   |                     
#> Petal Width                         |                   |                     
#> ------------------------------------------------------------------------------
#> Observations                        |               150 |                  150
#> 
#>                  lm3 |                  lm4
#> -------------------------------------------
#>  4.21 ( 3.41,  5.02) |  4.21 ( 3.41,  5.02)
#> -1.81 (-2.99, -0.62) | -1.80 (-2.99, -0.62)
#> -3.15 (-4.41, -1.90) | -3.19 (-4.50, -1.88)
#>  0.54 ( 0.00,  1.09) |  0.54 (-0.02,  1.09)
#>  0.29 (-0.30,  0.87) |  0.28 (-0.30,  0.87)
#>  0.45 (-0.12,  1.03) |  0.45 (-0.12,  1.03)
#>                      |  0.03 (-0.28,  0.34)
#> -------------------------------------------
#>                  150 |                  150

Created on 2024-10-30 with reprex v2.1.1

Desired

library(parameters)
data(iris)
lm1 <- lm(Sepal.Length ~ Species, data = iris)
lm2 <- lm(Sepal.Length ~ Species + Petal.Length, data = iris)
lm3 <- lm(Sepal.Length ~ Species * Petal.Length, data = iris)
lm4 <- lm(Sepal.Length ~ Species * Petal.Length + Petal.Width, data = iris)

tab <- compare_parameters(lm1, lm2, lm3, lm4)
print(tab, table_width = 80)
#> Parameter                           |               lm1 |                  lm2
#> ------------------------------------------------------------------------------
#> (Intercept)                         | 5.01 (4.86, 5.15) |  3.68 ( 3.47,  3.89)
#> Species [versicolor]                | 0.93 (0.73, 1.13) | -1.60 (-1.98, -1.22)
#> Species [virginica]                 | 1.58 (1.38, 1.79) | -2.12 (-2.66, -1.58)
#> Petal Length                        |                   |  0.90 ( 0.78,  1.03)
#> Species [versicolor] × Petal Length |                   |                     
#> Species [virginica] × Petal Length  |                   |                     
#> Petal Width                         |                   |                     
#> ------------------------------------------------------------------------------
#> Observations                        |               150 |                  150
#> 
#> Parameter                           |                  lm3 |                  lm4
#> ---------------------------------------------------------------------------------
#> (Intercept)                         |  4.21 ( 3.41,  5.02) |  4.21 ( 3.41,  5.02)
#> Species [versicolor]                | -1.81 (-2.99, -0.62) | -1.80 (-2.99, -0.62)
#> Species [virginica]                 | -3.15 (-4.41, -1.90) | -3.19 (-4.50, -1.88)
#> Petal Length                        |  0.54 ( 0.00,  1.09) |  0.54 (-0.02,  1.09)
#> Species [versicolor] × Petal Length |  0.29 (-0.30,  0.87) |  0.28 (-0.30,  0.87)
#> Species [virginica] × Petal Length  |  0.45 (-0.12,  1.03) |  0.45 (-0.12,  1.03)
#> Petal Width                         |                      |  0.03 (-0.28,  0.34)
#> ---------------------------------------------------------------------------------
#> Observations                        |                  150 |                  150

Created on 2024-10-30 with reprex v2.1.1

@mattansb
Copy link
Member Author

Was this working before?

@strengejacke
Copy link
Member

Yes

@strengejacke
Copy link
Member

See this code snippet from the original code:

      # copy first column, and all columns that did not fit into the first line
      # into the second table matrix
      if (i > 2 && i < ncol(final)) {
        final2 <- final[, c(1, i:ncol(final))]
        final <- final[, 1:(i - 1)]
      }

@mattansb
Copy link
Member Author

Fixed

data(iris)

lm1 <- lm(Sepal.Length ~ Species, data = iris)
lm2 <- lm(Sepal.Length ~ Species + Petal.Length, data = iris)
lm3 <- lm(Sepal.Length ~ Species * Petal.Length, data = iris)
lm6 <- lm5 <- lm4 <- lm(Sepal.Length ~ Species * Petal.Length + Petal.Width, data = iris)

tab <- parameters::compare_parameters(lm1, lm2, lm3, lm4, lm5, lm6)

print(tab, table_width = 80)
#> Parameter                           |               lm1 |                  lm2
#> ------------------------------------------------------------------------------
#> (Intercept)                         | 5.01 (4.86, 5.15) |  3.68 ( 3.47,  3.89)
#> Species (versicolor)                | 0.93 (0.73, 1.13) | -1.60 (-1.98, -1.22)
#> Species (virginica)                 | 1.58 (1.38, 1.79) | -2.12 (-2.66, -1.58)
#> Petal Length                        |                   |  0.90 ( 0.78,  1.03)
#> Species (versicolor) × Petal Length |                   |                     
#> Species (virginica) × Petal Length  |                   |                     
#> Petal Width                         |                   |                     
#> ------------------------------------------------------------------------------
#> Observations                        |               150 |                  150
#> 
#> Parameter                           |                  lm3
#> ----------------------------------------------------------
#> (Intercept)                         |  4.21 ( 3.41,  5.02)
#> Species (versicolor)                | -1.81 (-2.99, -0.62)
#> Species (virginica)                 | -3.15 (-4.41, -1.90)
#> Petal Length                        |  0.54 ( 0.00,  1.09)
#> Species (versicolor) × Petal Length |  0.29 (-0.30,  0.87)
#> Species (virginica) × Petal Length  |  0.45 (-0.12,  1.03)
#> Petal Width                         |                     
#> ----------------------------------------------------------
#> Observations                        |                  150
#> 
#> Parameter                           |                  lm4
#> ----------------------------------------------------------
#> (Intercept)                         |  4.21 ( 3.41,  5.02)
#> Species (versicolor)                | -1.80 (-2.99, -0.62)
#> Species (virginica)                 | -3.19 (-4.50, -1.88)
#> Petal Length                        |  0.54 (-0.02,  1.09)
#> Species (versicolor) × Petal Length |  0.28 (-0.30,  0.87)
#> Species (virginica) × Petal Length  |  0.45 (-0.12,  1.03)
#> Petal Width                         |  0.03 (-0.28,  0.34)
#> ----------------------------------------------------------
#> Observations                        |                  150
#> 
#> Parameter                           |                  lm5 |                  lm6
#> ---------------------------------------------------------------------------------
#> (Intercept)                         |  4.21 ( 3.41,  5.02) |  4.21 ( 3.41,  5.02)
#> Species (versicolor)                | -1.80 (-2.99, -0.62) | -1.80 (-2.99, -0.62)
#> Species (virginica)                 | -3.19 (-4.50, -1.88) | -3.19 (-4.50, -1.88)
#> Petal Length                        |  0.54 (-0.02,  1.09) |  0.54 (-0.02,  1.09)
#> Species (versicolor) × Petal Length |  0.28 (-0.30,  0.87) |  0.28 (-0.30,  0.87)
#> Species (virginica) × Petal Length  |  0.45 (-0.12,  1.03) |  0.45 (-0.12,  1.03)
#> Petal Width                         |  0.03 (-0.28,  0.34) |  0.03 (-0.28,  0.34)
#> ---------------------------------------------------------------------------------
#> Observations                        |                  150 |                  150

Created on 2024-10-30 with reprex v2.1.1


library(performance)

data(HolzingerSwineford1939, package = "lavaan")
structure <- " visual  =~ x1 + x2 + x3
               textual =~ x4 + x5 + x6
               speed   =~ x7 + x8 + x9 "
model1 <- lavaan::cfa(structure, data = HolzingerSwineford1939)
model2 <- lavaan::cfa(structure, data = HolzingerSwineford1939)

compare_performance(model1, model2) |> print(table_width = 50)
#> When comparing models, please note that probably not all models were fit
#>   from same data.
#> # Comparison of Model Performance Indices
#> 
#> Name   |  Model | Chi2(24) | p (Chi2)
#> -------------------------------------
#> model1 | lavaan |   85.306 |   < .001
#> model2 | lavaan |   85.306 |   < .001
#> 
#> Name   | Baseline(36) | p (Baseline) |   GFI
#> --------------------------------------------
#> model1 |      918.852 |       < .001 | 0.943
#> model2 |      918.852 |       < .001 | 0.943
#> 
#> Name   |  AGFI |   NFI |  NNFI |   CFI | RMSEA
#> ----------------------------------------------
#> model1 | 0.894 | 0.907 | 0.896 | 0.931 | 0.092
#> model2 | 0.894 | 0.907 | 0.896 | 0.931 | 0.092
#> 
#> Name   |    RMSEA  CI | p (RMSEA) |   RMR |  SRMR
#> -------------------------------------------------
#> model1 | [0.07, 0.11] |    < .001 | 0.082 | 0.065
#> model2 | [0.07, 0.11] |    < .001 | 0.082 | 0.065
#> 
#> Name   |   RFI |  PNFI |   IFI |   RNI
#> --------------------------------------
#> model1 | 0.861 | 0.605 | 0.931 | 0.931
#> model2 | 0.861 | 0.605 | 0.931 | 0.931
#> 
#> Name   | Loglikelihood |  AIC (weights)
#> ---------------------------------------
#> model1 |     -3737.745 | 7517.5 (0.500)
#> model2 |     -3737.745 | 7517.5 (0.500)
#> 
#> Name   |  BIC (weights) | BIC_adjusted
#> --------------------------------------
#> model1 | 7595.3 (0.500) |     7528.739
#> model2 | 7595.3 (0.500) |     7528.739

Created on 2024-10-30 with reprex v2.1.1

@strengejacke
Copy link
Member

I added some tests. Can be merged when all tests pass.

@mattansb
Copy link
Member Author

mattansb commented Nov 1, 2024

How about setting "auto" to be the default... ;)

This reverts commit ced0fbf.
@strengejacke
Copy link
Member

Let's change to "auto" in this PR, then check parameters with remotes to this PR and see how vignettes render? We have to make sure that everywhere, where we call export_table(), dots are passed to this function as well (so users can modify table_width).

@mattansb
Copy link
Member Author

mattansb commented Nov 1, 2024

Alright, let's do it - we can see that everything is okay in parameters and then take care of the rest of the packages (there are currently 55 files that use this function across easystats).

Merging!

@mattansb mattansb merged commit 410dc71 into main Nov 1, 2024
18 of 20 checks passed
@mattansb mattansb deleted the longer-export_table branch November 1, 2024 21:33
@mattansb
Copy link
Member Author

mattansb commented Nov 1, 2024

It might be better if we have a global option for this, since looking at the various codes it might be difficult to pass ... all the way down to export_table().

option(easystats.table_width = 80)

# In the function

if (is.numeric(table_width)) {
  line_width <- table_width
} else {
  line_width <- getOption("easystats.table_width", default = getOption("width"))
}

mattansb added a commit to easystats/report that referenced this pull request Nov 3, 2024
test with changes introduced in easystats/insight#952
mattansb added a commit to easystats/correlation that referenced this pull request Nov 3, 2024
test with changes introduced in easystats/insight#952
mattansb added a commit to easystats/datawizard that referenced this pull request Nov 3, 2024
test with changes introduced in easystats/insight#952
mattansb added a commit to easystats/performance that referenced this pull request Nov 3, 2024
test with changes introduced in easystats/insight#952
mattansb added a commit to easystats/see that referenced this pull request Nov 3, 2024
test with changes introduced in easystats/insight#952
mattansb added a commit to easystats/effectsize that referenced this pull request Nov 3, 2024
test with changes introduced in easystats/insight#952
mattansb added a commit to easystats/bayestestR that referenced this pull request Nov 3, 2024
test with changes introduced in easystats/insight#952
mattansb added a commit to easystats/modelbased that referenced this pull request Nov 3, 2024
test with changes introduced in easystats/insight#952
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Overflow export_table(format = "text")
3 participants