-
-
Save barryrowlingson/ace591e7625c377fbe4b to your computer and use it in GitHub Desktop.
hooks for knitr when I wrote my crime.html spatial stats tutorial. The main hook puts the code and image into separate divs. Written a couple of years ago as part of a more complex build system...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hook=list() | |
hook$plot = function(x,options){ | |
sprintf("\n[[[%s]]]\n",knitr:::.upload.url(x)) | |
} | |
imgmodal <- function(url,name){ | |
sprintf('<a data-toggle="modal" href="#img-%s" > | |
<img src="%s" class="rplot" /> | |
</a> | |
<div class="modal hide fade" id="img-%s"> | |
<button class="close" data-dismiss="modal">×</button> | |
<img src="%s" /> | |
<a href="" class="btn btn-primary" data-dismiss="modal">Close</a> | |
</div> ',name,url,name,url) | |
} | |
hook$chunk= | |
function (x, options) | |
{ | |
if (knitr:::output_asis(x, options)) | |
return(x) | |
## find the images | |
lines = strsplit(x,"\n")[[1]] | |
gotImage = grep("^\\[\\[\\[(.*)\\]\\]\\]$",lines) | |
if(length(gotImage)>0){ | |
figs = lines[gotImage] | |
lines = lines[-gotImage] | |
urls = substring(figs,4,nchar(figs)-3) | |
imgs = imgmodal(urls,options$label) #sprintf("<img src=\"%s\" />",urls) | |
imgdiv = sprintf("<div class=\"rimage\">%s</div>",paste(imgs,collapse="")) | |
tclass="transcript half" | |
}else{ | |
imgdiv="" | |
tclass="transcript" # full width transcript | |
} | |
transcript = sprintf("<div class=\"%s\">%s</div>\n",tclass,paste(lines,collapse="\n")) | |
x = sprintf("<div id=\"%s\" class=\"chunk\">%s</div>", options$label, paste(transcript,imgdiv)) | |
if (options$split) { | |
cat("split...\n") | |
name = fig_path(".html", options) | |
if (!file.exists(dirname(name))) | |
dir.create(dirname(name)) | |
cat(x, file = name) | |
sprintf("<iframe src=\"%s\" class=\"knitr\" width=\"100%%\"></iframe>", | |
name) | |
} | |
else x | |
} | |
render_html() | |
knit_hooks$set(hook) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment