Reputation: 313
So I guess there are multiple ways of doing this but they are all giving me headache. Let's say I have produced the latex code for a table using.
library(modelsummary)
library(tidyverse)
a <- lm(mpg ~ disp, data = mtcars)
b <- lm(mpg ~ cyl, data = mtcars)
table <- modelsummary(list(a,b), output = "latex")
table
Which yields
\begin{table}
\centering
\begin{tabular}[t]{lcc}
\toprule
& (1) & (2)\\
\midrule
(Intercept) & \num{29.600} & \num{37.885}\\
& (\num{1.230}) & (\num{2.074})\\
disp & \num{-0.041} & \\
& (\num{0.005}) & \\
cyl & & \num{-2.876}\\
& & (\num{0.322})\\
\midrule
Num.Obs. & \num{32} & \num{32}\\
R2 & \num{0.718} & \num{0.726}\\
R2 Adj. & \num{0.709} & \num{0.717}\\
AIC & \num{170.2} & \num{169.3}\\
BIC & \num{174.6} & \num{173.7}\\
Log.Lik. & \num{-82.105} & \num{-81.653}\\
F & \num{76.513} & \num{79.561}\\
RMSE & \num{3.15} & \num{3.10}\\
\bottomrule
\end{tabular}
\end{table}
The output I can easily copy into my .tex-document and compile. However, let's say I want to manually modify something in the table. I thouht I could do this by splitting the character vector and remove/add/change the rows and then convert it back into a character vector.
table <- str_split(table, "\n")[1][[1]]
# Remove two rows
table <- table[-c(11,12)]
table <- paste0(table, collapse = "\n")
table
The output then becomes:
[1] "\\begin{table}\\n\\centering\\n\\begin{tabular}[t]{lcc}\\n\\toprule\\n & (1) & (2)\\\\\\n\\midrule\\n(Intercept) & \\num{29.600} & \\num{37.885}\\\\\\n & (\\num{1.230}) & (\\num{2.074})\\\\\\ndisp & \\num{-0.041} & \\\\\\n & (\\num{0.005}) & \\\\\\n\\midrule\\nNum.Obs. & \\num{32} & \\num{32}\\\\\\nR2 & \\num{0.718} & \\num{0.726}\\\\\\nR2 Adj. & \\num{0.709} & \\num{0.717}\\\\\\nAIC & \\num{170.2} & \\num{169.3}\\\\\\nBIC & \\num{174.6} & \\num{173.7}\\\\\\nLog.Lik. & \\num{-82.105} & \\num{-81.653}\\\\\\nF & \\num{76.513} & \\num{79.561}\\\\\\nRMSE & \\num{3.15} & \\num{3.10}\\\\\\n\\bottomrule\\n\\end{tabular}\\n\\end{table}"
Which is obviously quite useless. What to do?
Upvotes: 0
Views: 165