Using pure dot inside a knitr chunk with engine = "dot" is straightforward, but you have to write the dot code yourself.

<<r dot-ex,  engine = "dot",  echo=FALSE>>=
digraph test123 { 
          A -> B
}
@

I want a function to do it for me.

dotFun <- function() {
  dotCode <- 'digraph test123 { 
          A -> B
          }'
  return(dotCode)
}

and then call this inside a knit chunk similar to a function that returns LaTeX code and knit with result = 'as.is'

<<r dot-ex,  engine = "dot">>=
cat(dotFun())
@

but this results in :Error in (knit_engines$get(options$engine))(options): setting chunk option results = 'asis' yields the same error message.

Is there a way to do this?

有帮助吗?

解决方案

It is not possible with the current version of knitr (v1.5), but will be possible in the next version (v1.6), which has not been released yet. If you use the development version on Github, you can actually assign the source code to a code chunk via the code option, e.g.

<<dot-ex,  engine = "dot", code = dotFun()>>=
@

More on this in the news for v1.6.

其他提示

Any particular reason this has to be evaluated within a knitr chunk with that engine? Why not do it directly from R with some system calls? You can write the command to a file, and use system to call dot on that file, and read the results back into R.

This is, in fact, what knitr does. You can probably pretty easily take the knitr dot engine and wrap it into a function of your own -- see https://github.com/yihui/knitr/blob/master/R/engine.R#L144.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top