# Demos of `knit_expand()`

A few simple examples:

``````library(knitr)
knit_expand(text = 'The value of pi is {{pi}}.')
``````
``````##  "The value of pi is 3.14159265358979."
``````
``````knit_expand(text = 'The value of a is {{a}}, so a + 1 is {{a+1}}.', a = rnorm(1))
``````
``````##  "The value of a is -0.419844254151494, so a + 1 is 0.580155745848506."
``````
``````knit_expand(text = 'The area of a circle with radius {{r}} is {{pi*r^2}}', r = 5)
``````
``````##  "The area of a circle with radius 5 is 78.5398163397448"
``````

Any number of variables:

``````knit_expand(text = 'a is {{a}} and b is {{b}}, with my own pi being {{pi}} instead of {{base::pi}}', a=1, b=2, pi=3)
``````
``````##  "a is 1 and b is 2, with my own pi being 3 instead of 3.14159265358979"
``````

Custom delimiter `<% %>`:

``````knit_expand(text = 'I do not like curly braces, so use % with <> instead: a is <% a %>.', a = 8, delim = c("<%", "%>"))
``````
``````##  "I do not like curly braces, so use % with <> instead: a is 8."
``````

The pyexpander delimiter:

``````knit_expand(text = 'hello \$(LETTERS) and \$(pi)!', delim = c("\$(", ")"))
``````
``````##  "hello X and 3.14159265358979!"
``````

Arbitrary R code:

``````knit_expand(text = 'you cannot see the value of x {{x=rnorm(1)}}but it is indeed created: x = {{x}}')
``````
``````##  "you cannot see the value of x but it is indeed created: x = 0.494748186837257"
``````
``````res = knit_expand(text = c(' x | x^2', '{{x=1:5;paste(sprintf("%2d | %3d", x, x^2), collapse = "\n")}}'))
cat(res)
``````
``````##  x | x^2
##  1 |   1
##  2 |   4
##  3 |   9
##  4 |  16
##  5 |  25
``````

The m4 example: http://en.wikipedia.org/wiki/M4_(computer_language)

``````res = knit_expand(text = c('{{i=0;h2=function(x){i<<-i+1;sprintf("<h2>%d. %s</h2>", i, x)} }}<html>',
'{{h2("First Section")}}', '{{h2("Second Section")}}', '{{h2("Conclusion")}}', '</html>'))
cat(res)
``````
``````## <html>
## <h2>1. First Section</h2>
## <h2>2. Second Section</h2>
## <h2>3. Conclusion</h2>
## </html>
``````

Build regression models based on a template; loop through all variables in `mtcars`:

``````src = lapply(names(mtcars)[-1], function(i) {
knit_expand(text=c("# Regression on {{i}}", '```{r lm-{{i}}}', 'lm(mpg~{{i}}, data=mtcars)', '```'))
})
# knit the source
res = knit_child(text = unlist(src))
res = paste('<pre><code>', gsub('^\\s*|\\s*\$', '', res), '</code></pre>', sep = '')
``````
``````# Regression on cyl

```r
lm(mpg~cyl, data=mtcars)
```

```
##
## Call:
## lm(formula = mpg ~ cyl, data = mtcars)
##
## Coefficients:
## (Intercept)          cyl
##       37.88        -2.88
```
# Regression on disp

```r
lm(mpg~disp, data=mtcars)
```

```
##
## Call:
## lm(formula = mpg ~ disp, data = mtcars)
##
## Coefficients:
## (Intercept)         disp
##     29.5999      -0.0412
```
# Regression on hp

```r
lm(mpg~hp, data=mtcars)
```

```
##
## Call:
## lm(formula = mpg ~ hp, data = mtcars)
##
## Coefficients:
## (Intercept)           hp
##     30.0989      -0.0682
```
# Regression on drat

```r
lm(mpg~drat, data=mtcars)
```

```
##
## Call:
## lm(formula = mpg ~ drat, data = mtcars)
##
## Coefficients:
## (Intercept)         drat
##       -7.52         7.68
```
# Regression on wt

```r
lm(mpg~wt, data=mtcars)
```

```
##
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
##
## Coefficients:
## (Intercept)           wt
##       37.29        -5.34
```
# Regression on qsec

```r
lm(mpg~qsec, data=mtcars)
```

```
##
## Call:
## lm(formula = mpg ~ qsec, data = mtcars)
##
## Coefficients:
## (Intercept)         qsec
##       -5.11         1.41
```
# Regression on vs

```r
lm(mpg~vs, data=mtcars)
```

```
##
## Call:
## lm(formula = mpg ~ vs, data = mtcars)
##
## Coefficients:
## (Intercept)           vs
##       16.62         7.94
```
# Regression on am

```r
lm(mpg~am, data=mtcars)
```

```
##
## Call:
## lm(formula = mpg ~ am, data = mtcars)
##
## Coefficients:
## (Intercept)           am
##       17.15         7.24
```
# Regression on gear

```r
lm(mpg~gear, data=mtcars)
```

```
##
## Call:
## lm(formula = mpg ~ gear, data = mtcars)
##
## Coefficients:
## (Intercept)         gear
##        5.62         3.92
```
# Regression on carb

```r
lm(mpg~carb, data=mtcars)
```

```
##
## Call:
## lm(formula = mpg ~ carb, data = mtcars)
##
## Coefficients:
## (Intercept)         carb
##       25.87        -2.06
`````````