Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 874
Hendrik Avatar asked Jan 17 '26 22:01

Hendrik


2 Answers

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.

like image 93
slackline Avatar answered Jan 19 '26 20:01

slackline


This might be of interest to you

Stata and R Markdown (Windows) https://www.ssc.wisc.edu/~hemken/Stataworkshops/Stata%20and%20R%20Markdown/StataMarkdown.html

It uses library(Statamarkdown), which will create distinct ```{stata} chunks. So you should be able to run Stata and run R chunks.

I assume you will have to export and import to share objects between different chunks (haven't tried this out yet).

Something similar also exists for Python https://cran.r-project.org/web/packages/reticulate/vignettes/r_markdown.html - here objects can be shared between R and Python (very cool!)

like image 24
Joanne Demmler Avatar answered Jan 19 '26 20:01

Joanne Demmler