Search code examples
rlatexr-markdownknitrfigure

How to keep a TABLE "here defnitely" in R markdown


I want to keep a table created in an rmarkdown document that outputs to pdf locked in place and not flexibly fitting between text.

For example, my code is:

---
title: "Rest"
author: "Dowdy"
date: "3/23/2019"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
library(stargazer)
library(data.table)
```

Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. 


Hello friend. 


\begin{center}
```{r star, results='asis',echo=F,eval=T, message=FALSE}

star = capture.output(stargazer(data.table(cars)[1:15],digits=3,type = "latex",summary=F,digits.extra = 2,header = F))

star = gsub("& \\$([0-9]+)\\$", "& \\$ 0.000 \\$", star)

cat(star)

```
\end{center}

Welcome to my world of toys.  Welcome to my world of toys.Welcome to my world of toys.Welcome to my world of toys.Welcome to my world of toys.Welcome to my world of toys.Welcome to my world of toys.Welcome to my world of toys.Welcome to my world of toys.Welcome to my world of toys.
Welcome to my world of toys.
Welcome to my world of toys. gs I am testing heloo. 
Of all the things I am testing heloo. Of all the things I am testing heloo. Of all the things I am testing heloo. 

Of all the things I am testing heloo. Of all the things I am testing heloo. 

1. Test hi hihhihihih 

2. why is this hiiii

3. why test test test 


4. test test test

Unfortunately, the output is this:

enter image description here

I do not want the image to split up my numbered list and text this way. I want the order of the text and image to be exactly as I typed it in the rmarkdown document. Specifically, that numbered list and the paragraph about toys should appear all after the table as it is typed in the markdown.

For figures, this question tried to do what I want to do I believe. Of course, I am asking this for tables instead of figures, so the solution does not help me.


Solution

  • As suggested in the link you provided, include

    header-includes:
       - \usepackage{float}
    

    in the YAML header. Then, and according to the the stargazer manual, you can set the float permission with argument table.placement = "H", which

    Places the float at precisely the location in the LaTeX code.

    Ref: LaTeX/Floats, Figures and Captions

    In your case, it would be

    star = capture.output(stargazer(data.table(cars)[1:15], digits=3,
                          type = "latex",summary=F,digits.extra = 2,
                          header = F, table.placement = "H"))