Using CoffeeScript with knitr

First we set a flag -p for the coffee engine to print the JavaScript output instead of evaluating the code (engine.opts = '-p'):

knitr::opts_chunk$set(cache = TRUE, engine.opts = '-p')

You need to install coffee, the command-line version of CoffeeScript.

CoffeeScript compiles javascript:

@square = (x) -> x * x

To use CoffeeScript in an HTML document, use results="asis", echo=FALSE, and wrap the chunk in <script> tags.



Of course you can also run the code, if you remove the -p flag from the chunk option engine.opts (I'm not evaluating this code chunk here because I do not have a proper version of coffee on Debian yet; if you do, you can remove eval=FALSE):

x = 42
console.log "The answer is ", x


Try the parsermd package in your browser

Any scripts or data that you put into this service are public.

parsermd documentation built on May 20, 2021, 5:08 p.m.