Created
March 24, 2015 17:59
-
-
Save srvanderplas/68d73ee4c6fdead4ed32 to your computer and use it in GitHub Desktop.
Knitr plot fragment hook for reveal.js
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
c0 <- knitr::knit_hooks$get('chunk') | |
knitr::knit_hooks$set( | |
list( | |
chunk=function(x,options){ | |
if(is.null(options$class) & is.null(options$fragIndex)){ | |
c0(x, options) | |
} else if(is.null(options$fragIndex)) { | |
classidx <- which(names(options)=="class") | |
paste0( | |
paste0("<span class='", options$class, "'>"), | |
c0(x, options[-classidx]), | |
'</span>\n' | |
) | |
} | |
else if(length(options$fragIndex)==1) { | |
classidx <- which(names(options)%in%c("class", "fragIndex")) | |
str_replace( | |
paste0( | |
paste0("<span class='", options$class, "' data-fragment-index=", options$fragIndex,">"), | |
c0(x, options[-classidx]), | |
'</span>\n' | |
), | |
"<div class=\"chunk\" id=\"(.*?)\">\\s*<div class=\"rimage default\">(.*)</div>\\s*</div>", | |
"\\2" | |
) | |
} else { | |
classidx <- which(names(options)%in%c("class", "fragIndex")) | |
str_replace( | |
paste0( | |
paste0("<span class='", options$class, " fade-in' data-fragment-index=", options$fragIndex[1],">"), | |
paste0( | |
paste0("<span class='", options$class, " fade-out' data-fragment-index=", options$fragIndex[2],">"), | |
c0(x, options[-classidx]), | |
'</span>\n' | |
), | |
'</span>\n' | |
), | |
"<div class=\"chunk\" id=\"(.*?)\">\\s*<div class=\"rimage default\">(.*)</div>\\s*</div>", | |
"\\2" | |
) | |
} | |
} | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment