How to Include a Local Image File in an R Presentation

Jesse John Feb 02, 2024
  1. Include a Local Image File in an R Presentation
  2. Conclusion
How to Include a Local Image File in an R Presentation

This article will teach us how to link to a local image in an R Presentation. In addition, we will also see how to align and size the image.

Include a Local Image File in an R Presentation

We will create the presentation in R Studio. This will help us easily knit the RMarkdown file to an R Presentation.

We need to install the rmarkdown package if it is not already installed. The command to install the package is:

install.packages("rmarkdown")

RMarkdown allows us to create HTML presentations in two formats: ioslides and slidy. Inserting images is identical in both formats.

From version v2022.07, R Studio also allows us to create a Quarto presentation in Reveal JS format. This is also an HTML presentation, and inserting images is identical.

First, place the image in the same folder as the Rmd file of the presentation. Then, the code to link to the image is:

![Alt Text or Image Caption](Image_Filename.png)

Even if the folder with the Rmd file is not the working directory of R, R Studio finds the image.

By default, RMarkdown embeds images in the rendered HTML file using data: URIs. This makes the rendered HTML file self-contained.

It may be shared without providing the image files separately. However, Quarto presentations are not self-contained by default.

Sample Presentation

The first code chunk shows a sample RMarkdown presentation; it will be saved as an Rmd file. It includes the YAML header for the presentation.

The second code chunk shows a sample CSS file which is only used to align or resize the image. This code chunk must be saved as a CSS file in the same folder as the Rmd file; CSS has many other possibilities besides the styles used here.

The sample image that we will use is the following. It must be saved in the same folder.

Sample Image

When we knit an RMarkdown presentation, R Studio creates a self-contained HTML presentation file with the image embedded. It can be shared or used on any other computer with a modern browser.

Sample R Markdown Presentation:

---
title: "R HTML Presentation"
output:
  ioslides_presentation:
    css: RImage.css
---

## Slide with Centered Image

![Image Caption](RImage.png){.center}

<p style="text-align:center;">Centered Text</p>

## Slide with Left-aligned Image

![](RImage.png){.left}

## Slide with Right Aligned Image

![](RImage.png){.right}

## Slide with Right-aligned Small-sized Image

![](RImage.png){.small .right}

Sample CSS File:

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

.left {
  float:left;
}

.right {
  float:right;
}

.small {
  max-width: 25%;
}

Conclusion

The syntax to include an image in an R Markdown presentation is very simple. Using CSS, we can control the size and position of the image on the slide.

Author: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.