R Markdown to ConTeXt question

I am converting R Markdown to ConTeXt using R Markdown's render function and running into a bit of a problem. I'd like to be able to treat the R Markdown document as a ConTeXt document with R code embedded in it, similar to how knitr allows one to embed R code in a LaTeX file. I can ALMOST do this, except for the following issue: whenever an R code chunk is inside any sort of \startxxx ... \stopxxx environment, it doesn't get converted to ConTeXt properly in the rendering process.

Here's a minimal working example/reprex.

The following file temp1.Rmd compiles to ConTeXt correctly with rmarkdown::render("temp1.Rmd", rmarkdown::context_document(ext=".tex"), clean=FALSE):

```{r, echo=FALSE}
plot(rnorm(20))
```

The intermediate Markdown code that is produced is:

![](temp1_files/figure-context/unnamed-chunk-5-1.pdf)<!-- -->

The final ConTeXt code that is produced is:

{\externalfigure[temp1_files/figure-context/unnamed-chunk-5-1.pdf]}

This compiles correctly in ConTeXt.

However, the following file temp2.Rmd does not compile to ConTeXt correctly with rmarkdown::render("temp2.Rmd", rmarkdown::context_document(ext=".tex"), clean=FALSE):

\startalignment[center]
```{r, echo=FALSE}
plot(rnorm(20))
```
\stopalignment

The intermediate Markdown code that is produced is, as expected:

\startalignment[center]
![](temp2_files/figure-context/unnamed-chunk-6-1.pdf)<!-- -->
\stopalignment

But the final ConTeXt code that is produced is:

\startalignment[center]
![](temp2_files/figure-context/unnamed-chunk-6-1.pdf)<!-- -->
\stopalignment

This of course does not generate the desired graphic in ConTeXt.

As far as I can tell, this is a general issue: whenever an R code chunk occurs within a \startxxx ... \stopxxx environment (including \starttext ... \stoptext, which is obviously a problem), it gets to the intermediate Markdown file okay, but then it doesn't compile correctly to ConTeXt from there. The problem does not occur for inline R code, which compiles correctly wherever it is. That problem also does not occur if the \startxxx ... \stopxxx environment is included in a pandoc template (rather than in the R Markdown file itself), but that isn't sufficient for my purposes.

In short, what I'd like to do is be able to include R code chunks (not just inline R code) anywhere in an R Markdown file, including inside \startxxx ... \stopxxx enviornments. I'd appreciate any help that anyone can offer me on this.

1 Like

This is pandoc issue here, or a pandoc behavior not an issue.

\startalignement and \stopalignement are not markdown so Pandoc is seing that and know how to deal with it. This is because raw_tex is activated, and that is why those two commands are not transformed, and not seen as markdown. But it will consider that what is in the middle in not markdown either and it is raw content. That is why the conversion does not happen.

Here an example at the command line (the conversion result by pandoc is after the ^Z)

❯ pandoc -t context
\startalignment[center]
![](temp2_files/figure-context/unnamed-chunk-6-1.pdf)<!-- -->
\stopalignment
^Z
\startalignment[center]
![](temp2_files/figure-context/unnamed-chunk-6-1.pdf)<!-- -->
\stopalignment

You have several solution

  • you can use raw attributes on the line with ConteXt string. This will allow Pandoc to not see the all lines as a block
❯ pandoc -t context
`\startalignment[center]`{=context}
![](temp2_files/figure-context/unnamed-chunk-6-1.pdf)<!-- -->
`\stopalignment`{=context}
^Z
\startalignment[center]
{\externalfigure[temp2_files/figure-context/unnamed-chunk-6-1.pdf]}
\stopalignment

You can also add an empy line to break the block (if it works for Context after, I don't know that)

❯ pandoc -t context
\startalignement[center]

![](temp2_files/figure-context/unnamed-chunk-6-1.pdf)<!-- -->

\stopalignment
^Z
\startalignement[center]

{\externalfigure[temp2_files/figure-context/unnamed-chunk-6-1.pdf]}

\stopalignment

hope it helps

1 Like

Thank you for your detailed and very well explained solution! This answers my question, clearly and concisely. I appreciate it very much.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.