1

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}
RDTJr
  • 185
  • 1
  • 9
  • Which figure caption are you looking for? It's printing the name of your code chunk. Maybe it's something else in your `.Rmd` file (e.g. YAML header)? – bttomio Feb 22 '21 at 21:39
  • Perhaps you need to add something like [this](https://stackoverflow.com/a/31336997/13249862)? – bttomio Feb 22 '21 at 21:41
  • Thank you for the link but I the table caption rather than a figure, e.g. 'Table 1: The top 3 point scorers in the NBA' and then the table below it – RDTJr Feb 22 '21 at 21:43
  • It's worked properly on two other tables but the rest of them aren't working – RDTJr Feb 22 '21 at 21:45
  • You need to provide more details in this case. – bttomio Feb 22 '21 at 21:50
  • 1
    I've just added screenshots to explain better – RDTJr Feb 22 '21 at 21:55

1 Answers1

1

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

bttomio
  • 2,206
  • 1
  • 6
  • 17
  • I've just attempted your solutions but they haven't been successful for me, it's just outputting the same issue. – RDTJr Feb 22 '21 at 22:04
  • Could you please provide a minimal reproducible example? It's hard to know what's going on without knowing the other parts of your `.Rmd` file. Thanks – bttomio Feb 22 '21 at 22:06
  • I've just added everything I'm using in the post. – RDTJr Feb 22 '21 at 22:11
  • 1
    I've found the solution! so it didn't like ```{r top_3pt_scorers}```, I had to change it to ```{r top3ptscorers}``` it didn't seem to like underscores – RDTJr Feb 22 '21 at 22:31
  • I see, good to hear! You can edit my answer or post a new one explaining your solution. – bttomio Feb 23 '21 at 07:40