From a8204da2bdb778a13639a20fc91e5bc00d9b38c3 Mon Sep 17 00:00:00 2001 From: Julius Krumbiegel Date: Thu, 24 Oct 2024 13:59:42 +0200 Subject: [PATCH 1/5] add single-arg version of `table_one` --- src/table_one.jl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/table_one.jl b/src/table_one.jl index 5021a88..b86b943 100644 --- a/src/table_one.jl +++ b/src/table_one.jl @@ -518,6 +518,19 @@ function table_one( Table(cells, header_offset-1, nothing; celltable_kws...) end +""" + table_one(table; kwargs...) + +Create a `table_one` with with all columns from `table` except those used in the `groupby` keyword. +""" +function table_one(table; groupby = [], kwargs...) + groups = make_groups(groupby) + groupsyms = [g.symbol for g in groups] + all_names = Tables.columnnames(table) + all_names_but_groups = setdiff(all_names, groupsyms) + return table_one(table, all_names_but_groups; groupby, kwargs...) +end + tableone_column_header() = CellStyle(halign = :center, bold = true) tableone_column_header_spanned() = CellStyle(halign = :center, bold = true, border_bottom = true) tableone_column_header_key() = CellStyle(; halign = :center) From 6d0a0108c116dc1bf5da6f9e0f0bd1377ff6480a Mon Sep 17 00:00:00 2001 From: Julius Krumbiegel Date: Thu, 24 Oct 2024 13:59:48 +0200 Subject: [PATCH 2/5] add tests --- test/references/table_one/single_arg.docx.txt | 951 +++++++++++ .../references/table_one/single_arg.latex.txt | 34 + test/references/table_one/single_arg.txt | 101 ++ test/references/table_one/single_arg.typ.txt | 45 + .../single_arg_with_groupby.docx.txt | 1498 +++++++++++++++++ .../single_arg_with_groupby.latex.txt | 33 + .../table_one/single_arg_with_groupby.txt | 122 ++ .../table_one/single_arg_with_groupby.typ.txt | 71 + test/runtests.jl | 6 + 9 files changed, 2861 insertions(+) create mode 100644 test/references/table_one/single_arg.docx.txt create mode 100644 test/references/table_one/single_arg.latex.txt create mode 100644 test/references/table_one/single_arg.txt create mode 100644 test/references/table_one/single_arg.typ.txt create mode 100644 test/references/table_one/single_arg_with_groupby.docx.txt create mode 100644 test/references/table_one/single_arg_with_groupby.latex.txt create mode 100644 test/references/table_one/single_arg_with_groupby.txt create mode 100644 test/references/table_one/single_arg_with_groupby.typ.txt diff --git a/test/references/table_one/single_arg.docx.txt b/test/references/table_one/single_arg.docx.txt new file mode 100644 index 0000000..c523281 --- /dev/null +++ b/test/references/table_one/single_arg.docx.txt @@ -0,0 +1,951 @@ +############################## [Content_Types].xml ############################## + + + + + + + + + +############################## _rels/.rels ############################## + + + + +############################## word/_rels/document.xml.rels ############################## + + + + +############################## word/styles.xml ############################## + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +############################## word/document.xml ############################## + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Total + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + value1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Mean (SD) + + + + + + + + + + + + + + + 4.5 + + + + ( + + + + 2.45 + + + + ) + + + + + + + + + + + + + + + + + + + + Median [Min, Max] + + + + + + + + + + + + + + + 4.5 + + + + [ + + + + 1 + + + + , + + + + 8 + + + + ] + + + + + + + + + + + + + + + + + + + + value2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + a + + + + + + + + + + + + + + + 3 + + + + ( + + + + 37.5 + + + + %) + + + + + + + + + + + + + + + + + + + + b + + + + + + + + + + + + + + + 3 + + + + ( + + + + 37.5 + + + + %) + + + + + + + + + + + + + + + + + + + + c + + + + + + + + + + + + + + + 2 + + + + ( + + + + 25 + + + + %) + + + + + + + + + + + + + + + + + + + + group1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + a + + + + + + + + + + + + + + + 4 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + + + + + + b + + + + + + + + + + + + + + + 4 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + + + + + + group3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + c + + + + + + + + + + + + + + + 4 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + + + + + + d + + + + + + + + + + + + + + + 4 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + + + + + + group2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + e + + + + + + + + + + + + + + + 4 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + + + + + + f + + + + + + + + + + + + + + + 4 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/references/table_one/single_arg.latex.txt b/test/references/table_one/single_arg.latex.txt new file mode 100644 index 0000000..110d739 --- /dev/null +++ b/test/references/table_one/single_arg.latex.txt @@ -0,0 +1,34 @@ +\documentclass{article} +\usepackage{threeparttable} +\usepackage{multirow} +\usepackage{booktabs} +\begin{document} +\begin{table}[!ht] +\setlength\tabcolsep{0pt} +\centering +\begin{threeparttable} +\begin{tabular}{@{\extracolsep{2ex}}*{2}{lc}} +\toprule + & \textbf{Total} \\ +\midrule +\textbf{value1} & \\ +\hspace{12.0pt}Mean (SD) & 4.5 (2.45) \\ +\hspace{12.0pt}Median [Min, Max] & 4.5 [1, 8] \\ +\textbf{value2} & \\ +\hspace{12.0pt}a & 3 (37.5\%) \\ +\hspace{12.0pt}b & 3 (37.5\%) \\ +\hspace{12.0pt}c & 2 (25\%) \\ +\textbf{group1} & \\ +\hspace{12.0pt}a & 4 (50\%) \\ +\hspace{12.0pt}b & 4 (50\%) \\ +\textbf{group3} & \\ +\hspace{12.0pt}c & 4 (50\%) \\ +\hspace{12.0pt}d & 4 (50\%) \\ +\textbf{group2} & \\ +\hspace{12.0pt}e & 4 (50\%) \\ +\hspace{12.0pt}f & 4 (50\%) \\ +\bottomrule +\end{tabular} +\end{threeparttable} +\end{table} +\end{document} \ No newline at end of file diff --git a/test/references/table_one/single_arg.txt b/test/references/table_one/single_arg.txt new file mode 100644 index 0000000..3827ed6 --- /dev/null +++ b/test/references/table_one/single_arg.txt @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Total
value1
Mean (SD)4.5 (2.45)
Median [Min, Max]4.5 [1, 8]
value2
a3 (37.5%)
b3 (37.5%)
c2 (25%)
group1
a4 (50%)
b4 (50%)
group3
c4 (50%)
d4 (50%)
group2
e4 (50%)
f4 (50%)
\ No newline at end of file diff --git a/test/references/table_one/single_arg.typ.txt b/test/references/table_one/single_arg.typ.txt new file mode 100644 index 0000000..6a1bf04 --- /dev/null +++ b/test/references/table_one/single_arg.typ.txt @@ -0,0 +1,45 @@ + +#table( + rows: 17, + columns: 2, + column-gutter: 0.25em, + align: (left, center), + stroke: none, + table.hline(y: 0, stroke: 1pt), + [], + [*Total*], + table.hline(y: 1, stroke: 0.75pt), + [*value1*], + [], + [#h(12.0pt)Mean (SD)], + [4.5 (2.45)], + [#h(12.0pt)Median [Min, Max]], + [4.5 [1, 8]], + [*value2*], + [], + [#h(12.0pt)a], + [3 (37.5%)], + [#h(12.0pt)b], + [3 (37.5%)], + [#h(12.0pt)c], + [2 (25%)], + [*group1*], + [], + [#h(12.0pt)a], + [4 (50%)], + [#h(12.0pt)b], + [4 (50%)], + [*group3*], + [], + [#h(12.0pt)c], + [4 (50%)], + [#h(12.0pt)d], + [4 (50%)], + [*group2*], + [], + [#h(12.0pt)e], + [4 (50%)], + [#h(12.0pt)f], + [4 (50%)], + table.hline(y: 17, stroke: 1pt), +) diff --git a/test/references/table_one/single_arg_with_groupby.docx.txt b/test/references/table_one/single_arg_with_groupby.docx.txt new file mode 100644 index 0000000..31a435f --- /dev/null +++ b/test/references/table_one/single_arg_with_groupby.docx.txt @@ -0,0 +1,1498 @@ +############################## [Content_Types].xml ############################## + + + + + + + + + +############################## _rels/.rels ############################## + + + + +############################## word/_rels/document.xml.rels ############################## + + + + +############################## word/styles.xml ############################## + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +############################## word/document.xml ############################## + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + group1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Total + + + + + + + + + + + + + + + a + + + + + + + + + + + + + + + b + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + value1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Mean (SD) + + + + + + + + + + + + + + + 4.5 + + + + ( + + + + 2.45 + + + + ) + + + + + + + + + + + + + + + 2.5 + + + + ( + + + + 1.29 + + + + ) + + + + + + + + + + + + + + + 6.5 + + + + ( + + + + 1.29 + + + + ) + + + + + + + + + + + + + + + + + + + + Median [Min, Max] + + + + + + + + + + + + + + + 4.5 + + + + [ + + + + 1 + + + + , + + + + 8 + + + + ] + + + + + + + + + + + + + + + 2.5 + + + + [ + + + + 1 + + + + , + + + + 4 + + + + ] + + + + + + + + + + + + + + + 6.5 + + + + [ + + + + 5 + + + + , + + + + 8 + + + + ] + + + + + + + + + + + + + + + + + + + + value2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + a + + + + + + + + + + + + + + + 3 + + + + ( + + + + 37.5 + + + + %) + + + + + + + + + + + + + + + 2 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + 1 + + + + ( + + + + 25 + + + + %) + + + + + + + + + + + + + + + + + + + + b + + + + + + + + + + + + + + + 3 + + + + ( + + + + 37.5 + + + + %) + + + + + + + + + + + + + + + 1 + + + + ( + + + + 25 + + + + %) + + + + + + + + + + + + + + + 2 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + + + + + + c + + + + + + + + + + + + + + + 2 + + + + ( + + + + 25 + + + + %) + + + + + + + + + + + + + + + 1 + + + + ( + + + + 25 + + + + %) + + + + + + + + + + + + + + + 1 + + + + ( + + + + 25 + + + + %) + + + + + + + + + + + + + + + + + + + + group3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + c + + + + + + + + + + + + + + + 4 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + 2 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + 2 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + + + + + + d + + + + + + + + + + + + + + + 4 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + 2 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + 2 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + + + + + + group2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + e + + + + + + + + + + + + + + + 4 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + 2 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + 2 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + + + + + + f + + + + + + + + + + + + + + + 4 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + 2 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + 2 + + + + ( + + + + 50 + + + + %) + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/references/table_one/single_arg_with_groupby.latex.txt b/test/references/table_one/single_arg_with_groupby.latex.txt new file mode 100644 index 0000000..6da72d6 --- /dev/null +++ b/test/references/table_one/single_arg_with_groupby.latex.txt @@ -0,0 +1,33 @@ +\documentclass{article} +\usepackage{threeparttable} +\usepackage{multirow} +\usepackage{booktabs} +\begin{document} +\begin{table}[!ht] +\setlength\tabcolsep{0pt} +\centering +\begin{threeparttable} +\begin{tabular}{@{\extracolsep{2ex}}*{4}{lccc}} +\toprule + & & \multicolumn{2}{c}{\textbf{group1}} \\ +\cmidrule{3-4} + & \textbf{Total} & a & b \\ +\midrule +\textbf{value1} & & & \\ +\hspace{12.0pt}Mean (SD) & 4.5 (2.45) & 2.5 (1.29) & 6.5 (1.29) \\ +\hspace{12.0pt}Median [Min, Max] & 4.5 [1, 8] & 2.5 [1, 4] & 6.5 [5, 8] \\ +\textbf{value2} & & & \\ +\hspace{12.0pt}a & 3 (37.5\%) & 2 (50\%) & 1 (25\%) \\ +\hspace{12.0pt}b & 3 (37.5\%) & 1 (25\%) & 2 (50\%) \\ +\hspace{12.0pt}c & 2 (25\%) & 1 (25\%) & 1 (25\%) \\ +\textbf{group3} & & & \\ +\hspace{12.0pt}c & 4 (50\%) & 2 (50\%) & 2 (50\%) \\ +\hspace{12.0pt}d & 4 (50\%) & 2 (50\%) & 2 (50\%) \\ +\textbf{group2} & & & \\ +\hspace{12.0pt}e & 4 (50\%) & 2 (50\%) & 2 (50\%) \\ +\hspace{12.0pt}f & 4 (50\%) & 2 (50\%) & 2 (50\%) \\ +\bottomrule +\end{tabular} +\end{threeparttable} +\end{table} +\end{document} \ No newline at end of file diff --git a/test/references/table_one/single_arg_with_groupby.txt b/test/references/table_one/single_arg_with_groupby.txt new file mode 100644 index 0000000..e360a84 --- /dev/null +++ b/test/references/table_one/single_arg_with_groupby.txt @@ -0,0 +1,122 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
group1
Totalab
value1
Mean (SD)4.5 (2.45)2.5 (1.29)6.5 (1.29)
Median [Min, Max]4.5 [1, 8]2.5 [1, 4]6.5 [5, 8]
value2
a3 (37.5%)2 (50%)1 (25%)
b3 (37.5%)1 (25%)2 (50%)
c2 (25%)1 (25%)1 (25%)
group3
c4 (50%)2 (50%)2 (50%)
d4 (50%)2 (50%)2 (50%)
group2
e4 (50%)2 (50%)2 (50%)
f4 (50%)2 (50%)2 (50%)
\ No newline at end of file diff --git a/test/references/table_one/single_arg_with_groupby.typ.txt b/test/references/table_one/single_arg_with_groupby.typ.txt new file mode 100644 index 0000000..6a1d6c9 --- /dev/null +++ b/test/references/table_one/single_arg_with_groupby.typ.txt @@ -0,0 +1,71 @@ + +#table( + rows: 15, + columns: 4, + column-gutter: 0.25em, + align: (left, center, center, center), + stroke: none, + table.hline(y: 0, stroke: 1pt), + [], + [], + table.cell(colspan: 2)[*group1*], + table.hline(y: 1, start: 2, end: 4, stroke: 0.75pt), + [], + [*Total*], + [a], + [b], + table.hline(y: 2, stroke: 0.75pt), + [*value1*], + [], + [], + [], + [#h(12.0pt)Mean (SD)], + [4.5 (2.45)], + [2.5 (1.29)], + [6.5 (1.29)], + [#h(12.0pt)Median [Min, Max]], + [4.5 [1, 8]], + [2.5 [1, 4]], + [6.5 [5, 8]], + [*value2*], + [], + [], + [], + [#h(12.0pt)a], + [3 (37.5%)], + [2 (50%)], + [1 (25%)], + [#h(12.0pt)b], + [3 (37.5%)], + [1 (25%)], + [2 (50%)], + [#h(12.0pt)c], + [2 (25%)], + [1 (25%)], + [1 (25%)], + [*group3*], + [], + [], + [], + [#h(12.0pt)c], + [4 (50%)], + [2 (50%)], + [2 (50%)], + [#h(12.0pt)d], + [4 (50%)], + [2 (50%)], + [2 (50%)], + [*group2*], + [], + [], + [], + [#h(12.0pt)e], + [4 (50%)], + [2 (50%)], + [2 (50%)], + [#h(12.0pt)f], + [4 (50%)], + [2 (50%)], + [2 (50%)], + table.hline(y: 15, stroke: 1pt), +) diff --git a/test/runtests.jl b/test/runtests.jl index 52a7113..084836d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -235,6 +235,12 @@ end ) t = table_one(data, [:value], groupby = [:group1, :group2], group_totals = :group2) reftest(t, "references/table_one/missing_as_a_group_factor") + + t = table_one(df) + reftest(t, "references/table_one/single_arg") + + t = table_one(df, groupby = :group1) + reftest(t, "references/table_one/single_arg_with_groupby") end From e0ab286324479a4d3d373ad4432892efe8af3c36 Mon Sep 17 00:00:00 2001 From: Julius Krumbiegel Date: Thu, 24 Oct 2024 14:00:02 +0200 Subject: [PATCH 3/5] add docs entry --- docs/src/predefined_tables/table_one.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/src/predefined_tables/table_one.md b/docs/src/predefined_tables/table_one.md index 09b2109..9d9298d 100644 --- a/docs/src/predefined_tables/table_one.md +++ b/docs/src/predefined_tables/table_one.md @@ -9,7 +9,7 @@ Optionally, there can be grouping applied along the columns as well. In this example, several variables of a hypothetical population are analyzed split by sex. -```@example +```@example table_one_intro using SummaryTables using DataFrames @@ -28,6 +28,15 @@ table_one( ) ``` +You can also omit the second argument as a shortcut when you quickly want to summarize all columns of your dataset. The columns in `groupby` are excluded automatically: + +```@example table_one_intro +table_one( + data, + groupby = :blood_type, +) +``` + ## Argument 1: `table` The first argument can be any object that is a table compatible with the `Tables.jl` API. @@ -64,12 +73,14 @@ data = [(; x = 1, y = "4"), (; x = 2, y = "5"), (; x = 3, y = "6")] table_one(data, [:x, :y]) ``` -## Argument 2: `analyses` +## Optional argument 2: `analyses` The second argument takes a vector specifying analyses, with one entry for each "row section" of the resulting table. If only one analysis is passed, the vector can be omitted. Each analysis can have up to three parts: the variable, the analysis function and the label. +For convenience, if the `analyses` argument is omitted, it is equivalent to passing `Tables.columnnames(table)` except that all columns referenced in `groupby` are filtered out. + The variable is passed as a `Symbol`, corresponding to a column in the input data, and must always be specified. The other two parts are optional. From fdf37fcdaa92be443a79bd3c6b330a6a7a335dba Mon Sep 17 00:00:00 2001 From: Julius Krumbiegel Date: Thu, 24 Oct 2024 14:01:12 +0200 Subject: [PATCH 4/5] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a07d664..d97a955 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Added single-argument method to `table_one` which summarizes all columns in the passed table except those used for grouping [#48](https://github.com/PumasAI/SummaryTables.jl/pull/48). - Fixed issue with using `missing` as a group in `table_one`. This PR also removes redundant group totals if there's just one subgroup to do a total over [#47](https://github.com/PumasAI/SummaryTables.jl/pull/47). ## 3.0.0 - 2024-09-23 From e7efab84d4581f2341ff7cc269ee0c38770e0dde Mon Sep 17 00:00:00 2001 From: Julius Krumbiegel Date: Thu, 24 Oct 2024 15:54:59 +0200 Subject: [PATCH 5/5] remove a now incorrect test --- test/runtests.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 084836d..5771969 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -153,8 +153,6 @@ end reftest(t, path) = @testset "$path" run_reftest(t, path, func) @testset "table_one" begin - @test_throws MethodError table_one(df) - t = table_one(df, [:value1]) reftest(t, "references/table_one/one_row")