How to Import Local Image Using Knitr for Markdown

How to import local image using knitr for markdown

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

How to set size for local image using knitr for markdown?

You can also read the image using png package for example and plot it like a regular plot using grid.raster from the grid package.

```{r fig.width=1, fig.height=10,echo=FALSE}
library(png)
library(grid)
img <- readPNG("path/to/your/image")
grid.raster(img)
```

With this method you have full control of the size of you image.

Insert picture/table in R Markdown

Several sites provide reasonable cheat sheets or HOWTOs for tables and images. Top on my list are:

  • Pandoc readme, specifically tables

  • RStudio's RMarkdown, more details in basics (including tables) and a rewrite of pandoc's markdown.

Pictures are very simple to use but do not offer the ability to adjust the image to fit the page (see Update, below). To adjust the image properties (size, resolution, colors, border, etc), you'll need some form of image editor. I find I can do everything I need with one of ImageMagick, GIMP, or InkScape, all free and open source.

To add a picture, use:

![Caption for the picture.](/path/to/image.png)

I know pandoc supports PNG and JPG, which should meet most of your needs.

You do have control over image size if you are creating it in R (e.g., a plot). This can be done either directly in the command to create the image or, even better, via options if you are using knitr (highly recommended ... check out chunk options, specifically under Plots).

I strongly recommend perusing these tutorials; markdown is very handy and has many features most people don't use on a regular basis but really like once they learn it. (SO is not necessarily the best place to ask questions that are answered very directly in these tutorials.)


Update, 2019-Aug-31

Some time ago, pandoc incorporated "link_attributes" for images (apparently in 2015, with commit jgm/pandoc#244cd56). "Resizing images" can be done directly. For example:

![unchanged image](foo.jpg)
![much-smaller image](foo.jpg){#id .class width=30 height=20px}
![half-size image](foo.jpg){#id .class width=50% height=50%}

The dimensions can be provided with no units (pixels assumed), or with "px, cm, mm, in, inch and %" (ref: https://pandoc.org/MANUAL.html, search for link_attributes).

(I'm not certain that CommonMark has implemented this, though there was a lengthy discussion.)

Path to image in my package for R mark down

Given your file path BayesianXXX/inst/image/ZZZ.jpg

system.file("image", "ZZZ.jpg", package="BayesianXXX")

Should work as long as the package is installed on the system (and in one of the library locations from .libPaths()).

In the YYY.Rmd, you can then use inline code:

`r paste0("![](",system.file("image", "ZZZ.jpg", package="BayesianXXX"), ")")`

Or in a chunk using cat and results='asis'

add and resize a local image to a .Rmd file in RStudio that will produce a pdf

From @tmpname12345

You can use raw latex to include a figure in pdf_output: \includegraphics[width=250pt]{path/file.png}



Related Topics



Leave a reply



Submit