rr-markdown

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)

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.


Solution

  • 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