Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Image in R Markdown Template Without Having to Create a New Directory for Template

I’m building a beamer-presentation template, and I would like to include a logo on the front of the slides. Although this could be achieved by including an image in the directory of the presentation, I would prefer not to have to create a new directory for each new presentation just for that one image.

Is there a way where I can retrieve the relative file path from within the package resources folder and have it reference that location in the LaTeX beamer template?

I have tried placing the image in the resources folder along with the .tex template but when I try to knit it I get a file not found error.

like image 297
dylanjm Avatar asked Feb 21 '18 23:02

dylanjm


People also ask

How do I embed an image in R Markdown?

To add an image in markdown you must stop text editing, and you do this with the command [Alt text] precedeed by a ! Then you have to add the path to the image in brackets. The path to the image is the path from your directory to the image.

How do I create a custom R Markdown template?

You can create a template for every homework assignment or exercise that you want your students to work through. And they can access all of these templates easily when they go to File > New File > R Markdown . And then select From Template from the dialogue box menu. Your custom R Markdown template will appear there.

What are the three types of sections in an R Markdown document?

There are three basic components of an R Markdown document: the metadata, text, and code.


Video Answer


2 Answers

I managed to do this with kind of a workaround:

I derive the absolute path of the resource folder in the yaml header with an R function:

---
output:
  myPackage::myCustomFunction
resource-folder: '`r system.file("rmarkdown/templates/myPackage/resources/", package="myPackage")`'
---

Inside my tex tamplate I use:

\includegraphics[]{$resource-folder$myImage.png}

This way I can place my images in the resource folder along with the tex template and don't have to copy them along with each new instance of the document.

like image 75
nnn Avatar answered Sep 26 '22 06:09

nnn


This question has two key problems:

  1. Have an easy way to provide a file path to an image, regardless of where the R Markdown file is.
  2. Make it so that the logo does not have to be specified in the YAML each time, as seen in the solution by nnn

Overall solution

As this problem requires a package to be properly resolved, I have put together a really basic repository here, which you may want to fork. Alternatively you can download it here:

devtools::install_github("mikey-harper/rmarkdown-image")

Custom Function

It is useful to create your own R Markdown output for two reasons:

  1. You can link to system files really easily. In this case, you want to have a reference to your logo.
  2. You can provide default pandoc arguments, rather than having them in the YAML.

These two points can be used in tandem to provide the logo file (which is stored in the package) directly to the template titlegraphic YAML option. Here is the basic function from the package.

beamer_custom <- function(...){

  # Define filepaths
  logo <- system.file(package = "template", "logo.png")
  template <- system.file(package = "template", "template.tex")

  # supply files to your custom format
  rmarkdown::beamer_presentation(...,
                                 template = template,
                                 pandoc_args = rmarkdown::pandoc_variable_arg("titlegraphic", logo))
}

Note that the ... in the function means that we can supply any arguments to our new function which will be passed directly to the beamer_presentation function.

Custom Template

Defining your own template isn't entirely necessary here, as the default template includes a lot of customisation options for beamer. I only made a single change to the template, and this was to force the logo size to be 2cm tall. I therefore added [height=2cm] to line 338:

\titlegraphic{\includegraphics[height=2cm]{$titlegraphic$}}

Using this in a template. Some additional options have been added to the output (theme, colortheme, fonttheme) to highlight that it is still easy to pass other arguments to our new function.

---
title: "R Markdown"
date: \today
author: Michael Harper
subtitle: How to make awesome R Markdown presentation
output: 
  template::beamer_custom:
    theme: "AnnArbor"
    colortheme: "dolphin"
    fonttheme: "structurebold"
---


# Text

Beautiful text

enter image description here

You probably want to read Chapters 17 and 18 of the R Markdown book if the concept of making Custom formats sounds intimidating!

like image 42
Michael Harper Avatar answered Sep 25 '22 06:09

Michael Harper