leifeld/texreg

Problems with the texreg longtable option in RMarkdown

sgenter1 opened this issue · 1 comments

Hi,

I was attempting to create a long regression table with Stargazer, but ran into problems and was delighted to find the texreg package and to see it has a longtable option. When I attempted to create the table, however, I received an error message. I posted two related questions on the RStudio community forum (one regarding Stargazer and one regarding the question I'm posting here) and got over a hundred views between the two posts, but no responses. I hope I'm not violating any community norms by re-posing the question here, but it seems like going to the source might be the best approach here.

Below is an example that reproduces the problem. This example doesn't give me the exact error message I get when using my own data, but it's close so I'm hoping a solution to one might result in a solution to the other. Note that the "tv_short" model works fine, but the "tv_long" model doesn't work when calling texreg directly or when pasting the results of the LaTex output into the .Rms.

As an aside, the exact error message I'm getting with my own data (not the gss_cat data) is:

! Misplaced \noalign.
\hline ->\noalign 
                  {\ifnum 0=`}\fi \let \hskip \vskip \let \vrule \hrule \let...
l.166 \hline

Error: LaTeX failed to compile render_preview.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See render_preview.log for more info.
Execution halted

The error message from knitting the .Rmd below is:

! Extra }, or forgotten $.
<template> \unskip \hfil }
                          \hskip \tabcolsep \endtemplate 
l.115 rincome$25000 or more        &
                                     $-0.58^{*}$  \\ 

Error: LaTeX failed to compile longtable_texreg.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See longtable_texreg.log for more info.
Execution halted

Please let me know if I can clarify anything. I very much appreciate your time and attention.

---
title: ""
header-includes:
   - \usepackage{longtable}
output:
      bookdown::pdf_document2:
        toc: no
        keep_tex: false
---

```{r, include=FALSE}
library(forcats) # for the gss_cat data
library(texreg)

tv_short <- lm(tvhours ~ marital + age,
               data = gss_cat)

tv_long <- lm(tvhours ~ marital + age + race + rincome + partyid + relig + denom,
              data = gss_cat)

```

```{r, results='asis', echo = FALSE}

texreg(tv_long,
       #file = "longtable_texreg.tex",
       caption.above = TRUE,
       longtable = TRUE,
       use.packages = FALSE)

```

<!-- Below is the raw LaTex code from "file = "longtable_texreg.tex"" Using the raw code produces the same error message -->

<!-- \begin{center} -->
<!-- \begin{longtable}{l c} -->
<!-- \hline -->
<!--  & Model 1 \\ -->
<!-- \hline -->
<!-- \endfirsthead -->
<!-- \hline -->
<!--  & Model 1 \\ -->
<!-- \hline -->
<!-- \endhead -->
<!-- \hline -->
<!-- \endfoot -->
<!-- \hline -->
<!-- \multicolumn{2}{l}{\scriptsize{$^{***}p<0.001$; $^{**}p<0.01$; $^{*}p<0.05$}}\\ -->
<!-- \caption{Statistical models} -->
<!-- \label{table:coefficients} -->
<!-- \endlastfoot \\ -->
<!-- (Intercept)                  & $1.75$       \\ -->
<!--                              & $(1.16)$     \\ -->
<!-- maritalNever married         & $0.29$       \\ -->
<!--                              & $(1.10)$     \\ -->
<!-- maritalSeparated             & $0.37$       \\ -->
<!--                              & $(1.10)$     \\ -->
<!-- maritalDivorced              & $0.13$       \\ -->
<!--                              & $(1.10)$     \\ -->
<!-- maritalWidowed               & $0.09$       \\ -->
<!--                              & $(1.10)$     \\ -->
<!-- maritalMarried               & $-0.14$      \\ -->
<!--                              & $(1.10)$     \\ -->
<!-- age                          & $0.02^{***}$ \\ -->
<!--                              & $(0.00)$     \\ -->
<!-- raceBlack                    & $1.04^{***}$ \\ -->
<!--                              & $(0.11)$     \\ -->
<!-- raceWhite                    & $-0.14$      \\ -->
<!--                              & $(0.09)$     \\ -->
<!-- rincomeDon't know            & $0.34$       \\ -->
<!--                              & $(0.35)$     \\ -->
<!-- rincomeRefused               & $-0.43$      \\ -->
<!--                              & $(0.30)$     \\ -->
<!-- rincome$25000 or more        & $-0.58^{*}$  \\ -->
<!--                              & $(0.28)$     \\ -->
<!-- rincome$20000 - 24999        & $-0.13$      \\ -->
<!--                              & $(0.29)$     \\ -->
<!-- rincome$15000 - 19999        & $-0.00$      \\ -->
<!--                              & $(0.30)$     \\ -->
<!-- rincome$10000 - 14999        & $0.06$       \\ -->
<!--                              & $(0.29)$     \\ -->
<!-- rincome$8000 to 9999         & $0.21$       \\ -->
<!--                              & $(0.33)$     \\ -->
<!-- rincome$7000 to 7999         & $-0.17$      \\ -->
<!--                              & $(0.36)$     \\ -->
<!-- rincome$6000 to 6999         & $0.32$       \\ -->
<!--                              & $(0.36)$     \\ -->
<!-- rincome$5000 to 5999         & $0.28$       \\ -->
<!--                              & $(0.36)$     \\ -->
<!-- rincome$4000 to 4999         & $0.25$       \\ -->
<!--                              & $(0.36)$     \\ -->
<!-- rincome$3000 to 3999         & $0.32$       \\ -->
<!--                              & $(0.34)$     \\ -->
<!-- rincome$1000 to 2999         & $0.19$       \\ -->
<!--                              & $(0.32)$     \\ -->
<!-- rincomeLt $1000              & $0.46$       \\ -->
<!--                              & $(0.34)$     \\ -->
<!-- rincomeNot applicable        & $0.67^{*}$   \\ -->
<!--                              & $(0.28)$     \\ -->
<!-- partyidDon't know            & $0.00$       \\ -->
<!--                              & $(2.45)$     \\ -->
<!-- partyidOther party           & $-0.08$      \\ -->
<!--                              & $(0.37)$     \\ -->
<!-- partyidStrong republican     & $-0.25$      \\ -->
<!--                              & $(0.34)$     \\ -->
<!-- partyidNot str republican    & $-0.24$      \\ -->
<!--                              & $(0.34)$     \\ -->
<!-- partyidInd,near rep          & $-0.20$      \\ -->
<!--                              & $(0.34)$     \\ -->
<!-- partyidIndependent           & $-0.02$      \\ -->
<!--                              & $(0.34)$     \\ -->
<!-- partyidInd,near dem          & $-0.27$      \\ -->
<!--                              & $(0.34)$     \\ -->
<!-- partyidNot str democrat      & $-0.14$      \\ -->
<!--                              & $(0.34)$     \\ -->
<!-- partyidStrong democrat       & $-0.06$      \\ -->
<!--                              & $(0.34)$     \\ -->
<!-- religDon't know              & $2.90^{*}$   \\ -->
<!--                              & $(1.16)$     \\ -->
<!-- religInter-nondenominational & $0.95$       \\ -->
<!--                              & $(0.85)$     \\ -->
<!-- religNative american         & $1.66$       \\ -->
<!--                              & $(1.03)$     \\ -->
<!-- religChristian               & $0.63$       \\ -->
<!--                              & $(0.75)$     \\ -->
<!-- religOrthodox-christian      & $0.66$       \\ -->
<!--                              & $(0.88)$     \\ -->
<!-- religMoslem/islam            & $0.37$       \\ -->
<!--                              & $(0.84)$     \\ -->
<!-- religOther eastern           & $-0.06$      \\ -->
<!--                              & $(1.05)$     \\ -->
<!-- religHinduism                & $0.36$       \\ -->
<!--                              & $(0.88)$     \\ -->
<!-- religBuddhism                & $0.74$       \\ -->
<!--                              & $(0.83)$     \\ -->
<!-- religOther                   & $1.02$       \\ -->
<!--                              & $(0.81)$     \\ -->
<!-- religNone                    & $0.94$       \\ -->
<!--                              & $(0.78)$     \\ -->
<!-- religJewish                  & $0.72$       \\ -->
<!--                              & $(0.80)$     \\ -->
<!-- religCatholic                & $1.17$       \\ -->
<!--                              & $(0.78)$     \\ -->
<!-- religProtestant              & $0.56$       \\ -->
<!--                              & $(0.73)$     \\ -->
<!-- denomDon't know              & $1.83^{*}$   \\ -->
<!--                              & $(0.80)$     \\ -->
<!-- denomNo denomination         & $-0.31$      \\ -->
<!--                              & $(0.64)$     \\ -->
<!-- denomOther                   & $-0.41$      \\ -->
<!--                              & $(0.63)$     \\ -->
<!-- denomEpiscopal               & $-0.25$      \\ -->
<!--                              & $(0.65)$     \\ -->
<!-- denomPresbyterian-dk wh      & $-0.24$      \\ -->
<!--                              & $(0.66)$     \\ -->
<!-- denomPresbyterian, merged    & $-0.63$      \\ -->
<!--                              & $(0.76)$     \\ -->
<!-- denomOther presbyterian      & $-0.17$      \\ -->
<!--                              & $(0.80)$     \\ -->
<!-- denomUnited pres ch in us    & $-0.68$      \\ -->
<!--                              & $(0.70)$     \\ -->
<!-- denomPresbyterian c in us    & $-0.37$      \\ -->
<!--                              & $(0.72)$     \\ -->
<!-- denomLutheran-dk which       & $-0.05$      \\ -->
<!--                              & $(0.66)$     \\ -->
<!-- denomEvangelical luth        & $-0.74$      \\ -->
<!--                              & $(0.70)$     \\ -->
<!-- denomOther lutheran          & $0.30$       \\ -->
<!--                              & $(0.85)$     \\ -->
<!-- denomWi evan luth synod      & $-0.58$      \\ -->
<!--                              & $(0.80)$     \\ -->
<!-- denomLutheran-mo synod       & $-0.38$      \\ -->
<!--                              & $(0.67)$     \\ -->
<!-- denomLuth ch in america      & $-0.13$      \\ -->
<!--                              & $(0.76)$     \\ -->
<!-- denomAm lutheran             & $-0.43$      \\ -->
<!--                              & $(0.69)$     \\ -->
<!-- denomMethodist-dk which      & $-0.16$      \\ -->
<!--                              & $(0.66)$     \\ -->
<!-- denomOther methodist         & $0.10$       \\ -->
<!--                              & $(0.82)$     \\ -->
<!-- denomUnited methodist        & $-0.12$      \\ -->
<!--                              & $(0.64)$     \\ -->
<!-- denomAfr meth ep zion        & $-0.04$      \\ -->
<!--                              & $(0.89)$     \\ -->
<!-- denomAfr meth episcopal      & $-0.17$      \\ -->
<!--                              & $(0.75)$     \\ -->
<!-- denomBaptist-dk which        & $0.29$       \\ -->
<!--                              & $(0.64)$     \\ -->
<!-- denomOther baptists          & $-0.30$      \\ -->
<!--                              & $(0.67)$     \\ -->
<!-- denomSouthern baptist        & $-0.03$      \\ -->
<!--                              & $(0.64)$     \\ -->
<!-- denomNat bapt conv usa       & $0.22$       \\ -->
<!--                              & $(0.83)$     \\ -->
<!-- denomNat bapt conv of am     & $-0.29$      \\ -->
<!--                              & $(0.73)$     \\ -->
<!-- denomAm bapt ch in usa       & $1.23$       \\ -->
<!--                              & $(0.70)$     \\ -->
<!-- denomAm baptist asso         & $-0.06$      \\ -->
<!--                              & $(0.67)$     \\ -->
<!-- denomNot applicable          & $-0.69$      \\ -->
<!--                              & $(0.69)$     \\ -->
<!-- \hline -->
<!-- R$^2$                        & $0.13$       \\ -->
<!-- Adj. R$^2$                   & $0.12$       \\ -->
<!-- Num. obs.                    & $11299$      \\ -->
<!-- \end{longtable} -->
<!-- \end{center} -->

Hi, and thanks again for your time and attnetion. I did end up getting a reply on the RStudio forum that solved the problem (for both datasets, despite the different error messages). Apparently the $ in the income variable is a special character in LaTex and needs to be removed from the variable in advance of running the model so that it doesn't get passed to texreg. The solution was posted here: https://community.rstudio.com/t/how-to-use-texreg-for-a-long-table-in-rmarkdown/99509/3

Essentially, cderv at RStudio suggested the following to remove the $ before running the model:

gss_cat2 <- gss_cat %>%
  mutate(rincome = stringr::str_replace_all(rincome, "[$]", "\\\\$"))