Skip to content

Instantly share code, notes, and snippets.

@zross
Last active May 4, 2016 16:20
Show Gist options
  • Save zross/6fe6c9d494646245a864 to your computer and use it in GitHub Desktop.
Save zross/6fe6c9d494646245a864 to your computer and use it in GitHub Desktop.
Not completely finished hook to add fragment tags to RMarkdown code chunks (slidify, revealjs, R)
s0 <- knitr::knit_hooks$get('source')
o0 <- knitr::knit_hooks$get('output')
p0 <- knitr::knit_hooks$get('plot')
knitr::knit_hooks$set(
list(
source=function(x,options){
if (is.null(options$class)) s0(x, options)
else
paste0("<pre class='", options$class, "'><code class='r'>",paste0(x, collapse="\n"))
},
output = function(x,options){
if (is.null(options$class)) o0(x, options)
else
print(x)
paste0('\n',x,'</code></pre>')
},
# plot = function(x, options){
# print(p0(x, options))
# },
chunk=function(x,options){
print(x)
if (is.null(options$class)){
return(x)
}else{
# if we already have closing tags (from the output) then we're done
if(grepl('</code></pre>',x)==1){return(x)}
# if not we add them here
if(grepl('</code></pre>',x)!=1){return(paste0(x,'</code></pre>'))}
}
# }else{
# #if no image and closing tags exist
# if(grepl("!\\[", x)!=1 & grepl('</code></pre>',x)==1){return(x)}
# #if image and no closing tags exist
# if(grepl("!\\[", x)==1 & grepl('</code></pre>',x)!=1){
# x<-gsub("!\\[", '</code></pre>\n![',x)
#
# return(x)
# }
#
#
# }
return(x)
}
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment