3

I'm trying to embed a static image of a targets workflow in an rmarkdown document. I tried to do this by using tar_mermaid, defining a target that writes the workflow in mermaid format mm <- tar_mermaid(); writeLines(mm, "target_mermaid.js") but the help for tar_mermaid says

You can visualize the graph by copying the text into a public online mermaid.js editor or a mermaid GitHub code chunk

I am looking for a programmatic way to either (1) embed the Javascript output in an (R)markdown file, or (2) render it (as SVG, PNG, whatever).

I thought as a shortcut that I could cut-and-paste into a markdown code chunk delimited by ```mermaid, or use cat(readLines("target_mermaid.js"), sep = "\n") in a chunk with results = "asis" but I guess that only works in Github markdown (I'm using Pandoc to render to HTML) ... ?

The visNetwork package has a visSave() function which can save to HTML (not quite what I wanted but better than what I've managed so far), and a visExport() function (which saves to PNG etc. but only by clicking in a web browser). Furthermore, targets wraps the visNetwork functions in a way that is (so far) hard for me to unravel (i.e., it doesn't return a visNetwork object, but automatically returns a widget ...)

For the time being I can go to https://mermaid.live, paste in the mermaid code, and export the PNG manually but I really want to do it programmatically (i.e. as part of my workflow, without manual steps involved).

enter image description here

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453

2 Answers2

2

I am not quite sure about the answer. But I have an idea. And I will delete if it is not adequate:

If you want execute mermaid code to get for example an html output then you could do this with quarto. I am not sure if this is possible with rmarkdown:

See https://quarto.org/docs/authoring/diagrams.htmlS

---
title: "Untitled"
format: html
editor: visual
---

## Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.

## Running Code

    ```{mermaid}
    flowchart LR
      A[Hard edge] --> B(Round edge)
      B --> C{Decision}
      C --> D[Result one]
      C --> E[Result two]
    ```

output: enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 1
    I appreciate the answer, but I don't think this is quite going to work because the *content of the chunk itself is programatically generated*. Of course I can use another step to do that (e.g. using `sed`/`cat` whatever to embed the generated mermaid code in the text) but I was hoping to avoid that ... (since it's *not* raw markdown, using a code chunk with "asis" doesn't work ... (indeed it doesn't work in rmarkdown, I wouldn't object to switching to Quarto for this if I could get it to work ...) – Ben Bolker Jul 29 '22 at 19:07
  • 1
    `knitr` can generate a `mermaid.js` code chunk with this method: https://github.com/ropensci/targets/pull/802. It's an R code chunk with `results = "asis"` which outputs the lines of text for the mermaid.js code chunk. – landau Jul 29 '22 at 20:27
1

@landau's suggestion to look here almost works, if I'm willing to use Quarto instead of Rmarkdown (GH Markdown is not an option). The cat() trick was the main thing I was missing. The .qmd file below gets most of the way there but has the following (cosmetic) issues:

  • I don't know how to suppress the tidyverse startup messages, because targets is running the visualization code in a separate R instance that the user has (AFAIK) little control of;
  • the default size of the graph is ugly.

Any further advice would be welcome ...

---
title: "targets/quarto/mermaid example"
---

```{r}
suppressPackageStartupMessages(library("tidyverse"))
library("targets")
```

```{r, results = "asis", echo = FALSE}
cat(c("```{mermaid}", tar_mermaid(), "```"), sep = "\n")
```

Beginning of document:


image of Quarto output, showing code chunks and tidyverse startup messages

Zooming out:

full view of Quarto output, showing an awkwardly sized corner of the targets network

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453