wolfsatthedoor
wolfsatthedoor

Reputation: 7303

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.

Upvotes: 2

Views: 3737

Answers (2)

henrik_ibsen
henrik_ibsen

Reputation: 813

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"))

Upvotes: 3

piie
piie

Reputation: 645

I have to check this when I update my tex, and I might need a little more information on what you want exactly, but if you want to have the table in the middle of the text, all you need to do is remove the \begin{center} and \end{center}. R chunks are really used as copy paste wherever you want, even working as web links if you want. Additionally, you can use kable() from knitr for tables and this sometimes helps sometimes makes things worse. I'll check back later when I have better internet.

Upvotes: 1

Related Questions