Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add local image file in R presentation

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 .

like image 955
ponyhd Avatar asked Aug 07 '15 21:08

ponyhd


People also ask

How do I insert an image into R markdown?

To add an image in markdown you must stop text editing, and you do this with the command [Alt text] precedeed by a ! Then you have to add the path to the image in brackets. The path to the image is the path from your directory to the image.

How do you create a presentation in R?

To create a new R Presentation you execute the New File -> R Presentation command: After specifying the location to save the presentation, a new presentation will be created and a preview will show within the Presentation tab in the upper right corner of the IDE.

What is knitr package?

The R package knitr is a general-purpose literate programming engine, with lightweight API's designed to give users full control of the output without heavy coding work. It combines many features into one package with slight tweaks motivated from my everyday use of Sweave.


1 Answers

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)

like image 59
Lucylalalala Avatar answered Oct 10 '22 00:10

Lucylalalala