Adding a new language to knitr, with variable robustness

I would like to expand knitr

to a new language or more than one. Mimicking the source code for the function eng_go

, here , produces code like this that adds, for example, support for CSS CLClick.

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 this in a piece of code in a file .Rmd

, and the very next piece of code can be of type cs

and will be interpreted as I expect.

My question is this: is there any documentation on extending this basic functionality with new features?

Specifically, I would like to know how to make the variables persist in chunks (processing all the chunks at once?) And adjust the formatting of the output based on the type of the resulting object.

+3


source to share





All Articles