add local image file in R presentation

ponyhd picture ponyhd · Aug 7, 2015 · Viewed 22.2k times · Source

I'm trying to include one imaging file (.png) using R markdown for R presentation. I followed the suggestion from: How to import local image using knitr for markdown but by using ![title](my.png), I get this error:

Error: unexpected '[' in "!["

The my.png file is in current path. I also tried using absolute path but got the same error message.

Putting above inside the r chuck failed too.

I also tried

```{r,fig.width=350, fig.height=250,echo=FALSE}
library(png)
library(grid)
appimg <- readPNG('my.png')
grid.raster(appimg)
```

but failed too!

I am working on windows 7 R studio 0.98.1102 and R 3.2.1 .

Answer

Lucylalalala picture Lucylalalala · Sep 5, 2016

After a huge mount of Google search. I finanlly figure out what is the problem.

The key is that HTML cannot refer local file for security reason. Except that it is a local HTML file, then it can refer local file in the same file directory.

And R presentation is actually a HTML file like a webpage.

So, just put your image file the same directory with the HTML file, things will work. At least it worked for me.

Just use

![some caption](img_file_name.png)