Skip to content

Instantly share code, notes, and snippets.

@kylebutts
Last active December 7, 2020 23:28
Show Gist options
  • Select an option

  • Save kylebutts/c2431663a77a1608ca17124442b4964a to your computer and use it in GitHub Desktop.

Select an option

Save kylebutts/c2431663a77a1608ca17124442b4964a to your computer and use it in GitHub Desktop.
Extract body from any string with \midule ... \bottomrule
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()
@kylebutts

Copy link
Copy Markdown
Author
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)
#> Warning: replacing previous import 'vctrs::data_frame' by 'tibble::data_frame'
#> when loading 'dplyr'
library(gt, warn.conflicts = FALSE)

# Latex Body
head(mtcars) %>%
    gt() %>% 
    extract_body() %>% 
    cat()
#> 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4 \\ 
#> 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\ 
#> 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1 \\ 
#> 21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1 \\ 
#> 18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2 \\ 
#> 18.1 & 6 & 225 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1 \\


## 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()
#> Intercept  & $1.35^{*}$   & $2.10^{***}$ \\
#>            & $(0.56)$     & $(0.34)$     \\
#> Treated    & $5.79^{***}$ & $4.79^{***}$ \\
#>            & $(0.79)$     & $(0.48)$     \\
#> Covariate  &              & $2.66^{***}$ \\
#>            &              & $(0.43)$     \\
#> \midrule
#> Adj. R$^2$ & $0.73$       & $0.91$       \\
#> Num. obs.  & $20$         & $20$         \\

Created on 2020-12-07 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment