sas_collectcode {SASmarkdown} | R Documentation |
Create a knitr chunk hook for accumlating code.
Description
This wrapper function calls knitr::knit_hooks$set()
to
define a chunk hook. When the chunk hook is later invoked, this
writes the contents of the current chunk to the end of a SAS
autoexec.sas
file.
This may be used with any of the SAS language engines.
Usage
sas_collectcode()
Details
This function is automatically invoked when the SASmarkdown
library is attached. Normally a user will not need to call
this function, instead using collectcode=TRUE
as a
chunk option - see the example below.
When knitr calls SAS, each code chunk is processed as a separate SAS batch job. Where code in one chunk depends upon the results from a prevous chunk, code needs to be repeated and re-evaluated.
This function creates a knitr chunk hook that signals when one
chunk's code should be saved for re-use later. The code ends up
in a temporary SAS autoexec.sas
file.
Value
There are no return values, chunk hook creation is a side effect here.
Note
If there is already an ‘autoexec.sas’ in the directory where the source document is located, collected code will be added to it, and the original file will be restored after your document is processed.
Author(s)
Doug Hemken
See Also
Examples
sas_collectcode()
## Not run:
indoc <- '
---
title: "Linking SASmarkdown Code Chunks"
author: "Doug Hemken"
output: html_document
---
# An R console example
## In a first code chunk, set up with
```{r}
library(SASmarkdown)
```
## Then mark SAS code chunks with
```{sas, collectcode=TRUE}
data class;
set sashelp.class;
bmi = 703*weight/height**2;
run;
```
## A later chunk that depends on the first.
```{sas}
proc means;
var bmi;
run;
```
'
if (!is.null(SASmarkdown::find_sas())) {
# 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)
}
## End(Not run)