This is an introduction to writing dynamic documents using R Markdown to produce documents based on Stata. This describes using R on linstat to create documents that depend upon Stata code. The source document is Statalinux.rmd


Markdown is a language for formatting not-too-complicated documents using just a few text symbols. It is designed to be easy to read and write. If you read and write email, you are probably already familiar with many of these formatting conventions. For more specifics about Markdown see John Gruber's Markdown article.

Dynamic Markdown has been implemented for a number of programming languages, including Stata and R. Within Stata there is a dynamic markdown package called stmd that relies on Stata's dyndoc command, as well as the user-written package markstat. Each has it's strengths and weaknesses.

The system I will describe here is intended primarily for those of us who are already using R Markdown to write documentation in other languages, and would like to use this for Stata as well.

R Markdown is a dynamic markdown system that extends Markdown by allowing you to include blocks of code in one of several programming languages. The code is evaluated, and both the code and it's results are included in a Markdown document. To read more about the details of R Markdown see RStudio's R Markdown webpages

RStudio uses an R package called knitr (this could also be called directly from R), which includes the ability to evaluate Stata.

The documentation for knitr can be found in R's Help, from Yihui Xie's web page, or in the book, R Markdown: The Definitive Guide.

Finally, I use some helper functions in a package called Statamarkdown. While these are not necessary to write dynamic documents based on Stata, they make life easier.

Statamarkdown can be installed from

library(devtools) # before this you may need to install devtools

Note, RStudio is a great environment for writing Markdown with executable R code chunks, but it is not a friendly environment for extensively debugging problems in your Stata code. If your Stata code is complicated, you should probably work out the details in Stata first, then pull it into RStudio to develop your documentation!

# .libPaths()
# installed.packages()["Statamarkdown","Package"]
statapath <- find_stata()
## Stata found at /usr/local/stata/stata
# packageVersion(Statamarkdown)
# statapath <- "/software/stata/stata"
knitr::opts_chunk$set(engine.path=list(stata=statapath), comment=NA)

Setting up the Stata engine

In order to execute your Stata code, knitr needs to know where the Stata executable is located. This can be done with a preliminary code chunk, by loading the Statamarkdown package:

```{r, echo=FALSE, message=FALSE}
statapath <- "/software/stata/stata"
knitr::opts_chunk$set(engine.path=list(stata=statapath), comment=NA)

(In knitr jargon, a block of code is a "code chunk".)

If the package fails to find your copy of Stata (you will see a message), you may have to specify this yourself (see Stata Engine Path).

After this setup chunk, subsequent code to be processed by Stata can be specified as:


-- Stata code here --


Linking Code Blocks (collectcode=TRUE)

Each block (chunk) of Stata code is executed as a separate batch job. This means that as you move from code chunk to code chunk, all your previous work is lost. To retain data from code chunk to code chunk requires collecting (some of) your code and processing it silently at the beginning of each subsequent chunk.

You can have knitr collect code for you, as outlined in Linking Stata Code Blocks.

Hints and Examples

Code Separately, or in the Output

Stata does not give you fine control over what ends up in the .log file. You can decide whether to present code and output separately (R style), or include the code in the output (Stata style).
See Stata Output Hooks).

Including Graphs

Including graphics requires graph export in Stata, and an image link in the R Markdown. The knitr chunk option echo can print just specified lines of code, allowing you to hide the graph export command.

Descriptive Statistics

A simple example.

```{stata, collectcode=TRUE}
sysuse auto
sysuse auto

. sysuse auto
(1978 Automobile Data)

. summarize

    Variable |        Obs        Mean    Std. Dev.       Min        Max
        make |          0
       price |         74    6165.257    2949.496       3291      15906
         mpg |         74     21.2973    5.785503         12         41
       rep78 |         69    3.405797    .9899323          1          5
    headroom |         74    2.993243    .8459948        1.5          5
       trunk |         74    13.75676    4.277404          5         23
      weight |         74    3019.459    777.1936       1760       4840
      length |         74    187.9324    22.26634        142        233
        turn |         74    39.64865    4.399354         31         51
displacement |         74    197.2973    91.83722         79        425
  gear_ratio |         74    3.014865    .4562871       2.19       3.89
     foreign |         74    .2972973    .4601885          0          1

Frequency Tables

Using chunk option echo=FALSE, more typical Stata documentation style.

```{stata, echo=FALSE}
tab1 foreign rep78

running /home/h/hemken/PUBLIC_web/Stataworkshops/ . tab1 foreign rep78

-> tabulation of foreign  

   Car type |      Freq.     Percent        Cum.
   Domestic |         52       70.27       70.27
    Foreign |         22       29.73      100.00
      Total |         74      100.00

-> tabulation of rep78  

     Repair |
Record 1978 |      Freq.     Percent        Cum.
          1 |          2        2.90        2.90
          2 |          8       11.59       14.49
          3 |         30       43.48       57.97
          4 |         18       26.09       84.06
          5 |         11       15.94      100.00
      Total |         69      100.00