Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert plain picture (jpeg / png) from chunk with knitr

Tags:

I want to insert a picture at the beginning of text using a code block, not markdown. I am using the Tufte handout template output: rmarkdown::tufte_handout and when I insert it straight after the YAML header but before TOC like this:

\centering ![width='100%'](./cropped-banner_efpt.jpg) \raggedright \tableofcontents \clearpage 

the image then spans the main body. I know that with chunks there is an option to have the chunk to span the whole page placing fig.fullwidth = TRUE in the chunk header, but I am a bit stuck with this as I am not generating any graph from data and I do not know how to simpy place an image from within a chunk.

Another matter was that when I set toc: true in the YAML header, the image would only come after the inserted toc - that is why I am inserting toc with the latex command.

Thank you for your suggestions.

like image 385
r0berts Avatar asked Apr 01 '16 07:04

r0berts


People also ask

How do I insert a JPEG into R markdown?

To insert an image, you can also use an R code chunk. --- title: "R Markdown Tips and Tricks" output: html_document --- You can also insert an image using R code: ```{r} knitr::include_graphics("img/rmarkdown_hex. png") ``` The image looks smaller by default though.

How do I paste a picture into a RMD?

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 I insert an image into R?

To add a picture to a plot in base R, we first need to read the picture in the appropriate format and then rasterImage function can be used. The most commonly used format for picture in R is PNG. A picture in PNG format can be added to a plot by supplying the values in the plot where we want to add the picture.

How do I embed a plot in R markdown?

You can embed an R code chunk like this: ```{r} summary(cars) ``` You can also embed plots, for example: ```{r, echo=FALSE} plot(cars) ``` Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.


1 Answers

When a figure is not generated from R code, you may use knitr::include_graphics() to insert it to the document, e.g.

```{r echo=FALSE, out.width='100%'} knitr::include_graphics('./cropped-banner_efpt.jpg') ``` 
like image 52
Yihui Xie Avatar answered Oct 17 '22 14:10

Yihui Xie