0
votes

I have a Markdown document that was generated using Knitr (literate programming). This markdown document gets converted to Microsoft Word (docx) and HTML using pandoc. Now I would like to include specific parts from the Markdown in HTML, and others in docx. The concrete use case is that I'm able to generate JS+HTML charts using rCharts which is fine for HTML, but obviously doesn't render in docx, so I would like to use a simple PNG image in that case. Is there some specific pandoc syntax or trick that I can use for this?

1

1 Answers

0
votes

So one way to solve this is to post-process the generated markdown from knitr.

I output some mustasche and then parse that using the R package whisker.

Roughly the code looks like:

md <- knit(rmd, envir=e)

docx.temp <- tempfile()
html.temp <- tempfile()

writeLines(whisker.render(readLines(md), list(html=T)), html.temp)
writeLines(whisker.render(readLines(md), list(html=F)), docx.temp)

docx <- pandoc(docx.temp, format="docx")
html <- pandoc(html.temp, format="html")

file.copy(docx, "./report.docx", overwrite=T)
file.copy(html, "./report.html", overwrite=T)

With the Rmd (knitr) containing something roughly like

{{^html}}
```{r}
WITHOUT HTML
```
{{/html}}

{{#html}}
```{r}
WITH HTML
```
{{/html}}