乳胶中的tkplot通过knitr和igraph

Tyl*_*ker 9 r igraph knitr tkplot

这可能是一个疯狂的奇怪梦想.我dreampt我可以把一个tkplotigraph通过乳胶文档中knitr.我知道Yihui知道动画的东西,所以我想也许这是可能的.谷歌搜索没有显示我之后的情况,所以这里是一个非工作尝试:

\documentclass[a4paper]{scrartcl}
\begin{document}

<<setup, include=FALSE, cache=FALSE>>=
library(igraph)
@

<<network>>=
edges <- structure(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "A", "B", "C", 
    "D", "E", "F", "G", "H", "I", "J", "E", "G", "G", "F", "H", "G", 
    "D", "J", "J", "D", "B", "C", "D", "I", "I", "H", "A", "B", "G", 
    "I", "F", "D", "F", "J", "D", "B", "E", "E", "A", "E"), .Dim = c(30L, 
    2L), .Dimnames = list(NULL, c("person", "choice")))

g <- graph.data.frame(edges, directed=TRUE)
tkplot(g)
@ 

\end{document}
Run Code Online (Sandbox Code Playgroud)

Yih*_*Xie 3

好吧,一个快速而肮脏的答案:

\documentclass{article}
\begin{document}

<<setup, include=FALSE, cache=FALSE>>=
library(igraph)
library(tcltk)
knit_hooks$set(igraph = function(before, options, envir) {
  if (before) return()
  path = knitr:::fig_path('.eps')
  tkpostscript(igraph:::.tkplot.get(options$igraph)$canvas,
                     file = path)
  sprintf('\\includegraphics{%s}', path)
})
@

<<network, igraph=1>>=
edges <- structure(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "A", "B", "C", 
    "D", "E", "F", "G", "H", "I", "J", "E", "G", "G", "F", "H", "G", 
    "D", "J", "J", "D", "B", "C", "D", "I", "I", "H", "A", "B", "G", 
    "I", "F", "D", "F", "J", "D", "B", "E", "E", "A", "E"), .Dim = c(30L, 
    2L), .Dimnames = list(NULL, c("person", "choice")))

g <- graph.data.frame(edges, directed=TRUE)
tkplot(g)
@ 

\end{document}
Run Code Online (Sandbox Code Playgroud)

随意用 打磨它hook_plot_custom