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

30,022

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')
```
Share:
30,022

Related videos on Youtube

r0berts
Author by

r0berts

I am a hobbyist in statistical programming and my main field is psychological medicine. I like the StackExchange community. What else - oh, yes, I am a linux fan and most computers I have run linux on them - Manjaro or Debian.

Updated on July 09, 2022

Comments

  • r0berts
    r0berts almost 2 years

    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.

  • r0berts
    r0berts about 8 years
    Thank you, this works very well. In tufte-handout template it goes to full width of the page only if you give option fig.fullwidth=TRUE in the header. But out.width='100%' option is also very useful to know. I too found another solution but that is with inserting latex code such as: \begin{figure*} \includegraphics{././cropped-banner_efpt.jpg} \end{figure*} In any case thanks a bunch and knitr is phenomenal!
  • Pablo Adames
    Pablo Adames about 4 years
    Works really well on R version 3.6.3 (2020-02-29) for x86_64-pc-linux-gnu (64-bit). Thanks!
  • dsz
    dsz over 2 years
    While considerably more verbose than ![...](...), this method give access to far more control in terms of placement and size, and can therefore be useful even for static images.