15

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 .

Community
  • 1
  • 1
ponyhd
  • 491
  • 1
  • 4
  • 19

4 Answers4

20

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)

Lucylalalala
  • 313
  • 2
  • 10
6

Removing the quotes actually worked for me and note that one better doesn't keep any spaces while naming the image as in use,

![title](my_image.png)

Using quotes or having spaces(when renamed) hasn't worked for me in this case.

4

The 'imager' package makes this pretty easy:

library(imager)
myimg <- load.image("<pathto>/file.png")
plot(myimg)
Brian D
  • 2,570
  • 1
  • 24
  • 43
  • Thanks, how could we remove x and y axis from plot in this package? – ah bon May 24 '22 at 03:09
  • 1
    it's the regular base r `plot()` command so `plot(myimg, axes=FALSE)` would work as usual. See: https://stackoverflow.com/questions/1154242/remove-plot-axis-values – Brian D May 24 '22 at 19:24
3

If your output format is : slidy_presentation You can use html code:

<img src="/Users/name/folder/xyz.png";>

Further, if you wish to align and adjust the image to a specific side (say right of your final output and specify a size) you can use something like -

<img src="/Users/name/folder/xyz.png"; style="max-width:280px;float:right;">