简体   繁体   中英

R Markdown image caption not showing

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.

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") 
```

在此处输入图像描述

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM