1

In R Markdown, I am trying to add a caption to an image, but it is not showing. Using RStudio and "Knit to HTML" Code & screenshot of HTML are below. There is no caption, even though it is in the brackets.

# First test
## SubHeader 1
kl;jdafkjjdsfajk;
![literally the most amazing apple ever](C:/Users/.../Stemilt-Cosmic-Crisp-Apple-2019.jpg)

enter image description here

This similar question is unanswered, but it looks like "fig.cap" is needed. I tried adding {r fig.cap = "caption2- literally the most amazing apple ever"} to the code (as line 5 in the above) but it did not work, it just printed the exact text that was entered, curly braces and all.

a11
  • 3,122
  • 4
  • 27
  • 66
  • I thought the issue was that there is no image text, because in your example output, the image text is missing. You should fix that and make the question a little more clear. – neilfws Apr 26 '21 at 23:49
  • I want a caption, like in the [accepted answer here](https://stackoverflow.com/questions/31926623/figures-captions-and-labels-in-knitr). What else should I say instead of "I want a caption"? There is no problem just writing text, as "kl;jdafkjjdsfajk;" prints fine. – a11 Apr 26 '21 at 23:52
  • I think the combination of "it did not work" and the missing image text (which is still missing by the way) gives the impression that no image text was the issue. – neilfws Apr 27 '21 at 00:02

2 Answers2

2

You can either use the fig.cap argument to an R code chunk with knitr::include_graphics, or provide a caption through a markdown image link.

A minimal example:

---
title: "Untitled"
output: html_document
---

# Option 1: `fig.cap` with `include_graphics`

```{r echo=FALSE, fig.cap = "Figure caption"}
knitr::include_graphics("https://mathworld.wolfram.com/images/gifs/rabbduck.jpg")
```


# Option 2: The markdown way way

![Figure caption](https://mathworld.wolfram.com/images/gifs/rabbduck.jpg)

produces

enter image description here

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • 1
    Just to note that option 2 (which is the same method as used in the original question) **does** generate a caption - if you examine the HTML you'll find a paragraph of class = caption. Perhaps the OP was confused because it is not styled in the same way as a caption generated when graphics are generated in a code chunk _i.e._ centered and italicised. [Same goes for](https://www.w3schools.com/tags/tag_figcaption.asp)`
    `.
    – neilfws Apr 27 '21 at 01:26
  • Option 2 was in my OP and didn't work, hence the question – a11 Apr 27 '21 at 01:30
2

maybe I over thought this, but I saw with HTML output docs, you need the fig_caption: true in your YAML for the the figure to get rendered with a caption. I used a few packages I use when posting images in R markdown reports.

---
title: "test"
output: 
  html_document:
      fig_caption: true
---
    
```{r setup, fig.cap="This is a screenshot",fig.align = 'center', warning=FALSE,     message=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(cowplot)
library(magick)
ggdraw() +
  draw_image("delete.png") 
```

enter image description here

Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27
  • 2
    This is not necessary. [`fig_caption = TRUE` by default for `html_document`](https://github.com/rstudio/rmarkdown/blob/898199b9bc3df2da50edd1f7ded472e6c9e46c1b/R/html_document.R#L224). – Maurits Evers Apr 28 '21 at 12:22