Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 730
Fiddler Avatar asked Dec 07 '25 11:12

Fiddler


2 Answers

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

like image 56
Joanne Demmler Avatar answered Dec 10 '25 00:12

Joanne Demmler


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)
like image 29
David Siddons Avatar answered Dec 10 '25 02:12

David Siddons



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!