Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add javascript code in knitr

How do I add javascript code to a Rmd?

I'm using the following chunk options:

```{r engine = 'js', results='asis', echo = T, highlight = T}
var i = 1 + 1;
``` 

And this produces a .md with:

<script type="text/javascript">
var i = 1 + 1;
</script>

I would like to have something like:

```js
var i = 1 + 1;
```

<script type="text/javascript">
var i = 1 + 1;
</script>

In other words, I don't need the code to be evaluated when knitting, I just need that highlighted code appears in the output.

like image 255
Daniel Falbel Avatar asked May 31 '26 19:05

Daniel Falbel


1 Answers

As of knitr v1.16.5, this is the default behavior, i.e. the JS code will be echoed by default. This will work out of the box:

```{js}
var i = 1 + 1;
``` 
like image 174
Yihui Xie Avatar answered Jun 03 '26 09:06

Yihui Xie