SASmarkdown (version 0.8.2)

sas_collectcode: 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()

Arguments

Value

There are no return values, chunk hook creation is a side effect here.

Author

Doug Hemken

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.

See Also

SASmarkdown-package

Examples

Run this code
sas_collectcode()
if (FALSE) {
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)
}
}

Run the code above in your browser using DataLab