When you include dynamic Stata code in a Markdown document, you do not have to show your reader all the code you use. Just what is visible to your reader is controlled by code block attributes. This optional specification is made as part of the code fence, after the language specification and a comma.
You have the option of showing the reader:
You also have the option of showing the reader:
See also help dynamic tags##code
in Stata Help. When processing your dynamic document using knitr
in R, these attributes are referred to as "chunk options".
Most of these options can be specified using the same keywords that are used in Stata's dynamic tags. For compatability, most of these options also have a knitr
style of specification or a markstat
style of specification.
You can show your reader only the output from a code chunk with the nocommands
option in the code fence info tag. The knitr
version of this option is echo=FALSE
.
Your code block looks like this:
```{stata, nocommands}
sysuse auto, clear
tabstat weight, stat(mean sd)
```
But the result in your document is just:
(1978 Automobile Data)
variable | mean sd
-------------+--------------------
weight | 3019.459 777.1936
----------------------------------
Again, the code block:
```{stata, echo=FALSE}
tabulate foreign
```
And the result in your document:
Car type | Freq. Percent Cum.
------------+-----------------------------------
Domestic | 52 70.27 70.27
Foreign | 22 29.73 100.00
------------+-----------------------------------
Total | 74 100.00
Code block:
```s, nocommands
pwcorr mpg weight disp
```
What appears in your document:
| mpg weight displa~t
-------------+---------------------------
mpg | 1.0000
weight | -0.8072 1.0000
displacement | -0.7056 0.8949 1.0000
You can show your reader only the code you use, without the output, yet the code is evaluated. Notice that this suppresses all output in your Stata results viewer, as well as in your document. This suppresses the notes that many commands make, but not error messages (which cause your document to simply stop processing, unless you have used the nostop
option with stmd
for your whole document).
Code block:
```{stata, nooutput}
sysuse auto, clear
regress mpg weight
```
In your document:
. sysuse auto, clear // no dataset note
. regress mpg weight // no output
You might use this is you want to discuss how to write some code, and then later deal with the output.
Source | SS df MS Number of obs = 74
-------------+---------------------------------- F(1, 72) = 134.62
Model | 1591.9902 1 1591.9902 Prob > F = 0.0000
Residual | 851.469256 72 11.8259619 R-squared = 0.6515
-------------+---------------------------------- Adj R-squared = 0.6467
Total | 2443.45946 73 33.4720474 Root MSE = 3.4389
------------------------------------------------------------------------------
mpg | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
weight | -.0060087 .0005179 -11.60 0.000 -.0070411 -.0049763
_cons | 39.44028 1.614003 24.44 0.000 36.22283 42.65774
------------------------------------------------------------------------------
This may also be specified in the knitr
manner with results=FALSE
or results="hide"
(note the capitalization and the quotes).
To have code evaluated in the background, use the quietly
option.
Code block:
```{stata, quietly}
generate gpm=1/mpg
```
(Although I show you my code here, including the code fence, the actual execution takes place quietly after this paragraph.)
In your document:
This is equivalent to using both nocommands
and nooutput
.
Code block:
```{stata, nocommands, nooutput}
regress gpm weight
```
In your document:
(The markstat
style of specification uses a forward slash at the end of the language specification.)
. regress
Source | SS df MS Number of obs = 74
-------------+---------------------------------- F(1, 72) = 194.71
Model | .008729651 1 .008729651 Prob > F = 0.0000
Residual | .003227977 72 .000044833 R-squared = 0.7300
-------------+---------------------------------- Adj R-squared = 0.7263
Total | .011957628 73 .000163803 Root MSE = .0067
------------------------------------------------------------------------------
gpm | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
weight | .0000141 1.01e-06 13.95 0.000 .0000121 .0000161
_cons | .0077077 .0031426 2.45 0.017 .0014431 .0139723
------------------------------------------------------------------------------