Last active
December 7, 2020 23:28
-
-
Save kylebutts/c2431663a77a1608ca17124442b4964a to your computer and use it in GitHub Desktop.
Extract body from any string with \midule ... \bottomrule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extract_body <- function(gt) { | |
| if(inherits(gt, "gt_tbl")) gt <- as.character(gt::as_latex(gt)) | |
| stringr::str_match(gt, "(?s)\\\\midrule\\n(.*)\\\\bottomrule")[[2]] | |
| } | |
| ## GT Tables ------------------------------------------------------------------- | |
| library(tidyverse, warn.conflicts = FALSE) | |
| library(gt, warn.conflicts = FALSE) | |
| # Latex Body | |
| head(mtcars) %>% | |
| gt() %>% | |
| extract_body() %>% | |
| cat() | |
| ## Regression Tables ----------------------------------------------------------- | |
| df <- tibble( | |
| treat = 1:20 > 10, | |
| z = runif(20, -1, 1), | |
| y = 5 * treat + 3 * z + 2 + rnorm(20, 0, 1^2) | |
| ) | |
| m1 <- lm(y ~ treat, data = df) | |
| m2 <- lm(y ~ treat + z, data = df) | |
| texreg::texreg( | |
| list(m1, m2), | |
| booktabs = TRUE, | |
| custom.coef.names = c("Intercept", "Treated", "Covariate"), | |
| include.rsquared = F | |
| ) %>% | |
| extract_body() %>% | |
| cat() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created on 2020-12-07 by the reprex package (v0.3.0)