I'm somewhat new to R Markdown, so apologies if this is silly. I'm preparing a pdf report using R Markdown/Knitr, and running into a problem where, if I try to include a styled table using kable, it moves the table to the bottom of the page.
Here is an example of the code:
---
title: "Testing"
output:
pdf_document:
fig_caption: yes
tables: true
---
```{r setup, include=FALSE}
library(knitr)
library(kableExtra)
```
Section 1: In Table \ref{fig:table1} below...
```{r table1, echo=FALSE}
kable(head(mtcars[,1:4],4), format = "latex", align = "c", caption ="\\label{fig:table1}Table Caption") %>%
column_spec(1, bold = T, width = "6em") %>%
kable_styling(position = "center")
```
Section 2: Lorem ipsum dolor sit amet
The PDF this outputs for me has "section 1" and "section 2" following each other, with the table at the very bottom of the page.
I have tried removing the caption, and using fig.cap at the start of the chunk. Although my table stays where it should, it doesn't generate a caption at all, and references to the figure turn into ??.
What works: getting rid of kableExtra, changing format from latex to pandoc. The table stays where it should, and I get the captions, but the table loses the extra formatting that I'd really like to have for the report.
What am I doing wrong? Thanks for your help!
Edit: Sorry! I'd like for the table to show up where I put it (after "section 1", before "section 2"). Like below, only with a caption/figure label. (The linked image is what I get if I get rid of the caption arg in that code chunk above)