new123
new123

Reputation: 27

Cannot add .png image to rmarkdown - Error: unexpected '[' in "!["

I want to add a picture (.png) to my r markdown file (and I want to knit to PDF). However, I've tried this simple code recommended everywhere but it doesn't work for me:

![Caption for the picture.](image.png)

Because I get the error:

Error: unexpected '[' in "!["

I'm very new to r markdown. I'm not sure what I'm doing wrong - I've already moved the .Rmd file to where the images are which I saw was also recommended but it doesn't seem like that's the issue. I haven't seen anyone else have issues with the "![]" before.

Does anyone have any suggestions on how to fix this?

(Also, knitr::include_graphics("image.png") works for me and yes I could use that but I want to get to the root of the problem above...)

Upvotes: 1

Views: 4359

Answers (1)

Waldi
Waldi

Reputation: 41260

The image insertion Rmarkdown syntax doesn't work inside an r chunck.
In a chunck you can use knitr::include_graphics("image.png") as mentioned in your post.

This doesn't work :

```{r}
![Caption for the picture.](image.png)
```

Error: unexpected '[' in "!["

This works :

Either :   
```{r}
knitr::include_graphics("image.png")
```

Or :   
![Caption for the picture.](image.png)

Upvotes: 6

Related Questions