How can I insert side by side png files from my computer into rstudio when creating an html document?
The following works well (plots)
```{r, echo=FALSE,fig.width=4, fig.show='hold'} plot(cars) plot(rnorm(100)) ```
But for images from a path, only the last image is displayed
```{r fig.width=3, fig.show='hold'} library(png) img <- readPNG("C:/path to my picture/picture.png") grid.raster(img) img2 <- readPNG("C:/path to my picture/picture2.png") grid.raster(img2) ```
In a . Rmd document there are several ways to produce figures side-by-side. If the figures already exist, the easiest way for me is to use knitr::include_graphics(c(fig1, fig2, ...)) [Thx: Yihui!]
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.
When you run render , R Markdown feeds the . Rmd file to knitr, which executes all of the code chunks and creates a new markdown (. md) document which includes the code and its output. The markdown file generated by knitr is then processed by pandoc which is responsible for creating the finished format.
knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.
You can use knitr::include_graphics()
as this one accepts a vector of paths as an argument.
Then you should use fig.show='hold',fig.align='center'
in order to plot them on the same line and out.width="49%", out.height="20%"
to control the output size.
```{r, echo=FALSE,out.width="49%", out.height="20%",fig.cap="caption",fig.show='hold',fig.align='center'} knitr::include_graphics(c("path/to/img1","path/to/img1")) ```
You should learn the syntax of Markdown (really, you need about five minutes). The solution does not even involve R at all:
![](path/to/picture.png) ![](path/to/picture2.png)
BTW, you'd better avoid absolute paths. Use relative paths (relative to your Rmd file).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With