8 Inline Stata Results

Statamarkdown does not provide support for inline code, because knitr does not support inline code in any language other than R.

While this means you cannot call Stata inline to insert results within ordinary text, for users with sufficient knowledge of both Stata and R, there is a workaround. It is not very elegant, but here goes.

8.1 Example

An example of what we are aiming for is the following:


sysuse auto, clear
summarize price
(1978 automobile data)

    Variable |        Obs        Mean    Std. dev.       Min        Max
-------------+---------------------------------------------------------
       price |         74    6165.257    2949.496       3291      15906

The average price was 6165.257.


8.2 Markdown

The markdown used is:


```{stata, collectcode=TRUE} 
sysuse auto, clear
summarize price
```
```{stata for_inline, include=FALSE} 
file open myfile using example.txt, write replace
file write myfile `"`r(mean)'"'
file close myfile
```

The average price was `r format(as.numeric(readLines('example.txt'), warn=FALSE), nsmall=2)`.

```{r cleanup, include=FALSE} 
unlink("example.txt")
```

The second code block, labeled "for_inline" is Stata code that writes the appropriate value to a file, here named "example.txt".

The inline code, written in R, then reads the value from the file and formats it for display.

The final code block, labeled "cleanup" then removes the file used to transfer the value. In this example it is written in R, but we could have used Stata just as well.

I warned you this isn't graceful!

This page was written using:

  • Statamarkdown version 0.9.2
  • knitr version 1.45