Movatterモバイル変換


[0]ホーム

URL:


Formatting, printing and exportingtables

The difference between a dataframe and its render

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.3400

When 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:

knitr::kable(df,format ="markdown")
| 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):

knitr::kable(df,format ="html")
VariableGroupCICI_lowCI_highp
1A0.953.3504.2300.0010
3A0.952.4255.3100.0456
5A0.956.2137.1230.4500
3B0.9512.10013.5000.0042
1B0.951.2303.6100.3400

Theinsight workflow

The {insight} package also contains function to improve the“printing”, or rendering, of tables. Its design dissociates two separateand independent steps:formatting andexporting.

Formatting

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.340

As 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():

library(datawizard)# for data_modify()df|>data_modify(p =format_p(p,stars =TRUE))|>format_table()#>   Variable Group         95% CI           p#> 1        1     A [ 3.35,  4.23] p = 0.001**#> 2        3     A [ 2.42,  5.31] p = 0.046*#> 3        5     A [ 6.21,  7.12] p = 0.450#> 4        3     B [12.10, 13.50] p = 0.004**#> 5        1     B [ 1.23,  3.61] p = 0.340

Using unicode symbols as effect size names

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.40

In 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.40

Exporting

The 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.34

For markdown or HTML, simply change theformat argumentto markdown (“md”)…

export_table(df,format ="md")
VariableGroupCICI_lowCI_highp
1A0.953.354.231.00e-03
3A0.952.425.310.05
5A0.956.217.120.45
3B0.9512.1013.504.20e-03
1B0.951.233.610.34

…or HTML format.

export_table(df,format ="html")
VariableCICI_lowCI_highp
A
10.953.354.231.00e-03
30.952.425.310.05
50.956.217.120.45
B
30.9512.1013.504.20e-03
10.951.233.610.34

This can be combined withformat_table().

df|>format_table(ci_brackets =c("(",")"))|>export_table(format ="html")
Variable95% CIp
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

Displaying tables withdisplay()

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:

# By default, display() creates a markdown tabledisplay(df)
VariableGroupCICI_lowCI_highp
1A0.953.354.231.00e-03
3A0.952.425.310.05
5A0.956.217.120.45
3B0.9512.1013.504.20e-03
1B0.951.233.610.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":

display(df,format ="html")
VariableCICI_lowCI_highp
A
10.953.354.231.00e-03
30.952.425.310.05
50.956.217.120.45
B
30.9512.1013.504.20e-03
10.951.233.610.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.

display(df,format ="tt")
VariableGroupCICI_lowCI_highp
1A0.953.354.231.00e-03
3A0.952.425.310.05
5A0.956.217.120.45
3B0.9512.1013.504.20e-03
1B0.951.233.610.34

Since all arguments are passed toexport_table(), youcan also use additional arguments like theby argument forgrouping.

display(df,format ="tt",by ="Group")
VariableCICI_lowCI_highp
AAAAA
10.953.354.231.00e-03
30.952.425.310.05
50.956.217.120.45
BBBBB
30.9512.1013.504.20e-03
10.951.233.610.34

[8]ページ先頭

©2009-2025 Movatter.jp