Skip to content

Instantly share code, notes, and snippets.

@benwhalley
Last active March 5, 2018 14:23
Show Gist options
  • Save benwhalley/01a19626552fb0cdb6921bdcc754c4fe to your computer and use it in GitHub Desktop.
Save benwhalley/01a19626552fb0cdb6921bdcc754c4fe to your computer and use it in GitHub Desktop.
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment