Each code chunk in your dynamic markdown document runs
as a separate batch file in Stata. This means that
the results of one code chunk do not automatically
carry over to the next. You can make the results of
a chunk carry over to all subsequent chunks by using
the collectcode=TRUE
chunk option.
Collected code accumulates, and runs silently before each subsequent chunk.
(If necessary, manually specify the Stata executable location.)
library(Statamarkdown)
In this example we load the data and calculate a new variable in a first code chunk. Then in a later code chunk we also use the data, including the new variable.
In the first code chunk, use the collectcode
option.
```{stata first-Stata, collectcode=TRUE}`r ''` sysuse auto, clear generate gpm = 1/mpg summarize price gpm ```
Which looks like this in your document: ```{stata first-Stata, collectcode=TRUE} sysuse auto, clear generate gpm = 1/mpg summarize price gpm
### Second code block: Then you can use the data and the new variable in a later code chunk. ```{stata second-Stata}`r ''` regress price gpm ``` Which looks like this in your document: ```{stata second-Stata} regress price gpm
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.