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
datasummary_balance
does not use siunitx
#399
Comments
This is still an issue on In relation to this, the documentation for modelsummary/man/datasummary_balance.Rd Lines 183 to 185 in 2499b55
|
Ah yeah, that would be great. I'm sure the solution only takes a few lines of code, but finding the right spot in the logic might (or might not) be tricky. If you look in the main function, there are calls to datasummary(output="dataframe"). If you dig, you'll find that the numeric formatter disables siuntix wrapping when the output is a dataframe. This is important, usually, but in this case it bites us. What we need is to find a way to disable this check, and let datasummary() format with \num{} in the special case where the original calling function is datasummary_balance() |
I see, thanks for the details. I'll try to work on it this weekend and see if I can come up with a solution. |
If this is an approach that you think would work, maybe it can be implemented fairly quickly using something like foo <- function() {
caller <- sys.call(-1) # sys.call(-1) returns immediate caller
return(caller)
}
bar <- function() {
foo()
} Here, running r$> foo()
NULL
r$> bar()
bar() I am not familiar with this codebase, but by taking a quick look, I suppose that the "numeric formatter" that you mention is in |
Hmm, maybe this would work. But there's also a package-specific Here's a start of a pull request with a simple change that seems to work. Need to check why some tests are failing and test various cases, but that's a start... |
Alright, I think I’ve got this working now. It requires the development versions of both `tinytable: remotes::install_github("vincentarelbundock/tinytable")
remotes::install_github("vincentarelbundock/modelsummary") This ended up being one of those things that fell into a rather arcane corner of the package, so it was easier to fix myself. That said, I really appreciate your offer to help! And I really don’t want my taking over the issue to discourage you from making contributions if you run into other issues that you might care about. I tagged a couple issues as “good first” if you or anyone else want to try something. https://github.com/vincentarelbundock/modelsummary/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22 No pressure, obviously! library(modelsummary)
dat <- transform(
mtcars,
cyl = factor(cyl),
gear = factor(gear)
)
datasummary_balance(~am, data = dat, stars = TRUE, output = "latex") \begin{table}
\centering
\begin{tblr}[ %% tabularray outer open
] %% tabularray outer close
{ %% tabularray inner open
colspec={Q[]Q[]Q[]Q[]Q[]Q[]Q[]Q[]},
cell{1}{3}={c=2,}{halign=c,},
cell{1}{5}={c=2,}{halign=c,},
column{1}={halign=l,},
column{2}={halign=l,},
column{3}={halign=r,},
column{4}={halign=r,},
column{5}={halign=r,},
column{6}={halign=r,},
column{7}={halign=r,},
column{8}={halign=r,},
row{1}={halign=c,},
hline{11}={1,2,3,4,5,6,7,8}{solid, 0.05em, black},
} %% tabularray inner close
\toprule
& & 0 & & 1 & & & \\ \cmidrule[lr]{3-4}\cmidrule[lr]{5-6}
& & Mean & Std. Dev. & Mean & Std. Dev. & Diff. in Means & Std. Error \\ \midrule %% TinyTableHeader
mpg & & \num{17.1} & \num{3.8} & \num{24.4} & \num{6.2} & \num{7.2}** & \num{1.9} \\
disp & & \num{290.4} & \num{110.2} & \num{143.5} & \num{87.2} & \num{-146.8}*** & \num{35.0} \\
hp & & \num{160.3} & \num{53.9} & \num{126.8} & \num{84.1} & \num{-33.4} & \num{26.4} \\
drat & & \num{3.3} & \num{0.4} & \num{4.0} & \num{0.4} & \num{0.8}*** & \num{0.1} \\
wt & & \num{3.8} & \num{0.8} & \num{2.4} & \num{0.6} & \num{-1.4}*** & \num{0.2} \\
qsec & & \num{18.2} & \num{1.8} & \num{17.4} & \num{1.8} & \num{-0.8} & \num{0.6} \\
vs & & \num{0.4} & \num{0.5} & \num{0.5} & \num{0.5} & \num{0.2} & \num{0.2} \\
carb & & \num{2.7} & \num{1.1} & \num{2.9} & \num{2.2} & \num{0.2} & \num{0.7} \\
& & N & Pct. & N & Pct. & & \\
cyl & 4 & \num{3} & \num{15.8} & \num{8} & \num{61.5} & & \\
& 6 & \num{4} & \num{21.1} & \num{3} & \num{23.1} & & \\
& 8 & \num{12} & \num{63.2} & \num{2} & \num{15.4} & & \\
gear & 3 & \num{15} & \num{78.9} & \num{0} & \num{0.0} & & \\
& 4 & \num{4} & \num{21.1} & \num{8} & \num{61.5} & & \\
& 5 & \num{0} & \num{0.0} & \num{5} & \num{38.5} & & \\
\bottomrule
\end{tblr}
\end{table} |
Awesome! Thank you for the fix.
Not discouraged at all, I appreciate it that you took the time do it. I'll keep my eyes peeled! |
Problem is that the final table is a combination of several tables produced by
output="data.frame"
, so the rounding is done in a pre-processing stage, withoutsiunitx
.The text was updated successfully, but these errors were encountered: