Search code examples
rmarkdownstatar-markdown

Switch between R and Stata in Markdown


It is possible to generate Stata output with markdown in Rstudio, by spcifying the following in the script:

```{r}
statapath <- "C:/Program Files (x86)/Stata13/StataSE-64.exe"
opts_chunk$set(engine="stata", engine.path=statapath, comment="")
```

After that, it is possible to use Stata syntax to generate the output.

Is it possible to switch back to R for some code chunks, then switch back to Stata again?

The reason is that I use Stata for regression (tables), but R for most of the other things I do. So a functionality like this would come in handy for me.

I tried:

```{r}
rpath <- "C:/Program Files/RStudio/bin/rstudio.exe"
opts_chunk$set(engine="R", engine.path=rpath, comment="")
```

It did not work.


Solution

  • I very much doubt this is possible at present.

    I've had to use Stata for some analyses recently and do so by having a Do-file which takes arguments and calling it using system() to run the script. The script itself generates results from regression models (xtnbreg to be precise) and I collate these into one file using parmest within the do-file (saving as a Stata file). This resulting Stata file with the output from parmest is then read into R and printed using R's native markup rendering.

    I'd suggest considering this unless you want to dig very deep into adding functionality to RMarkdown/Pandoc.