-
Notifications
You must be signed in to change notification settings - Fork 9
/
diagram.R
34 lines (28 loc) · 873 Bytes
/
diagram.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
library(DiagrammeR)
library(DiagrammeRsvg)
knit_gv <- function(code, filename=NULL, width=NULL, height=800){
assetpath <- "assets"
dir.create(assetpath, showWarnings = F)
if (!is.character(knitr::current_input())){
format <- "png"
} else {
if (stringr::str_detect(
knitr::opts_knit$get("rmarkdown.pandoc.to"), "beamer|latex|pdf")) {
format <- "pdf"
} else {
format <- "png"
}
}
if (!is.character(filename)){
filename <- file.path(assetpath, paste0(sample(1e6:1e7, 1), ".", format))
} else {
filename = paste0(filename, ".", format)
}
library('rsvg')
outfun <- get(paste0("rsvg_", format))
capture.output({
g <- grViz(paste("digraph{", code, "}"))
DiagrammeRsvg::export_svg(g) %>% charToRaw %>% outfun(filename, width=width, height=height)
}, file='NUL')
knitr::include_graphics(filename)
}