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

Time:04-05

Instead of having to include the relative path to an image like so:

![\label{fig:R1amb}R1 Ambient RNA Contamination](../outs/pre-processing/ambientRNA_R1.png)

I would like to bring in an image from the yaml object like so:

![\label{fig:R1amb}R1 Ambient RNA Contamination](`r yaml$inputs$R1_ambient`)

where yaml$inputs$R1_ambient contains the full path to the png. However, this does not render the image. Why is that?

CodePudding user response:

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)
```
  • Related