I'd like to extend knitr with a new language, or more than one. Imitating the source code for the eng_go function, here, yields code like this, which adds CoffeeScript CLI support, for example.
knitr::knit_engines$set( cs = function ( options ) {
f = tempfile( 'code', '.', fileext = '.coffee' )
writeLines( c(
'console.log try',
paste0( ' ', options$code ),
'catch error then error.message'
), f )
on.exit( unlink( f ), add = TRUE )
cmd = Sys.which( 'coffee' )
extra = if ( options$eval ) {
args <- paste0( ' ', f )
message( 'running: ', cmd, args )
tryCatch(
system2( cmd, args, stdout = TRUE, stderr = TRUE, env = options$engine.env ),
error = function ( e ) {
if ( !options$error ) stop( e )
'Error in executing CoffeeScript code'
}
)
}
if ( options$results == 'hide' ) extra = NULL
knitr::engine_output( options, options$code, extra )
} )
You can put that in a code chunk in an .Rmd file, and the very next code chunk can be of type cs, and will be interpreted as I intend.
My question is this: Is there any documentation on extending that basic functionality with new features?
In particular, I'd like to know how to make variables persist across chunks (by processing all chunks at once??) and to customize output formatting based on the type of the resulting object.