39

I have an externally created png image in a local directory that I'd like to import to a report using knitr. The ultimate target filetype is html. I have no trouble getting my own figures to appear when I create them with R code, but I'm at a loss what I would have thought was a simpler problem.

While I'm at it, how would I import a figure for which I have a url?

Steve Buyske
  • 393
  • 1
  • 3
  • 4

2 Answers2

56

If you already have a local image, you can just use the HTML or markdown syntax to include it in your document. HTML syntax is <img src="path/to/your/image" /> and markdown is ![title](path/to/your/image).

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • 3
    Is there a way to resize and center the image with rmarkdown in RStudio? – Paulo E. Cardoso Sep 29 '14 at 14:42
  • 1
    @PauloCardoso New posts for new questions please. – Yihui Xie Sep 29 '14 at 19:17
  • Sorry. I'm not asking anything different but a improvement to your answer. The addi could be as simple as 'No. there is not' or 'Yes, adding a piece of code'. But thank you anyway for taking your time to comment. – Paulo E. Cardoso Sep 29 '14 at 21:41
  • @PauloCardoso That is fine. Then my answer is, Yes, I have already mentioned the HTML syntax. – Yihui Xie Sep 30 '14 at 02:13
  • see also http://stackoverflow.com/questions/15625990/how-to-set-size-for-local-image-using-knitr-for-markdown – baptiste Oct 31 '16 at 02:48
  • @Yihui, it appears that the path can not contain spaces, as my attempts to enclose the path in quotes caused errors. Putting the file in a directory path where none of the directories had spaces in their names worked. – svannoy Apr 25 '17 at 20:49
0

You can include image in knitted html by using knitr::include_graphics() in R Markdown.

knitr::include_graphics(rep('path_of_image.png'))

Make sure that you use ' / ' in path instead of ' \ '.

Ullas
  • 3
  • 4