Title: | 'Stata' Markdown |
---|---|
Description: | Settings and functions to extend the 'knitr' 'Stata' engine. |
Authors: | Doug Hemken [aut, cre] (SSCC, Univ. of Wisconsin-Madison), Tom Palmer [ctb] (MacOS, linux), Philipp Lepert [ctb] |
Maintainer: | Doug Hemken <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.9.2 |
Built: | 2024-10-29 03:15:29 UTC |
Source: | https://github.com/Hemken/Statamarkdown |
To use these functions and settings, attach the Statamarkdown
library from within the document to be knit
. A
typical preliminary code check in a document would be
```{r setup, include=FALSE} library(Statamarkdown) ```
Using the "Stata" language engine in knitr has a number of limitations. Each Stata code chunk is run as a separate batch file, and source code is part of the output returned to the document being knit. This package provides a language engine with code chunk options to overcome these limitations.
If you render multiple documents from the same script or R session,
you should detach("Statamarkdown")
in between documents.
A function here sets up a chunk hook, that silently repeats selected
code chunks
at the beginning of later code chunks. This allows
the code in one chunk to use the results of a previous chunk. The
user marks code chunks to be silently repeated with the
chunk option collectcode=TRUE
.
A second function here sets up an output hook. This removes Stata
code from the output by default. To leave Stata commands in the
output, specify the chunk option cleanlog=FALSE
.
To save the code from a code block (as a
"do" file) and also to save the Stata log file produced by
that code block, specify chunk option savedo=TRUE
. The filenames
are the same as the chunk label.
Whether or not to evaluate
the code in the code block. Use eval=FALSE
to show
code to the reader without having it evaluated.
Selective evaluation by specifying a numeric vector (an option for R code blocks) does not work for Stata.
Whether or not any trace
of this code block appears in your document. Use include=FALSE
to evaluate code but suppress the source code echo and all
output (including error messages).
This is equivalent to eval=TRUE, echo=FALSE, results="hide", error=FALSE
.
Whether or not
to show the reader the source code. Use echo=FALSE
to
suppress the source code in your document.
If this is specified as
a numeric vector, it indicates which source lines to show
or suppress. For example, echo=c(1,2)
shows only the
first two lines of the code block in the document (while still
evaluating the entire code block). Likewise, echo=-1
hides
just the first line of code from the reader.
To suppress
normal output while still showing error messages
use results="hide"
.
Whether or not to show error
messages in your document. To suppress error messages use
error=FALSE
.
Error messages that Stata writes to the log will appear as normal output - they are not "errors" in this context. This option affects error messages returned to/by the operating system.
A prefix to use before
lines of output. The default for R output is comment="##"
Filename to be run and input in the document.
Doug Hemken
More documentation and examples: https://www.ssc.wisc.edu/~hemken/Stataworkshops/stata.html#stata-and-r-markdown
The package that this extends: knitr-package.
This function searches for recent versions of Stata (>= Stata 11), in some of the usual default installation locations.
If Stata is not found, you will have to specify it's correct location yourself.
find_stata(message=TRUE)
find_stata(message=TRUE)
message |
(logical) Whether or not to print a message when Stata is found. |
A character string with the path and name of the Stata executable.
Doug Hemken
indoc <- ' # An R console example ## In a first code chunk, set up with ```{r} library(Statamarkdown) ``` ## Then mark Stata code chunks with ```{stata} sysuse auto, clear generate gpm = 1/mpg summarize price gpm ``` ' if (!is.null(Statamarkdown::find_stata())) { # To run this example, remove tempdir(). fmd <- file.path(tempdir(), "test.md") fhtml <- file.path(tempdir(), "test.html") knitr::knit(text=indoc, output=fmd) rmarkdown::render(fmd, "html_document", fhtml) }
indoc <- ' # An R console example ## In a first code chunk, set up with ```{r} library(Statamarkdown) ``` ## Then mark Stata code chunks with ```{stata} sysuse auto, clear generate gpm = 1/mpg summarize price gpm ``` ' if (!is.null(Statamarkdown::find_stata())) { # To run this example, remove tempdir(). fmd <- file.path(tempdir(), "test.md") fhtml <- file.path(tempdir(), "test.html") knitr::knit(text=indoc, output=fmd) rmarkdown::render(fmd, "html_document", fhtml) }
This function takes a Stata file containing special mark up in it's comments, and converts it to Markdown and HTML documents (or one of several other formats).
spinstata(statafile, text=NULL, keep=FALSE, ...)
spinstata(statafile, text=NULL, keep=FALSE, ...)
statafile |
A character string with the name of a Stata "do" file, containing markup in it's comments. |
text |
A character string in place of a file. |
keep |
Whether to save intermediate files. |
... |
options passed to |
This function takes a Stata file containing special mark up in
it's comments, and converts it into knitr's "spin" format.
This is in turn sent to knitr::spin
, and converted to
Markdown and HTML (or one of several other formats).
Special Markup:
"/*' "
- Begin document text, ends with "'*/"
"/*+ "
- Begin chunk header, ends with "+*/"
"/*R "
- Begin a chunk of R code, ends with "R*/"
"*/* "
- Dropped from document, ends with
"*/*"
The path to the output file.
If given text instead of a file, returns the compiled document as a character string.
Doug Hemken
## Not run: indoc <- "/*' # Statamarkdown Example This is a special Stata script which can be used to generate a report. You can write normal text in command-style comments. First we load Statamarkdown. '*/ /*+ setup +*/ /*R library(Statamarkdown) R*/ /*' The report begins here. '*/ /*+ example1, engine='stata' +*/ sysuse auto /* Stata comment */ summarize /*' You can use the ***usual*** Markdown to mark up text.'*/ " if (!is.null(Statamarkdown::find_stata())) { # To run this example, remove tempdir(). fhtml <- file.path(tempdir(), "test.html") x<-Statamarkdown::spinstata(text=indoc) writeLines(x, fhtml) } ## End(Not run)
## Not run: indoc <- "/*' # Statamarkdown Example This is a special Stata script which can be used to generate a report. You can write normal text in command-style comments. First we load Statamarkdown. '*/ /*+ setup +*/ /*R library(Statamarkdown) R*/ /*' The report begins here. '*/ /*+ example1, engine='stata' +*/ sysuse auto /* Stata comment */ summarize /*' You can use the ***usual*** Markdown to mark up text.'*/ " if (!is.null(Statamarkdown::find_stata())) { # To run this example, remove tempdir(). fhtml <- file.path(tempdir(), "test.html") x<-Statamarkdown::spinstata(text=indoc) writeLines(x, fhtml) } ## End(Not run)
This function creates a modified Stata engine.
Set up once per session (i.e. document). Ordinarily this is run automatically when Statamarkdown is loaded.
stata_engine(options)
stata_engine(options)
options |
|
This function is used as follows.
stata_engine(options)
is a language engine that returns Stata log output.
The end user should not need to use the language engine function directly. This is the workhorse function that actually calls Stata and returns output.
The language engine function returns Stata code and output internally to knitr.
Doug Hemken
indoc <- ' # An R console example ## In a first code chunk, set up with ```{r} library(Statamarkdown) ``` ## Then mark Stata code chunks with ```{stata} sysuse auto, clear generate gpm = 1/mpg summarize price gpm ``` ' if (!is.null(Statamarkdown::find_stata())) { # To run this example, remove tempdir(). fmd <- file.path(tempdir(), "test.md") fhtml <- file.path(tempdir(), "test.html") knitr::knit(text=indoc, output=fmd) rmarkdown::render(fmd, "html_document", fhtml) }
indoc <- ' # An R console example ## In a first code chunk, set up with ```{r} library(Statamarkdown) ``` ## Then mark Stata code chunks with ```{stata} sysuse auto, clear generate gpm = 1/mpg summarize price gpm ``` ' if (!is.null(Statamarkdown::find_stata())) { # To run this example, remove tempdir(). fmd <- file.path(tempdir(), "test.md") fhtml <- file.path(tempdir(), "test.html") knitr::knit(text=indoc, output=fmd) rmarkdown::render(fmd, "html_document", fhtml) }