Fiddler
Fiddler

Reputation: 31

Using include_graphics in R Markdown does not reproduce the image in HTML file

I am attempting to use R Markdown Notebooks (.Rmd files) in R Studio to capture notes and excercises while learning R programming. I find that any plots generated through the code chunks are being replicated in the corresponding html file correctly, however I am unable to get images to be replicated in the html.

Sample code below - The image is a .PNG file in the working directory path.

```{r}
library(knitr)
knitr::include_graphics("MyImage.PNG")
```

This replicates the image in the R Markdown Notebook correctly, but not in the html file.

I am able to replicate the image in the html file by directly using html syntax -

<img src="MyImage.PNG" alt="MyImage">

I have looked through other questions around this topic, but could not resolve this issue through any of the solutions provided. I would be grateful if any of you can help resolve this.

Thanks!

Upvotes: 3

Views: 9524

Answers (2)

David Siddons
David Siddons

Reputation: 31

I had the same problem when using shiny_prerendered with a learnr tutorial... This from Yan Holtz worked for me:

library(png)
library(grid)
img <- readPNG("photos/header_stats.png")
grid.raster(img)

Upvotes: 3

Joanne Demmler
Joanne Demmler

Reputation: 1446

I think this might be a bug to do with adding shiny.

I just did a quick test and it works for a normal document:

---
title: "Test"
output: html_document
---

```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)
```

```{r, echo=FALSE, out.width="50%"}
include_graphics("../images/RMarkdownOutputFormats.png")
```

enter image description here

but when I add shiny it doesn't work anymore:

---
title: "Test"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)
```

```{r, echo=FALSE, out.width="50%"}
include_graphics("../images/RMarkdownOutputFormats.png")
```

enter image description here

Upvotes: 6

Related Questions