### Check Statamarkdown Installation Source: https://github.com/hemken/statamarkdown/blob/master/README.md Loads the Statamarkdown package and runs its example to verify the installation. If successful, an example is created in a temporary directory. ```r library(Statamarkdown) example("stata_engine", package="Statamarkdown") ``` -------------------------------- ### R console example: Set up Statamarkdown and use Stata code chunks Source: https://github.com/hemken/statamarkdown/blob/master/man/find_stata.Rd Example from the find_stata documentation showing an R Markdown document with an R setup chunk and a Stata code chunk. The example checks that Stata is found and that rmarkdown is available, then renders the document to HTML in a fresh R process. ```markdown 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 ``` ' ``` ```r if (nzchar(Statamarkdown::find_stata(message = FALSE)) && requireNamespace("rmarkdown", quietly = TRUE)) { # To run this example, remove tempdir(). frmd <- file.path(tempdir(), "test.Rmd") fhtml <- file.path(tempdir(), "test.html") # Knit and render in a fresh R process, so that stale knitr state in a # long-running session (e.g. from RStudio's "Run examples" button) # cannot interfere with how the document text is parsed. xfun::Rscript_call( function(indoc, frmd, fhtml) { writeLines(indoc, frmd) rmarkdown::render(frmd, "html_document", fhtml) }, args = list(indoc, frmd, fhtml) ) message("HTML output created at: ", fhtml) if (interactive()) { # Show in the RStudio Viewer pane if available, otherwise the browser viewer <- getOption("viewer", default = utils::browseURL) viewer(fhtml) } } ``` -------------------------------- ### Example: spinstata with markup in a Stata do file Source: https://github.com/hemken/statamarkdown/blob/master/man/spinstata.Rd Shows a Stata do file with special comment markup (/*' ... '*/ for text, /*+ ... +*/ for chunk headers, /*R ... R*/ for R code) that spinstata converts to Markdown and HTML. The example runs only if Stata is found and the markdown package is installed; it writes the HTML to a temporary file and displays it in the viewer if interactive. ```stata 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 +*/ sysuse auto /* Stata comment */ summarize /*' You can use the ***usual*** Markdown to mark up text.'*/ " if (nzchar(Statamarkdown::find_stata(message = FALSE)) && requireNamespace("markdown", quietly = TRUE)) { # To run this example, remove tempdir(). fhtml <- file.path(tempdir(), "test.html") # Spin in a fresh R process, so that stale knitr state in a # long-running session (e.g. from RStudio's "Run examples" button) # cannot interfere with how the document text is parsed. x <- xfun::Rscript_call( function(indoc) Statamarkdown::spinstata(text = indoc), args = list(indoc) ) writeLines(x, fhtml) message("HTML output created at: ", fhtml) if (interactive()) { # Show in the RStudio Viewer pane if available, otherwise the browser viewer <- getOption("viewer", default = utils::browseURL) viewer(fhtml) } } ``` -------------------------------- ### R Markdown example using Statamarkdown and Stata engine Source: https://github.com/hemken/statamarkdown/blob/master/man/stata_engine.Rd Shows how to set up Statamarkdown in an R chunk and then use a Stata chunk. The Stata chunk runs Stata code and returns log output. Requires Statamarkdown and rmarkdown to be installed. ```markdown 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 ``` ' ``` -------------------------------- ### Attach Statamarkdown library in a setup chunk Source: https://github.com/hemken/statamarkdown/blob/master/man/Statamarkdown-package.Rd Place this preliminary code chunk in an R Markdown document to attach the Statamarkdown library before using its Stata language engine. The chunk is marked with include=FALSE to suppress its output. ```markdown ```{r setup, include=FALSE} library(Statamarkdown) ``` ``` -------------------------------- ### Install Statamarkdown from CRAN Source: https://github.com/hemken/statamarkdown/blob/master/README.md Installs the Statamarkdown R package from CRAN. Requires an R environment. ```r install.packages("Statamarkdown") ``` -------------------------------- ### Install Statamarkdown from GitHub Source: https://github.com/hemken/statamarkdown/blob/master/README.md Installs the development version of Statamarkdown from GitHub using the remotes package. ```r remotes::install_github("Hemken/Statamarkdown") ``` -------------------------------- ### Render R Markdown to HTML with rmarkdown::render Source: https://github.com/hemken/statamarkdown/blob/master/man/stata_engine.Rd Knit and render the example document in a fresh R process to avoid stale knitr state. Creates an HTML file in a temporary directory and displays it in the viewer if interactive. ```r if (nzchar(Statamarkdown::find_stata(message = FALSE)) && requireNamespace("rmarkdown", quietly = TRUE)) { # To run this example, remove tempdir(). frmd <- file.path(tempdir(), "test.Rmd") fhtml <- file.path(tempdir(), "test.html") # Knit and render in a fresh R process, so that stale knitr state in a # long-running session (e.g. from RStudio's "Run examples" button) # cannot interfere with how the document text is parsed. xfun::Rscript_call( function(indoc, frmd, fhtml) { writeLines(indoc, frmd) rmarkdown::render(frmd, "html_document", fhtml) }, args = list(indoc, frmd, fhtml) ) message("HTML output created at: ", fhtml) if (interactive()) { # Show in the RStudio Viewer pane if available, otherwise the browser viewer <- getOption("viewer", default = utils::browseURL) viewer(fhtml) } } ``` -------------------------------- ### Example: Extract Stata code from a dynamic document Source: https://github.com/hemken/statamarkdown/blob/master/man/purl_stata.Rd Shows how to use purl_stata() with a text string containing an R Markdown document. The document includes an R chunk and two Stata chunks; purl_stata() extracts the Stata code and returns the lines as a character vector when text is given and output is NULL. ```r indoc <- ' Some text. ```{r} library(Statamarkdown) ``` ```{stata first-Stata, collectcode=TRUE} sysuse auto, clear generate gpm = 1/mpg ``` ```{stata second-Stata} regress price gpm ``` ' purl_stata(text = indoc) ``` -------------------------------- ### spinstata(statafile, text = NULL, keep = FALSE, ...) Source: https://github.com/hemken/statamarkdown/blob/master/man/spinstata.Rd Converts a Stata do file with special comment markup into Markdown and HTML documents (or other formats). It processes the file through knitr's spin mechanism, supporting text input as an alternative to a file. ```APIDOC ## spinstata(statafile, text = NULL, keep = FALSE, ...) ### Description Converts a Stata do file with special comment markup into Markdown and HTML documents (or other formats). It processes the file through knitr's spin mechanism, supporting text input as an alternative to a file. ### Usage spinstata(statafile, text = NULL, keep = FALSE, ...) ### Arguments - **statafile** (character) - Required - A character string with the name of a Stata "do" file, containing markup in its comments. - **text** (character) - Optional - A character string in place of a file. - **keep** (logical) - Optional - Whether to save intermediate files. - **...** - Optional - Options passed to knitr::spin(), for example format (the output format, such as "Rmd" or "qmd"), knit, report or envir. ### Value The path to the output file. If given text instead of a file, returns the compiled document as a character string. ### Details This function takes a Stata file containing special markup in its 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 "*/*" Code in a chunk is taken to be Stata code, and the chunk header is given the engine='stata' chunk option, unless the code is marked as R code with the "/*R ... R*/" markup, or the chunk header sets an engine option itself. Writing engine='stata' in the chunk headers of a "do" file is therefore no longer necessary, but it is still honoured. With knitr >= 1.53, Stata is also used for code which is not preceded by a chunk header at all, in a document which has no chunks of R code. ### Examples ```r 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 +*/ sysuse auto /* Stata comment */ summarize /*' You can use the ***usual*** Markdown to mark up text.'*/ " if (nzchar(Statamarkdown::find_stata(message = FALSE)) && requireNamespace("markdown", quietly = TRUE)) { # To run this example, remove tempdir(). fhtml <- file.path(tempdir(), "test.html") # Spin in a fresh R process, so that stale knitr state in a # long-running session (e.g. from RStudio's "Run examples" button) # cannot interfere with how the document text is parsed. x <- xfun::Rscript_call( function(indoc) Statamarkdown::spinstata(text = indoc), args = list(indoc) ) writeLines(x, fhtml) message("HTML output created at: ", fhtml) if (interactive()) { # Show in the RStudio Viewer pane if available, otherwise the browser viewer <- getOption("viewer", default = utils::browseURL) viewer(fhtml) } } ``` ### See Also knitr::spin(), Statamarkdown-package ### Author Doug Hemken ``` -------------------------------- ### find_stata(message = TRUE) Source: https://github.com/hemken/statamarkdown/blob/master/man/find_stata.Rd A helper function that seeks to locate your Stata executable. Ordinarily this is run automatically when Statamarkdown is loaded. ```APIDOC ## find_stata(message = TRUE) ### Description A helper function that seeks to locate your Stata executable. Ordinarily this is run automatically when Statamarkdown is loaded. ### Usage find_stata(message = TRUE) ### Arguments - **message** (logical) - Optional - Whether or not to print messages: where Stata was found, or that no Stata executable could be found. ### Value A character string with the path and name of the Stata executable. ### Details 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 its correct location yourself. ### Examples ```r 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 (nzchar(Statamarkdown::find_stata(message = FALSE)) && requireNamespace("rmarkdown", quietly = TRUE)) { # To run this example, remove tempdir(). frmd <- file.path(tempdir(), "test.Rmd") fhtml <- file.path(tempdir(), "test.html") # Knit and render in a fresh R process, so that stale knitr state in a # long-running session (e.g. from RStudio's "Run examples" button) # cannot interfere with how the document text is parsed. xfun::Rscript_call( function(indoc, frmd, fhtml) { writeLines(indoc, frmd) rmarkdown::render(frmd, "html_document", fhtml) }, args = list(indoc, frmd, fhtml) ) message("HTML output created at: ", fhtml) if (interactive()) { # Show in the RStudio Viewer pane if available, otherwise the browser viewer <- getOption("viewer", default = utils::browseURL) viewer(fhtml) } } ``` ### See Also [Statamarkdown-package] ### Author Doug Hemken ``` -------------------------------- ### purl_stata(input, output = NULL, text = NULL, documentation = 1L) Source: https://github.com/hemken/statamarkdown/blob/master/man/purl_stata.Rd Extracts Stata code from the Stata code chunks of an R Markdown or Quarto document and writes it to a Stata do-file. It is the Stata analogue of knitr::purl(). ```APIDOC ## purl_stata(input, output = NULL, text = NULL, documentation = 1L) ### Description Extracts the code from the Stata code chunks of an R Markdown or Quarto document and writes it to a Stata do-file. It is the Stata analogue of knitr::purl(). ### Usage purl_stata(input, output = NULL, text = NULL, documentation = 1L) ### Arguments - **input** (character) - Required - A character string with the name of the input document. - **output** (character) - Optional - A character string with the name of the do-file to write. Defaults to the name of the input document with its extension changed to `.do`. - **text** (character) - Optional - A character string with the document text to use in place of a file. - **documentation** (integer/logical) - Optional - How much documentation to carry into the do-file, following knitr::purl(): `0` (or `FALSE`) extracts the code only; `1` (or `TRUE`, the default) precedes the code of each chunk with a Stata comment giving the chunk's header (its label and options), in the `*%% label ----` form knitr uses when it tangles a script in another language; `2` also includes the document's text as Stata comments (the code of non-Stata chunks is not included). ### Value If a do-file is written, the path to the do-file, invisibly. If `text` is given and `output` is `NULL`, a character vector of the extracted lines. ### Details Chunks are recognised with knitr's own chunk patterns, so indented chunks and fences of more than three backticks are handled. A chunk is extracted when its header engine is `stata`, or when it uses the older `r` chunk form with an `engine = "stata"` option. Chunks with the `purl = FALSE` or `eval = FALSE` options (either in the chunk header or in option comments) are skipped. Option comments are split from the code by knitr::partition_chunk(), so the forms knitr itself recognises are honoured: `*|` in a `stata` chunk and `#|` in a chunk using the older `engine='stata'` form. They are never copied into the do-file as code, but with `documentation >= 1` they are recorded as plain Stata comments below the chunk header line. ### Examples ```r indoc <- ' Some text. ```{r} library(Statamarkdown) ``` ```{stata first-Stata, collectcode=TRUE} sysuse auto, clear generate gpm = 1/mpg ``` ```{stata second-Stata} regress price gpm ``` ' purl_stata(text = indoc) ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.