Search code examples
rmarkdownknitrslidify

How to deal with double braces in code chunk?


I am writing Rmd files introducing the whisker package. And use slidify to compile it to html slides.

Therefore it is inevitable to use {{ and }} in the code. I wrote the following code in the Rmd file:

```{r}
tpl <- "
<b>Against:</b> {{x.against}}<br/>
<b>Venue:</b> {{x.venue}}<br/>
<b>Match:</b> {{x.type}}<br/>
<b>Score:</b> {{x.score}}<br/>
<b>Result:</b> <span class={{x.result}}>{{x.result}}</span><br/>
{{#x.sr}}<b>Strike Rate:</b> {{x.sr}} {{/x.sr}}
"
```

But in the parsed html file, the braces and the content inside are ignored:

<pre><code class="r">tpl &lt;- &quot;
&lt;b&gt;Against:&lt;/b&gt; &lt;br/&gt;
&lt;b&gt;Venue:&lt;/b&gt; &lt;br/&gt;
&lt;b&gt;Match:&lt;/b&gt; &lt;br/&gt;
&lt;b&gt;Score:&lt;/b&gt; &lt;br/&gt;
&lt;b&gt;Result:&lt;/b&gt; &lt;span class=&gt;&lt;/span&gt;&lt;br/&gt;

&quot;
</code></pre>

It is not convenient to directly write html in the Rmd file for codes. The option results='asis' in knitr is only affecting the output but not the code chunk.

Is there any solution to deal with {{ and }} in the code chunk? Thanks.


Solution

  • Slidify expands all mustache tags by default. There is an undocumented feature that will allow you to pass these tags untouched. Here is a simple example

    --- .RAW
    
    Template
    
    ```{r}
    {{ myvariable }}
    ```
    

    So add the .RAW class property to the slide where you want this behavior.