Insert Picture/Table in R Markdown

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

How To Insert Images Into Table in R Markdown

You can do this with the knitr and pander packages. Car picture taken from here: https://car-from-uk.com/sale.php?id=55162&country=us; renamed to "rx4.jpg" in my working directory.

Code chunk in rmarkdown doc:

library(knitr)
library(dplyr)
library(pander)

mtcars %>%
slice(1) %>%
mutate(
pic = "rx4.jpg" %>% pander::pandoc.image.return()
) %>%
pander()

Produces this output:

Sample Image

Use image as table in RMarkdown?

Looks like I got to a way to do it with kable with some inspiration...this still has two horizontal lines but that's ok for the moment:

```{r echo=F, warning=F}
temp.df = data.frame(image="![](mytable.png)")
temp.mat <- as.matrix(temp.df)
colnames(temp.mat) <- NULL
knitr::kable(temp.mat, caption="This is my caption")

```

Edit: as suggested by @HoneyBuddha, when using Bookdown, you must add the flag format="pandoc", i.e. knitr::kable(temp.mat, caption="This is my caption", format="pandoc")

Add an image to a table-like output in R

If you are using knitr with the rmarkdown package, it is pretty easy -- just use the Markdown syntax ![]() to include images, e.g.

---
title: "Flags"
author: "Yihui Xie"
date: "2014/08/03"
output: html_document
---

```{r results='asis'}
dat <- data.frame(
country = c('Canada', 'United Kindom'),
abbr = c('ca', 'gb'),
var1 = c(1, 2),
var2 = rnorm(2)
)
dat$flag <- sprintf('![](http://flagpedia.net/data/flags/mini/%s.png)', dat$abbr)
library(knitr)
kable(dat)
```

knitr, tables, and flags

If you need LaTeX/PDF output, you have to download these images by yourself. Here is an example:

---
title: "Flags"
author: "Yihui Xie"
date: "2014/08/03"
output: html_document
---

```{r}
dat <- data.frame(
country = c('Canada', 'United Kindom'),
abbr = c('ca', 'gb'),
var1 = c(1, 2),
var2 = rnorm(2)
)
dat$file <- paste0(dat$abbr, '.png')
dat$link <- paste0('http://flagpedia.net/data/flags/mini/', dat$file)
dat$flag <- sprintf('![](%s)', dat$file)
for (i in seq_len(nrow(dat))) {
if (!file.exists(dat$file[i])) xfun::download_file(dat$link[i])
}
knitr::kable(dat[, -c(5, 6)])
```

How do I insert externally-generated images into R markdown programatically?

I'm not sure which flavour of Rmarkdown you're using (so not sure about yaml$inputs and the label cross-reference). However, my solution should work nevertheless.

You can use knitr::include_graphics in an R chunk to make use of the parameterised path:

---
title: "Test"
date: "4/4/2022"
output: html_document
params:
R1_ambient: "test.png"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Plot
```{r R1amb, echo = FALSE, fig.cap = "R1 Ambient RNA Contamination"}
knitr::include_graphics(params$R1_ambient)
```

Add image on title area of a table with gt package in R

Here you go:

gt(mtcars) %>% tab_header(title = md("<img src='https://i.stack.imgur.com/UyMtM.png' style='height:30px;'> **2014 - 2019 Salary and Playoff Appearances**⚽"))

embed png in rmarkdown table

  1. It's always good to start with some reproducible example:

    df <- data.frame(name = c('bicycle', 'binoculars', 'globe'))
    df$url <- paste0('http://fa2png.io/static/images/',
    df$name, '_000000_64.png')
  2. Call pander::pandoc.image to render image markup from the above URLs in markdown:

    library(pander)
    df$url <- sapply(df$url, pandoc.image.return)
  3. Render the markdown table:

    pander(df)

Resulting in the following table:

-----------------------------------------------------------------------
name url
---------- ------------------------------------------------------------
bicycle ![](http://fa2png.io/static/images/bicycle_000000_64.png)

binoculars ![](http://fa2png.io/static/images/binoculars_000000_64.png)

globe ![](http://fa2png.io/static/images/globe_000000_64.png)
-----------------------------------------------------------------------

That can be converted to HTML or whatever other format is required by e.g. pandoc:

pandoc -t html

Sample Image



Related Topics



Leave a reply



Submit