title: Stargazer output appears below text - rmarkdown to pdf
body: <p><strong>The Problem</strong></p>
  
  <p>When using rmarkdown in RStudio, my <code>stargazer(glm())</code> output gets positioned below the text that I would like it to. It gets positioned in a different spot than the <code>r chunk</code> is.</p>
  
  <p>The PDF is created perfectly, it's just the position of the <code>stargazer</code> output that is a problem.</p>
  
  <p><strong>Background</strong></p>
  
  <p>I am trying to create a PDF with lots of text and a couple of <code>stargazer</code> <code>glm()</code> output between a few of the paragraphs. When I put more than one <code>stargazer()</code> output in my rmarkdown file and then "Knit" to PDF, the <code>stargazer()</code> output gets moved down below the text.</p>
  
  <p>I would like the <code>stargazer output</code> to get positioned where I put the <code>r chunks</code>. </p>
  
  <p>I do not have the same problem when using inserting <code>ggplot2()</code> output in a similar manner.</p>
  
  <p><strong>Failed Attempts</strong></p>
  
  <p>I have tried as many combinations as I know how of positioning my <code>r chunks</code> arguments. (Just in case)</p>
  
  <p>I have tried every combination of tab vs. spaces, before and after paragraphs/headers/r-chunks/etc. (That was a problem I had once with ggplot2 output)</p>
  
  <p>I have referenced the following StackOverflow Questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/29696172/how-to-hold-figure-position-with-figure-caption-in-pdf-output-of-knitr?noredirect=1&amp;lq=1">Using fig.pos = "H" and fig.pos = "h"</a></li>
  <li><a href="https://stackoverflow.com/questions/16626462/figure-position-in-markdown-when-converting-to-pdf-with-knitr-and-pandoc?noredirect=1&amp;lq=1">This very similar question</a></li>
  <li><a href="https://yihui.name/knitr/demo/minimal/" rel="nofollow noreferrer">Yihui Xie's examples</a></li>
  <li><a href="https://stackoverflow.com/questions/14670299/using-stargazer-with-rstudio-and-knitr/30423627">Stargazer and RStudio</a></li>
  <li>I tried using "\" at the end of (and between) chunks as mentioned in one of the above posts.</li>
  </ul>
  
  <p><strong>Reproducible Example</strong></p>
  
  <p>A reproducible example of my work problem:</p>
  
  <pre><code>---
  title: "Untitled"
  author: "Me"
  output: pdf_document
  ---
  
  ```{r setup, echo = FALSE}
  library(stargazer)
  
  mtcars_glm &lt;- glm(formula = vs ~ disp + am + cyl + mpg, family = "binomial", data = mtcars)
  
  ```
  
  # Heading1
  
  I have tried creating paragraphs like this.
  
      I have also tried creating paragraphs with 2 indents.
  
  ## Heading2
  
  Lets try to create a couple of nice tables with stargazer.
  
  ```{r attempt1, results = 'asis', echo = FALSE}
  
  stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), header = FALSE)
  
  ```
  
  And then we will add some text down here, too.
  
  ```{r attempt2, results = 'asis', echo = FALSE}
  
  stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), header = FALSE)
  
  ```
  
  And some more text.
  
  ```{r attempt3, results = 'asis', echo = FALSE}
  
  stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), header = FALSE)
  
  ```
  
  Lets see what happens.
  
  ### Heading3
  
  ```{r plot_attempt}
  
  boxplot(mtcars$mpg ~ mtcars$cyl)
  ```
  
  # Second Section
  
  ## Second Header
  </code></pre>
  
  <p>Here are the 3 pages of output:</p>
  
  <p>Page #1
  <a href="https://i.stack.imgur.com/8FOIr.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8FOIr.jpg" alt="Page 1"></a></p>
  
  <p>Page #2
  <a href="https://i.stack.imgur.com/uT8p7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uT8p7.jpg" alt="Page 2"></a></p>
  
  <p>Page #3
  <a href="https://i.stack.imgur.com/0h43Z.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0h43Z.jpg" alt="Page 3"></a></p>
  
  <p>Here is my sessionInfo:</p>
  
  <pre><code>R version 3.4.4 (2018-03-15)
  Platform: x86_64-w64-mingw32/x64 (64-bit)
  Running under: Windows &gt;= 8 x64 (build 9200)
  
  Matrix products: default
  
  locale:
  [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
  [5] LC_TIME=English_United States.1252    
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     
  
  other attached packages:
  [1] stargazer_5.2.1
  
  loaded via a namespace (and not attached):
   [1] compiler_3.4.4  backports_1.1.2 magrittr_1.5    rprojroot_1.3-2 htmltools_0.3.6 tools_3.4.4     yaml_2.1.19     Rcpp_0.12.16    stringi_1.1.7   rmarkdown_1.9  
  [11] knitr_1.20      stringr_1.3.0   digest_0.6.15   evaluate_0.10.1
  </code></pre>
  
  <p><strong>Thanks</strong></p>
  
  <p>If you can help me, thanks. I do not know much about LaTeX or Pandoc, so I imagine it is some sort of knowledge gap. Feel free just to point me in the right direction, too, if you think you've found a solution.</p>
  
  <p>I appreciate it.</p>