I'm trying to produce tables in R (not RMarkdown, as this question helps answer) with modelsummary
and I'm having a tough time adding reference labels (e.g., tab:hello
). As Vincent points out in this answer, extra arguments to modelsummary
should be pushed forward automatically, but it seems this does not work with LaTeX labels. Here's an example:
library(modelsummary)library(kableExtra)x <- rnorm(100)y <- rnorm(100)modelsummary(lm(y ~ x), output = "latex", caption = "test", label = "hello")
This produces:
\begin{table}\caption{test}\centering\begin{tabular}[t]{lc}\toprule& Model 1\\\midrule(Intercept) & \num{-0.143}\\& (\num{0.100})\\x & \num{-0.023}\\& (\num{0.092})\\\midruleNum.Obs. & \num{100}\\R2 & \num{0.001}\\R2 Adj. & \num{-0.010}\\AIC & \num{283.4}\\BIC & \num{291.2}\\Log.Lik. & \num{-138.698}\\F & \num{0.061}\\\bottomrule\end{tabular}\end{table}
Meanwhile, this works fine with a call to kable
:
df <- cbind.data.frame(x, y)kable(head(df), "latex", caption = "test", label = "hello")
Which produces:
\begin{table}\caption{\label{tab:hello}test}\centering\begin{tabular}[t]{r|r}\hlinex & y\\\hline0.8078318 & -0.0219732\\\hline0.4660209 & -0.9973773\\\hline-1.0620694 & -0.1360954\\\hline0.5639881 & 0.0185161\\\hline0.3459854 & 0.1333345\\\hline-0.8035314 & -0.0759982\\\hline\end{tabular}\end{table}
Weirdly, this only works when specifying a caption too. Defining only a label
in kable
doesn't produce a label.
Ideally, I'd like to only produce a label, but if I have to add a caption too, I can deal with that (since it seems like this is an issue with kable
as well---or just with LaTeX tables in general).
Thank you in advance for the help! It's much appreciated.