Reputation: 3396
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)
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.
Upvotes: 2
Views: 5455
Reputation: 41
I had the same question and it turns out that the figure needs to be in it's own paragraph (preceded and followed by a newline and two spaces) in order to show the caption, as described here.
Upvotes: 0
Reputation: 3242
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")
```
Upvotes: 2
Reputation: 50668
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
Upvotes: 2