How to use stargazer for a long table in RMarkdown

Hi, I'm trying to make a table with stargazer, but it's a long table and stargazer cuts it off at the end of the page. I looked around and found a user written function designed to help with this here: https://github.com/labreumaia/longtable.stargazer/blob/master/longtable.stargazer.R

The function works except for two things. The main problem is that the function produces two duplicate tables when used with rmarkdown. The second problem, though more superficial, is that when using the function the top border no longer matches the bottom border as it would with the stargazer function (i.e., the top border has one line instead of two thin lines). In the example below I pasted the function and created a table using it that reproduces the problem. I'm hoping someone can get the function to produce one table. Secondarily, it would be nice if the top and bottom borders also match.

---
title: ""
output: 
  bookdown::pdf_document2:
    toc: no
    keep_tex: false
---

```{r, include=FALSE}
#load stargazer
library(stargazer)

#function to make stargazer compatible with longtable, found here:
#https://github.com/labreumaia/longtable.stargazer/blob/master/longtable.stargazer.R

longtable.stargazer = function(..., float = T, longtable.float = F, 
                               longtable.head = T, filename = NULL){
  # Capturing stargazer to hack it
  require(stargazer)
  res = capture.output(
    stargazer(..., float = float)
  )
  # Changing tabulare environment for longtable
  res = gsub("tabular", "longtable", res)
  # removing floating environment
  if(float == T & longtable.float == F){
    res[grep("table", res)[1]] = res[grep("longtable", res)[1]]
    # Removing extra longtable commands
    res = res[-grep("longtable", res)[2]]
    res = res[-length(res)]
  }
  # Adding page headings
  if(longtable.head == T){
    res = c(res[1:which(res == "\\hline \\\\[-1.8ex] ")[1] - 1], "\\endhead", res[which(res == "\\hline \\\\[-1.8ex] ")[1]:length(res)])
  }
  # Exporting
  cat(res, sep = "\n")
  # Exporting
  if(!is.null(filename)){
    cat(res, file = filename, sep = "\n")
    # Message
    cat(paste("\nLaTeX output printed to", filename, "\n", sep = " ", 
              collapse = ""))
  }else{
    cat(res, sep = "\n")
  }
}
```

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

# model for table
mod <- lm(mpg ~ cyl, data = mtcars)

# example showing how longtable.stargazer function produces two tables
longtable.stargazer(mod,
                    title = "More Cylinders, Worse Mileage",
                    header = FALSE)
```

Hi all,

I've found a temporary workaround for now by removing spaces with stargazer's no.space = TRUE option and making the font size smaller with the font.size option. It's not a great solution because the table is now difficult to read. It's been nearly a week with no response, so I'm thinking of posting my question elsewhere. If I find an answer I'll update this post too.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.