Lesson 6. Add images to an R Markdown report

Learning objectives

After completing this tutorial, you will be able to:

  • Add an image to an R markdown report
  • Describe the ideal location to store an image associated with an R markdown report so that knitr can find it when it renders a file

What you need

You need R and RStudio to complete this tutorial. Also you should have an earth-analytics directory set up on your computer with a /data directory with it.

Add an image to your report

You can add images to an R Markdown report using markdown syntax as follows:

![alt text here](path-to-image-here)

However, when you knit the report, R will only be able to find your image if you have placed it in the right place - RELATIVE to your .Rmd file. This is where good file management becomes extremely important.

To make this simple, let’s set up a directory named images in your earth-analytics project / working directory. If your .Rmd file is located in the root of this directory and all images that you want to include in your report are located in the images directory within the earth-analytics directory, then the path that you would use for each image would look like:

images/week3/image-name-here.png

Let’s try it with an actual image.

![an image caption Source: Ultimate Funny Dog Videos Compilation 2013.](images/week3/silly-dog.png)

And here’s what that code does IF the image is in the right place:

an image caption Source: Ultimate Funny Dog Videos Compilation 2013.

If all of your images are in your images directory, then knitr will be able to easily find them. This also follows good file management practices because all of the images that you use in your report are contained within your project directory.

Leave a Comment