Skip to content

Commit

Permalink
Merge pull request #47 from PumasAI/jk/fix-missing-as-group
Browse files Browse the repository at this point in the history
Fix `missing` as group in `table_one`
jkrumbiegel authored Oct 24, 2024
2 parents a6d5cca + 6cc6d62 commit 4135532
Showing 12 changed files with 2,238 additions and 988 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/readme.yml
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ jobs:
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- uses: julia-actions/setup-julia@a0a0978e28861c11a3490cee468f87d5b568851a # v2.5.0
with:
version: '1'
version: '1.10'
- uses: julia-actions/cache@824243901fb567ccb490b0d0e2483ccecde46834 # v2.0.5
- uses: quarto-dev/quarto-actions/setup@87b35bb88b36317fa36b5189e9553b4164a5c5a3 # v2.1.6
with:
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

## Unreleased

- 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

- **Breaking** Footnotes are by default separated with linebreaks now. This can be changed by setting the new `Table` option `linebreak_footnotes = false` [#34](https://github.com/PumasAI/SummaryTables.jl/pull/34).
744 changes: 371 additions & 373 deletions README_files/figure-commonmark/cell-3-output-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
684 changes: 341 additions & 343 deletions README_files/figure-commonmark/cell-4-output-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
536 changes: 267 additions & 269 deletions README_files/figure-commonmark/cell-5-output-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/helpers.jl
Original file line number Diff line number Diff line change
@@ -79,7 +79,7 @@ function auto_round(number; target_digits::Int)
end

natural_lt(x::AbstractString, y::AbstractString) = NaturalSort.natural(x, y)
natural_lt(x, y) = x < y
natural_lt(x, y) = isless(x, y)

function validate_rowgaps(rowgaps, nrows)
nrows == 1 && !isempty(rowgaps) && error("No row gaps allowed for a table with one row.")
9 changes: 8 additions & 1 deletion src/table_one.jl
Original file line number Diff line number Diff line change
@@ -431,7 +431,14 @@ function table_one(
i_total_group = group_total_indices[ii]
i_parent_group = i_total_group - 1
next_key = length(df_analyses) == ikey ? nothing : keys(df_analyses)[ikey+1]
if next_key === nothing || key[i_parent_group] != next_key[i_parent_group] || ikey == length(df_analyses)

n_subgroups = count(keys(df_analyses)) do _key
Tuple(_key)[1:i_parent_group] === Tuple(key)[1:i_parent_group]
end
# if a group has only 1 subgroup, the total would be the same as that one so we skip it
n_subgroups == 1 && continue

if next_key === nothing || key[i_parent_group] !== next_key[i_parent_group] || ikey == length(df_analyses)
group_total_col = Cell[]

for i in 1:i_total_group
1,069 changes: 1,069 additions & 0 deletions test/references/table_one/missing_as_a_group_factor.docx.txt

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions test/references/table_one/missing_as_a_group_factor.latex.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
\documentclass{article}
\usepackage{threeparttable}
\usepackage{multirow}
\usepackage{booktabs}
\begin{document}
\begin{table}[!ht]
\setlength\tabcolsep{0pt}
\centering
\begin{threeparttable}
\begin{tabular}{@{\extracolsep{2ex}}*{7}{lcccccc}}
\toprule
& & \multicolumn{5}{c}{\textbf{group1}} \\
\cmidrule{3-7}
& & A & \multicolumn{3}{c}{B} & missing \\
& & \textbf{group2} & \multicolumn{3}{c}{\textbf{group2}} & \textbf{group2} \\
\cmidrule{3-3}\cmidrule{4-6}\cmidrule{7-7}
& \textbf{Total} & D & D & missing & Total & missing \\
\midrule
\textbf{value} & & & & & & \\
\hspace{12.0pt}Mean (SD) & 3.5 (1.87) & 2 (1.41) & 4 (NaN) & 5 (NaN) & 4.5 (0.707) & 4 (2.83) \\
\hspace{12.0pt}Median [Min, Max] & 3.5 [1, 6] & 2 [1, 3] & 4 [4, 4] & 5 [5, 5] & 4.5 [4, 5] & 4 [2, 6] \\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{table}
\end{document}
88 changes: 88 additions & 0 deletions test/references/table_one/missing_as_a_group_factor.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<table class="st-4edf7071">
<style>
.st-4edf7071 {
border: none;
margin: 0 auto;
padding: 0.25rem;
border-collapse: separate;
border-spacing: 0.85em 0.2em;
line-height: 1.2em;
}

.st-4edf7071 tr td {
vertical-align: top;
padding: 0;
border: none;
}

.st-4edf7071 br {
line-height: 0em;
margin: 0;
}

.st-4edf7071 sub {
line-height: 0;
}

.st-4edf7071 sup {
line-height: 0;
}
</style>
<tr><td colspan="7" style="border-bottom: 1.5px solid black; padding: 0"></td></tr>
<tr>
<td style="text-align:center;"></td>
<td style="text-align:center;"></td>
<td colspan="5" style="font-weight:bold;border-bottom:1px solid black; padding-bottom: 0.25em;text-align:center;">group1</td>
</tr>
<tr>
<td style="text-align:center;"></td>
<td style="text-align:center;"></td>
<td style="text-align:center;">A</td>
<td colspan="3" style="text-align:center;">B</td>
<td style="text-align:center;">missing</td>
</tr>
<tr>
<td style="text-align:center;"></td>
<td style="text-align:center;"></td>
<td style="font-weight:bold;border-bottom:1px solid black; padding-bottom: 0.25em;text-align:center;">group2</td>
<td colspan="3" style="font-weight:bold;border-bottom:1px solid black; padding-bottom: 0.25em;text-align:center;">group2</td>
<td style="font-weight:bold;border-bottom:1px solid black; padding-bottom: 0.25em;text-align:center;">group2</td>
</tr>
<tr>
<td style="text-align:center;"></td>
<td style="font-weight:bold;text-align:center;">Total</td>
<td style="text-align:center;">D</td>
<td style="text-align:center;">D</td>
<td style="text-align:center;">missing</td>
<td style="text-align:center;">Total</td>
<td style="text-align:center;">missing</td>
</tr>
<tr><td colspan="7" style="border-bottom:1px solid black;padding:0"></td></tr> <tr>
<td style="font-weight:bold;text-align:left;">value</td>
<td style="text-align:center;"></td>
<td style="text-align:center;"></td>
<td style="text-align:center;"></td>
<td style="text-align:center;"></td>
<td style="text-align:center;"></td>
<td style="text-align:center;"></td>
</tr>
<tr>
<td style="padding-left:12.0pt;text-align:left;">Mean (SD)</td>
<td style="text-align:center;">3.5 (1.87)</td>
<td style="text-align:center;">2 (1.41)</td>
<td style="text-align:center;">4 (NaN)</td>
<td style="text-align:center;">5 (NaN)</td>
<td style="text-align:center;">4.5 (0.707)</td>
<td style="text-align:center;">4 (2.83)</td>
</tr>
<tr>
<td style="padding-left:12.0pt;text-align:left;">Median [Min, Max]</td>
<td style="text-align:center;">3.5 [1, 6]</td>
<td style="text-align:center;">2 [1, 3]</td>
<td style="text-align:center;">4 [4, 4]</td>
<td style="text-align:center;">5 [5, 5]</td>
<td style="text-align:center;">4.5 [4, 5]</td>
<td style="text-align:center;">4 [2, 6]</td>
</tr>
<tr><td colspan="7" style="border-bottom: 1.5px solid black; padding: 0"></td></tr>
</table>
56 changes: 56 additions & 0 deletions test/references/table_one/missing_as_a_group_factor.typ.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

#table(
rows: 7,
columns: 7,
column-gutter: 0.25em,
align: (left, center, center, center, center, center, center),
stroke: none,
table.hline(y: 0, stroke: 1pt),
[],
[],
table.cell(colspan: 5)[*group1*],
table.hline(y: 1, start: 2, end: 7, stroke: 0.75pt),
[],
[],
[A],
table.cell(colspan: 3)[B],
[missing],
[],
[],
[*group2*],
table.hline(y: 3, start: 2, end: 3, stroke: 0.75pt),
table.cell(colspan: 3)[*group2*],
table.hline(y: 3, start: 3, end: 6, stroke: 0.75pt),
[*group2*],
table.hline(y: 3, start: 6, end: 7, stroke: 0.75pt),
[],
[*Total*],
[D],
[D],
[missing],
[Total],
[missing],
table.hline(y: 4, stroke: 0.75pt),
[*value*],
[],
[],
[],
[],
[],
[],
[#h(12.0pt)Mean (SD)],
[3.5 (1.87)],
[2 (1.41)],
[4 (NaN)],
[5 (NaN)],
[4.5 (0.707)],
[4 (2.83)],
[#h(12.0pt)Median [Min, Max]],
[3.5 [1, 6]],
[2 [1, 3]],
[4 [4, 4]],
[5 [5, 5]],
[4.5 [4, 5]],
[4 [2, 6]],
table.hline(y: 7, stroke: 1pt),
)
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -227,6 +227,14 @@ end
)
t = table_one(data, [:category], groupby = :group)
reftest(t, "references/table_one/category_with_missing")

data = (;
value = 1:6,
group1 = ["A", missing, "A", "B", "B", missing],
group2 = ["D", missing, "D", "D", missing, missing],
)
t = table_one(data, [:value], groupby = [:group1, :group2], group_totals = :group2)
reftest(t, "references/table_one/missing_as_a_group_factor")
end


0 comments on commit 4135532

Please sign in to comment.