insert side by side png images using knitr

后端 未结 3 899
花落未央
花落未央 2020-12-14 06:42

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=F         


        
相关标签:
3条回答
  • 2020-12-14 06:52

    We still lack a good answer to this question if the desired output is a MS Word document (I see that the OP specifically asked for HTML output, but I'm guessing I'm not the only one who came here looking for a solution that works for MS Word docs also).

    Here's one method, based on this and this, but the result is not very satisfactory:

    library(png)
    library(grid)
    library(gridExtra)
    img1 <-  rasterGrob(as.raster(readPNG("path/to/picture1.png")), interpolate = FALSE)
    img2 <-  rasterGrob(as.raster(readPNG("path/to/picture2.png")), interpolate = FALSE)
    grid.arrange(img1, img2, ncol = 2)
    
    0 讨论(0)
  • 2020-12-14 06:56

    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"))
    ``` 
    
    0 讨论(0)
  • 2020-12-14 07:01

    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).

    0 讨论(0)
提交回复
热议问题