Display targets dependency graph in GitHub README

If a project exposes a targets pipeline as its main thing, it would be nice to include the dependency graph in the project's README. GitHub supports rendering mermaid diagrams in a mermaid code block, and targets supports rendering a mermaid diagram as output suitable for copy/pasting into a markdown file, but I can't figure out a way to do this automatically. I tried results='asis'as a chunk option, but that didn't achieve the desired results. Anyone have any ideas?

1 Like

Hi @11rchitwood,

I think the trick is to encase the mermaid graph code in a fenced code-block that indicates it is a mermaid graph.

Check out Static graphs with mermaid.js by wlandau · Pull Request #802 · ropensci/targets · GitHub for more information.

Basically, this should work:

---
output: github_document
---

```{r, results = "asis", echo = FALSE}
library(targets)
tar_script({
  list(
    tar_target(name = "data0", command = c(1L + 1L),),
    tar_target(name = "data1", command = seq_len(3L)),
    tar_target(name = "data2", command = seq_len(3L) + 3L),
    tar_target(name = "map1", command = data1 + sum(data0), pattern = map(data1)),
    tar_target(name = "map2", command = data1 + data2, pattern = map(data1, data2)),
    tar_target(name = "map3", command = map1 + 1L, pattern = map(map1)),
    tar_target( name = "map4", command = map1 + map2, pattern = map(map1, map2)),
    tar_target( name = "map5", command = map1 + data2, pattern = map(map1, data2)),
    tar_target(name = "map6", command = sum(map1) + sum(data2), pattern = map(data2))
  )
})
cat(c("```mermaid", tar_mermaid(), "```"), sep = "\n")
```

Note the last line, where you cat() the tar_mermaid() graph code into a fenced code block with a mermaid tag. You still need results = 'asis'.

Which renders to Markdown as:

``` mermaid
graph LR
  subgraph Legend
    outdated([Outdated]):::outdated --- stem([Stem]):::none
    stem([Stem]):::none --- pattern[Pattern]:::none
  end
  subgraph Graph
    data0([data0]):::outdated --> map1[map1]:::outdated
    data1([data1]):::outdated --> map1[map1]:::outdated
    data1([data1]):::outdated --> map2[map2]:::outdated
    data2([data2]):::outdated --> map2[map2]:::outdated
    data2([data2]):::outdated --> map3[map3]:::outdated
    data2([data2]):::outdated --> map4[map4]:::outdated
    map1[map1]:::outdated --> map4[map4]:::outdated
    map1[map1]:::outdated --> map5[map5]:::outdated
    map1[map1]:::outdated --> map5[map5]:::outdated
    map1[map1]:::outdated --> map6[map6]:::outdated
    map2[map2]:::outdated --> map6[map6]:::outdated
  end
  classDef outdated stroke:#000000,color:#000000,fill:#78B7C5;
  classDef none stroke:#000000,color:#000000,fill:#94a4ac;
  linkStyle 0 stroke-width:0px;
  linkStyle 1 stroke-width:0px;
```

Notice how the mermaid graph code is inside a mermaid code block. Hope this is helpful.

This works! And here's the repo to prove it. Thanks!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.