3

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?

CelineDion
  • 906
  • 5
  • 21

1 Answers1

2

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)
```
starja
  • 9,887
  • 1
  • 13
  • 28
  • I see, it didn't occur to me to define variables in the header like that (beyond a few . I was reading in a separate file and assigning it to `yaml`. That could still work potentially. We will see. Thank you for your help. – CelineDion Apr 04 '22 at 18:23
  • I see. I guess it shouldn't be a problem to use your approach instead with `include_graphics`, however the yaml parameters are exactly thought for this usecase – starja Apr 05 '22 at 09:36