3

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!

Fiddler
  • 31
  • 1
  • 2
  • Cannot reproduce the problem. – Martin Schmelzer Dec 02 '18 at 12:58
  • I think the problem lies in your `knitr::` line. Since you've already loaded knitr, see if only `include_graphics(path)` works. – shiv_90 Dec 02 '18 at 13:02
  • @Shiv_90 I just tried removing the `knitr::` and i still have the same issue. – Fiddler Dec 02 '18 at 13:20
  • 2
    It could be because the path to the image file is wrong. When you process a notebook you'll often have a different working directory than when you knit a document. Try printing the result of `getwd()` in both situations, and see if they match. – user2554330 Dec 02 '18 at 14:21
  • 3
    @Shiv_90, the `knitr::` prefix will only make a difference if there are multiple versions of the `include_graphics` function around, and then it will be helpful to choose the right one. It definitely *won't* conflict with `library()`. – user2554330 Dec 02 '18 at 14:26
  • My guess is also the path. try an absolute path first – TC Zhang Dec 03 '18 at 07:49
  • I just had the same problem, but it only appeared after adding `runtime: shiny` to the YAML header. It does otherwise work fine. – Joanne Demmler Dec 07 '18 at 10:37
  • @Fiddler Were you able to resolve the problem? – shiv_90 Dec 25 '18 at 07:23

2 Answers2

5

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

Joanne Demmler
  • 1,406
  • 11
  • 31
3

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)
  • This still does not work when the path is not directly under the path of the R Markdown file when using the `runtime: shiny_prerendered` directive. This seems a bug because it prevents from placing the source files and the images in different folders under the project root. – Pablo Adames Dec 07 '21 at 11:10
  • A similar issue was discussed for the case of `knitr::include_graphics` when used to build a Xaringan presentation, see https://github.com/yihui/xaringan/issues/145. The path works as long as it is in the same path of the markdown file or on a path under it. It fails for paths above the path of the markdown file, thus passing a path argument like '../path/to/image' fails for a `xaringan` project as well as for a `learnr` project when using `knitr::incliude_graphics`. – Pablo Adames Dec 07 '21 at 11:28