The Problem
When using rmarkdown in RStudio, my stargazer(glm())
output gets positioned below the text that I would like it to. It gets
Set float = FALSE
. From the manual,
float: a logical value that indicates whether the resulting table will be a
floating table (set off, for instance, by \begin{table} and \end{table}).
In LaTeX, a table
environment is a floating environment.
E.g.
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), float = FALSE)
If you set float = FALSE
, you will not have any of the features that come with a floating environment, such as captions (i.e. the title) or labels. Instead, consider setting an unconditional table placement with the float
package. As an example, consider the following document (I use \clearpage
to start the body on page 2 so we can see the adjoining pages on the screenshot):
---
title: "Untitled"
author: "Me"
header-includes:
- \usepackage{lipsum}
output: pdf_document
---
\clearpage
\lipsum[1]
```{r setup, echo = FALSE, include = FALSE}
library(stargazer)
mtcars_glm <- glm(formula = vs ~ disp + am + cyl + mpg, family = "binomial", data = mtcars)
```
Table 1 here.
```{r tab1, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"),
header = FALSE, title = "Table 1")
```
\lipsum[2-3]
Table 2 here.
```{r tab2, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"),
header = FALSE, title = "Table 2")
```
\lipsum[4]
which gives
where Table 2 has been bumped to the following page, and the text after Table 2 has been moved up. This is how LaTeX behaves; it does not want to leave too much white space at the bottom of the page. To insist that Table 2 follows a piece of text, you can use the H
specifier (which requires the float
LaTeX package). Here's the same document, but note the table.placement
argument in the tab2
chunk:
---
title: "Untitled"
author: "Me"
header-includes:
- \usepackage{float}
- \usepackage{lipsum}
output: pdf_document
---
\clearpage
\lipsum[1]
```{r setup, echo = FALSE, include = FALSE}
library(stargazer)
mtcars_glm <- glm(formula = vs ~ disp + am + cyl + mpg, family = "binomial", data = mtcars)
```
Table 1 here.
```{r tab1, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"),
header = FALSE, title = "Table 1")
```
\lipsum[2-3]
Table 2 here.
```{r tab2, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"),
header = FALSE, title = "Table 2", table.placement = "H")
```
\lipsum[4]
which gives
The table is placed after the text ("Table 2 here"), even at the expense of leaving white space at the bottom of the page. An alternative is \FloatBarrier
from the placeins
package; see https://tex.stackexchange.com/questions/19766/how-to-control-the-position-of-floating-images.
In general, you should leave float (i.e. tables and figures) placements to LaTeX. See https://tex.stackexchange.com/questions/39017/how-to-influence-the-position-of-float-environments-like-figure-and-table-in-lat for an extensive discussion.