# DOT in Rmarkdown?

Is it possible to use dot as the language of an Rmarkdown chunk? If so, are there any examples? Thx.

Haven't used it myself, but see this post and the CRAN Task view

Actually, I find 'dot' is one of the engines which 'knitr' support.

library(magrittr)
#> Warning: 程辑包'magrittr'是用R版本3.6.1 来建造的
names(knitr::knit_engines\$get()) %>%
sort()
#>  [1] "asis"      "asy"       "awk"       "bash"      "block"     "block2"
#>  [7] "c"         "cat"       "coffee"    "css"       "dot"       "fortran"
#> [13] "fortran95" "gawk"      "go"        "groovy"    "haskell"   "highlight"
#> [19] "js"        "julia"     "lein"      "mysql"     "node"      "octave"
#> [25] "perl"      "psql"      "python"    "Rcpp"      "Rscript"   "ruby"
#> [31] "sas"       "sass"      "scala"     "scss"      "sed"       "sh"
#> [37] "sql"       "stan"      "stata"     "tikz"      "zsh"

Created on 2020-02-02 by the reprex package (v0.3.0)

However, I don't find any source to run dot just like python, Julia.
I think there is another way to do it.
Use the chunk to preview dot code and use another r chunk to display the dot output.

{cat, engine.opts=list(file = 'sample.dot')}
digraph course {
rankdir = LR
node [shape = box, style=filled]
layout = dot
compound =true
#color = crimson

a -> b
c -> d
}


{r}
DiagrammeR::grViz("sample.dot")


The temporal file "sample.dot" is optional, you can use a temp file by fs::path_temp.
Here, 'cat' is also one of the engines 'knitr' supports. Here are the notes to introduce it.

2 Likes

Hi @joxborrow I find the solution on a @yihui 's post.
See the example 'dot.Rmd' host on GitHub.

Here, the code is written in RMarkdown.

# A dot example

A dot-example taken from the man-pages. You need to install the graphviz package.

{r dot-ex, engine = "dot", fig.cap = "Funky dot", cache=TRUE}
digraph test123 {
a -> b -> c;
a -> {x y};
b [shape=box];
c [label="hello\nworld",color=blue,fontsize=24,
fontname="Palatino-Italic",fontcolor=red,style=filled];
a -> z [label="hi", weight=100];
x -> z [label="multi-line\nlabel"];
edge [style=dashed,color=red];
b -> x;
{rank=same; b x}
}


The output is here https://github.com/yihui/knitr-examples/blob/master/057-engine-dot.md

5 Likes

That is great. Thanks so much for this. That is a handy little post!

About graph-like content in Rmarkdown, you have also UML diagrams and the new nomnoml which as a knit engine included.

It is not DOT I think but in case someone coming to this post is interesting to UML, it could be useful.

BTW, If your question's been answered, would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

1 Like

One follow-up question, where do I install the graphviz package from? Is it on cran? I am having some issues installing. I am on windows.

For this feature of knitr engine, you need to have the Graphiz tools installed. dot is one of them.
This is not R related, it is external tools for Graph.

It should be set in the PATH afterward so that it is found by default. Otherwise you could use the engine.path knitr chunk option.

1 Like

This solution worked great on linux, but I am having some issues getting it to work on Windows. I used the following code, but get a "Error in system(command, as.integer(flag), f, stdout, stderr, timeout) character string expected as first argument" error. Any ideas?

`{r dot-ex, engine = "dot", engine.path="C:\\Program Files (x86)\\Graphviz2.38\\bin\\dot.exe", fig.cap = "Funky dot", cache=TRUE}
digraph test123 {
a -> b -> c;
a -> {x y};
b [shape=box];
c [label="hello\nworld",color=blue,fontsize=24,fontname="Palatino-Italic",fontcolor=red,style=filled];
a -> z [label="hi", weight=100];
x -> z [label="multi-line\nlabel"];
edge [style=dashed,color=red];
b -> x;
{rank=same; b x}
}

I try the dot script, it is right. So I think dot is occupied by another tool set in the environment variables. Would you like to print all directory containing dot.exe in your system? And see whether some is set in the environment variables.

@joxborrow can you confirm your error happens only when running the chunk inside the IDE by clicking on the green arrow ?
Is this working on Wndows when you render the all Rmd Document ? The document renders correctly for me.

I can reproduce your error on WINDOWS only when trying to run the chunk from the IDE "run current chunk" button ?

I am waiting for your confirmation before digging more, but I think there could be an issue with the IDE regarding this engine. Thanks !

Yep. That is the issue I am having as well. It did run correctly when running the entire Rmd. Just running the individual chunk has an issue. Thanks for your help!

1 Like

Thanks for the confirmation. I think there is a bug report to open in the IDE side for the "Run current chunk" feature to work as expected in the IDE;

1 Like

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