Most of objects encountered throughout the {easystats} packages are“tables”, i.e., a 2D matrix with columns and rows. In R, these objectsare often, at their core,data frames. Let’s create one to useas an example:
library(insight)df<-data.frame(Variable =c(1,3,5,3,1),Group =c("A","A","A","B","B"),CI =c(0.95,0.95,0.95,0.95,0.95),CI_low =c(3.35,2.425,6.213,12.1,1.23),CI_high =c(4.23,5.31,7.123,13.5,3.61),p =c(0.001,0.0456,0.45,0.0042,0.34))df#> Variable Group CI CI_low CI_high p#> 1 1 A 0.95 3.350 4.230 0.0010#> 2 3 A 0.95 2.425 5.310 0.0456#> 3 5 A 0.95 6.213 7.123 0.4500#> 4 3 B 0.95 12.100 13.500 0.0042#> 5 1 B 0.95 1.230 3.610 0.3400When I display in in the console (calling an object -e.g. df - is actually equivalent to callingprint(df)), the output looks alright, but it could beimproved. Some packages, such as {knitr}, have functions to create anicer output. For instance, in markdown, so that it can be nicelyrendered in markdown documents when copied:
| Variable|Group | CI| CI_low| CI_high| p||--------:|:-----|----:|------:|-------:|------:|| 1|A | 0.95| 3.350| 4.230| 0.0010|| 3|A | 0.95| 2.425| 5.310| 0.0456|| 5|A | 0.95| 6.213| 7.123| 0.4500|| 3|B | 0.95| 12.100| 13.500| 0.0042|| 1|B | 0.95| 1.230| 3.610| 0.3400|Or HTML, which again makes it look great in HTML files (such as thiswebpage you’re reading):
| Variable | Group | CI | CI_low | CI_high | p |
|---|---|---|---|---|---|
| 1 | A | 0.95 | 3.350 | 4.230 | 0.0010 |
| 3 | A | 0.95 | 2.425 | 5.310 | 0.0456 |
| 5 | A | 0.95 | 6.213 | 7.123 | 0.4500 |
| 3 | B | 0.95 | 12.100 | 13.500 | 0.0042 |
| 1 | B | 0.95 | 1.230 | 3.610 | 0.3400 |
The {insight} package also contains function to improve the“printing”, or rendering, of tables. Its design dissociates two separateand independent steps:formatting andexporting.
The purpose of formatting is to improve a given table, while stillkeeping it as a regular R data frame, so that it can be for instancefurther modified by the user.
format_table(df)#> Variable Group 95% CI p#> 1 1 A [ 3.35, 4.23] 0.001#> 2 3 A [ 2.42, 5.31] 0.046#> 3 5 A [ 6.21, 7.12] 0.450#> 4 3 B [12.10, 13.50] 0.004#> 5 1 B [ 1.23, 3.61] 0.340As you can see,format_table() modifies columns, turningnumber into characters (so that it has the same amount of digits), anddetecting confidence intervals. This is usually combined withcolumn-specific formatting functions, likeformat_p():
Withuse_symbols = TRUE, it is possible to rendercertain effect size names as symbols, if these are used as column names.Note that this only works on OS X or Linux, or on Windows from R 4.2 orhigher.
x<-data.frame(phi_adjusted =0.3,Glass_delta =0.4,Epsilon2 =0.7,R2 =0.4)# standard outputformat_table(x)#> Phi (adj.) Glass' delta Epsilon2 R2#> 1 0.30 0.40 0.70 0.40# column names of effect sizes as symbolsformat_table(x,use_symbols =TRUE)#> ϕ (adj.) Glass' Δ ε² R²#> 1 0.30 0.40 0.70 0.40In combination withexport_table() (see next section),this will give you nicely formatted tables.
export_table(format_table(x,use_symbols =TRUE))#> ϕ (adj.) | Glass' Δ | ε² | R²#> ---------------------------------#> 0.30 | 0.40 | 0.70 | 0.40The next step isexporting, which takes a data frame andrenders it in a given format, so that it looks good in the console, orin markdown, HTML or latex.
export_table(df)#> Variable | Group | CI | CI_low | CI_high | p#> -----------------------------------------------------#> 1 | A | 0.95 | 3.35 | 4.23 | 1.00e-03#> 3 | A | 0.95 | 2.42 | 5.31 | 0.05#> 5 | A | 0.95 | 6.21 | 7.12 | 0.45#> 3 | B | 0.95 | 12.10 | 13.50 | 4.20e-03#> 1 | B | 0.95 | 1.23 | 3.61 | 0.34For markdown or HTML, simply change theformat argumentto markdown (“md”)…
| Variable | Group | CI | CI_low | CI_high | p |
|---|---|---|---|---|---|
| 1 | A | 0.95 | 3.35 | 4.23 | 1.00e-03 |
| 3 | A | 0.95 | 2.42 | 5.31 | 0.05 |
| 5 | A | 0.95 | 6.21 | 7.12 | 0.45 |
| 3 | B | 0.95 | 12.10 | 13.50 | 4.20e-03 |
| 1 | B | 0.95 | 1.23 | 3.61 | 0.34 |
…or HTML format.
| Variable | CI | CI_low | CI_high | p |
|---|---|---|---|---|
| A | ||||
| 1 | 0.95 | 3.35 | 4.23 | 1.00e-03 |
| 3 | 0.95 | 2.42 | 5.31 | 0.05 |
| 5 | 0.95 | 6.21 | 7.12 | 0.45 |
| B | ||||
| 3 | 0.95 | 12.10 | 13.50 | 4.20e-03 |
| 1 | 0.95 | 1.23 | 3.61 | 0.34 |
This can be combined withformat_table().
| Variable | 95% CI | p |
|---|---|---|
| A | ||
| 1 | ( 3.35, 4.23) | 0.001 |
| 3 | ( 2.42, 5.31) | 0.046 |
| 5 | ( 6.21, 7.12) | 0.450 |
| B | ||
| 3 | (12.10, 13.50) | 0.004 |
| 1 | ( 1.23, 3.61) | 0.340 |
display()Whileexport_table() gives you fine-grained control, thedisplay() function serves as a high-level wrapper. It isdesigned to take an object and “display” it in a user-friendly format.Most of the functions in{easystats} that producetable-like outputs usually also have adisplay() method,which is a convenient way to visualize the results in different formats,which integrates nicely in markdown or quarto documents.
Let’s see how it works with our example data frame:
| Variable | Group | CI | CI_low | CI_high | p |
|---|---|---|---|---|---|
| 1 | A | 0.95 | 3.35 | 4.23 | 1.00e-03 |
| 3 | A | 0.95 | 2.42 | 5.31 | 0.05 |
| 5 | A | 0.95 | 6.21 | 7.12 | 0.45 |
| 3 | B | 0.95 | 12.10 | 13.50 | 4.20e-03 |
| 1 | B | 0.95 | 1.23 | 3.61 | 0.34 |
You can specify the output format using theformatargument. For instance, to create a rich HTML table (powered by the {gt}package), you can useformat = "html":
| Variable | CI | CI_low | CI_high | p |
|---|---|---|---|---|
| A | ||||
| 1 | 0.95 | 3.35 | 4.23 | 1.00e-03 |
| 3 | 0.95 | 2.42 | 5.31 | 0.05 |
| 5 | 0.95 | 6.21 | 7.12 | 0.45 |
| B | ||||
| 3 | 0.95 | 12.10 | 13.50 | 4.20e-03 |
| 1 | 0.95 | 1.23 | 3.61 | 0.34 |
A special option isformat = "tt", which creates a tableusing the {tinytable} package. This is a very flexible format, as thetable can be rendered to HTML, LaTeX, or other formats depending on thecontext.
| Variable | Group | CI | CI_low | CI_high | p |
|---|---|---|---|---|---|
| 1 | A | 0.95 | 3.35 | 4.23 | 1.00e-03 |
| 3 | A | 0.95 | 2.42 | 5.31 | 0.05 |
| 5 | A | 0.95 | 6.21 | 7.12 | 0.45 |
| 3 | B | 0.95 | 12.10 | 13.50 | 4.20e-03 |
| 1 | B | 0.95 | 1.23 | 3.61 | 0.34 |
Since all arguments are passed toexport_table(), youcan also use additional arguments like theby argument forgrouping.
| Variable | CI | CI_low | CI_high | p |
|---|---|---|---|---|
| A | A | A | A | A |
| 1 | 0.95 | 3.35 | 4.23 | 1.00e-03 |
| 3 | 0.95 | 2.42 | 5.31 | 0.05 |
| 5 | 0.95 | 6.21 | 7.12 | 0.45 |
| B | B | B | B | B |
| 3 | 0.95 | 12.10 | 13.50 | 4.20e-03 |
| 1 | 0.95 | 1.23 | 3.61 | 0.34 |