最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

latex - Header rows in tinytable in R without horizontal lines - Stack Overflow

programmeradmin0浏览0评论

I would like to make a regression table in R using modelsummary and outputting to latex. My understanding is that this is currently best done using tinytable functions rather than kableExtra. (See this post.)

Here is the code that I am using. I have also included what the function currently outputs, and what I would like it to look like.

Code:

library(modelsummary)
library(magrittr)
library(tinytable) 

lm1 <- lm(data = mtcars, formula = mpg ~ gear)
lm2 <- lm(data = mtcars, formula = mpg ~ gear + vs)
lm3 <- lm(data = mtcars, formula = mpg ~ gear + vs + disp)
lm4 <- lm(data = mtcars, formula = mpg ~ gear + vs + drat)

modelsummary(
  list(`Model 1` = lm1, # Doctor Coversation
       `Model 2` = lm2, # Specific Information
       `Model 3` = lm3),
  gof_omit = c("AIC|BIC|RMSE|Adj"),
  coef_omit = "covariate",
  stars = c('*' = 0.05),
  title = "Example Table",
  # output = "kableExtra",
  # output = "output/information.tex"
) %>% 
  group_tt(j = list("First" = 2,
                    "Second" = 3,
                    "Third" = 4)) %>% 
  group_tt(j = list("Begin" = 2,  
                    "Middle" = 3, 
                    "End" = 4)) %>% 
  group_tt(j = list("Model A" = 2,
                    "Model B" = 3,
                    "Model C" = 4)) %>% 
  theme_tt("tabular") %>% 
  print("latex")

Current Output (Latex)

Desired Output (Latex)

I would like to make a regression table in R using modelsummary and outputting to latex. My understanding is that this is currently best done using tinytable functions rather than kableExtra. (See this post.)

Here is the code that I am using. I have also included what the function currently outputs, and what I would like it to look like.

Code:

library(modelsummary)
library(magrittr)
library(tinytable) 

lm1 <- lm(data = mtcars, formula = mpg ~ gear)
lm2 <- lm(data = mtcars, formula = mpg ~ gear + vs)
lm3 <- lm(data = mtcars, formula = mpg ~ gear + vs + disp)
lm4 <- lm(data = mtcars, formula = mpg ~ gear + vs + drat)

modelsummary(
  list(`Model 1` = lm1, # Doctor Coversation
       `Model 2` = lm2, # Specific Information
       `Model 3` = lm3),
  gof_omit = c("AIC|BIC|RMSE|Adj"),
  coef_omit = "covariate",
  stars = c('*' = 0.05),
  title = "Example Table",
  # output = "kableExtra",
  # output = "output/information.tex"
) %>% 
  group_tt(j = list("First" = 2,
                    "Second" = 3,
                    "Third" = 4)) %>% 
  group_tt(j = list("Begin" = 2,  
                    "Middle" = 3, 
                    "End" = 4)) %>% 
  group_tt(j = list("Model A" = 2,
                    "Model B" = 3,
                    "Model C" = 4)) %>% 
  theme_tt("tabular") %>% 
  print("latex")

Current Output (Latex)

Desired Output (Latex)

Share Improve this question asked Feb 3 at 21:30 Ryan Baxter-KingRyan Baxter-King 3511 gold badge3 silver badges12 bronze badges 1
  • 3 looks to be hard coded in you could capture the latex code and gsub out the cmidrule part – rawr Commented Feb 4 at 2:07
Add a comment  | 

1 Answer 1

Reset to default 0

I tried to do this by using a custom theme as described here on p. 51. But it failed. It seems like rawr's suggestion is the only thing that works:

library(modelsummary)
library(magrittr)
library(tinytable) 

lm1 <- lm(data = mtcars, formula = mpg ~ gear)
lm2 <- lm(data = mtcars, formula = mpg ~ gear + vs)
lm3 <- lm(data = mtcars, formula = mpg ~ gear + vs + disp)
lm4 <- lm(data = mtcars, formula = mpg ~ gear + vs + drat)


output <- modelsummary(
  list(`Model 1` = lm1, # Doctor Coversation
       `Model 2` = lm2, # Specific Information
       `Model 3` = lm3),
  gof_omit = c("AIC|BIC|RMSE|Adj"),
  coef_omit = "covariate",
  stars = c('*' = 0.05),
  title = "Example Table",
  # output = "kableExtra",
  # output = "output/information.tex"
) %>% 
  group_tt(j = list("First" = 2,
                    "Second" = 3,
                    "Third" = 4)) %>% 
  group_tt(j = list("Begin" = 2,  
                    "Middle" = 3, 
                    "End" = 4)) %>% 
  group_tt(j = list("Model A" = 2,
                    "Model B" = 3,
                    "Model C" = 4)) %>% 
  theme_tt("tabular") %>% 
  print("latex")

cat(gsub("\\\\cmidrule(\\(lr\\)|\\([lr]\\)|)\\{.*?\\}", "", slot(output, "table_string")))

# Save only modified Table-LaTeX to file
writeLines(gsub("\\\\cmidrule(\\(lr\\)|\\([lr]\\)|)\\{.*?\\}", "", slot(output, "table_string")), "table_clean.tex")


# Save as full doc

latex_document <- paste(
  "\\documentclass{article}",
  "\\usepackage{booktabs}",  # Ensures proper formatting of tables
  "\\begin{document}",
  gsub("\\\\cmidrule(\\(lr\\)|\\([lr]\\)|)\\{.*?\\}", "", slot(output, "table_string")),
  "\\end{document}",
  sep = "\n"
)

writeLines(latex_document, "table_doc.tex")
发布评论

评论列表(0)

  1. 暂无评论