RDTJr
RDTJr

Reputation: 205

Rmarkdown figure caption not printing correctly

I'm currently using Rstudio and running this code:

{r top_3pt_scorers, echo = FALSE}

top_3pt_shooters <- NBAdf %>%
  filter(PTS_TYPE == 3) %>%
  group_by(PLAYER_NAME) %>%
  dplyr::summarize(Threes_attempted = n(), Threes_made = sum(as.numeric(FGM)),
            Three_point_total = sum(PTS, na.rm = TRUE))

knitr::kable(head(top_3pt_shooters[order(top_3pt_shooters$Threes_made, decreasing = TRUE), ]),
             caption = "The top 3 point scorers in the NBA")

But I'm getting this output:

enter image description here

Whereas my ideal output is:

enter image description here

Do I need to change something within the kable argument?

Solution:

To solve this issue, I just had to change the following code:

{r top_3pt_scorers, echo = FALSE}

to

{r top3ptscorers, echo = FALSE}

Upvotes: 1

Views: 432

Answers (1)

bttomio
bttomio

Reputation: 2306

It seems like a code chunk problem. Try this:

```{r top_3pt_scorers, echo = FALSE}

top_3pt_shooters <- NBAdf %>%
  filter(PTS_TYPE == 3) %>%
  group_by(PLAYER_NAME) %>%
  dplyr::summarize(Threes_attempted = n(), Threes_made = sum(as.numeric(FGM)),
            Three_point_total = sum(PTS, na.rm = TRUE))

knitr::kable(head(top_3pt_shooters[order(top_3pt_shooters$Threes_made, decreasing = TRUE), ]),
             caption = "The top 3 point scorers in the NBA")
```

You may use fig_caption = TRUE in your YAML header.

This may help you:

---
title: "Caption"
author: "bttomio"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: 
  pdf_document:
    fig_caption: true
---

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

## Caption with kable

```{r dt}
dt <- mtcars[1:5, 1:6]
kbl(dt, caption = "Demo table", booktabs = T) %>%
  kable_styling(latex_options =c("striped", "hold_position"))
```

```{r dt2}
dt <- mtcars[1:5, 1:6]
kbl(dt, caption = "Same demo table", booktabs = T) %>%
  kable_styling(latex_options =c("striped", "hold_position"))
```

-output

enter image description here

Upvotes: 1

Related Questions