skimr
Version:
CLI EDA for CSVs
375 lines (353 loc) • 8.77 kB
HTML
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Demos of <code>knit_expand()</code></title>
<style type="text/css">
/**
* Prism.s theme ported from highlight.js's xcode style
*/
pre code {
padding: 1em;
}
.token.comment {
color: #007400;
}
.token.punctuation {
color: #999;
}
.token.tag,
.token.selector {
color: #aa0d91;
}
.token.boolean,
.token.number,
.token.constant,
.token.symbol {
color: #1c00cf;
}
.token.property,
.token.attr-name,
.token.string,
.token.char,
.token.builtin {
color: #c41a16;
}
.token.inserted {
background-color: #ccffd8;
}
.token.deleted {
background-color: #ffebe9;
}
.token.operator,
.token.entity,
.token.url,
.language-css .token.string,
.style .token.string {
color: #9a6e3a;
}
.token.atrule,
.token.attr-value,
.token.keyword {
color: #836c28;
}
.token.function,
.token.class-name {
color: #DD4A68;
}
.token.regex,
.token.important,
.token.variable {
color: #5c2699;
}
.token.important,
.token.bold {
font-weight: bold;
}
.token.italic {
font-style: italic;
}
</style>
<style type="text/css">
body {
font-family: sans-serif;
max-width: 800px;
margin: auto;
padding: 1em;
line-height: 1.5;
box-sizing: border-box;
}
body, .footnotes, code { font-size: .9em; }
li li { font-size: .95em; }
*, *:before, *:after {
box-sizing: inherit;
}
pre, img { max-width: 100%; }
pre, pre:hover {
white-space: pre-wrap;
word-break: break-all;
}
pre code {
display: block;
overflow-x: auto;
}
code { font-family: 'DejaVu Sans Mono', 'Droid Sans Mono', 'Lucida Console', Consolas, Monaco, monospace; }
:not(pre) > code, code[class] { background-color: #F8F8F8; }
code.language-undefined, pre > code:not([class]) {
background-color: inherit;
border: 1px solid #eee;
}
table {
margin: auto;
border-top: 1px solid #666;
}
table thead th { border-bottom: 1px solid #ddd; }
th, td { padding: 5px; }
thead, tfoot, tr:nth-child(even) { background: #eee; }
blockquote {
color: #666;
margin: 0;
padding-left: 1em;
border-left: 0.5em solid #eee;
}
hr, .footnotes::before { border: 1px dashed #ddd; }
.frontmatter { text-align: center; }
#TOC .numbered li { list-style: none; }
#TOC .numbered { padding-left: 0; }
#TOC .numbered ul { padding-left: 1em; }
table, .body h2 { border-bottom: 1px solid #666; }
.body .appendix, .appendix ~ h2 { border-bottom-style: dashed; }
.footnote-ref a::before { content: "["; }
.footnote-ref a::after { content: "]"; }
section.footnotes::before {
content: "";
display: block;
max-width: 20em;
}
@media print {
body {
font-size: 12pt;
max-width: 100%;
}
tr, img { page-break-inside: avoid; }
}
@media only screen and (min-width: 992px) {
pre { white-space: pre; }
}
</style>
</head>
<body>
<div class="include-before">
</div>
<div class="frontmatter">
<div class="title"><h1>Demos of <code>knit_expand()</code></h1></div>
<div class="author"><h2></h2></div>
<div class="date"><h3></h3></div>
</div>
<div class="body">
<!--
%\VignetteEngine{knitr::knitr}
%\VignetteIndexEntry{Templating with knit_expand()}
-->
<h1 id="demos-of-knit-expand">Demos of <code>knit_expand()</code></h1>
<p>A few simple examples:</p>
<pre><code class="language-r">library(knitr)
knit_expand(text = 'The value of pi is {{pi}}.')
</code></pre>
<pre><code>## [1] "The value of pi is 3.14159265358979."
</code></pre>
<pre><code class="language-r">knit_expand(text = 'The value of a is {{a}}, so a + 1 is {{a+1}}.', a = rnorm(1))
</code></pre>
<pre><code>## [1] "The value of a is -0.192249268556317, so a + 1 is 0.807750731443683."
</code></pre>
<pre><code class="language-r">knit_expand(text = 'The area of a circle with radius {{r}} is {{pi*r^2}}', r = 5)
</code></pre>
<pre><code>## [1] "The area of a circle with radius 5 is 78.5398163397448"
</code></pre>
<p>Any number of variables:</p>
<pre><code class="language-r">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)
</code></pre>
<pre><code>## [1] "a is 1 and b is 2, with my own pi being 3 instead of 3.14159265358979"
</code></pre>
<p>Custom delimiter <code><% %></code>:</p>
<pre><code class="language-r">knit_expand(text = 'I do not like curly braces, so use % with <> instead: a is <% a %>.', a = 8, delim = c("<%", "%>"))
</code></pre>
<pre><code>## [1] "I do not like curly braces, so use % with <> instead: a is 8."
</code></pre>
<p>The pyexpander delimiter:</p>
<pre><code class="language-r">knit_expand(text = 'hello $(LETTERS[24]) and $(pi)!', delim = c("$(", ")"))
</code></pre>
<pre><code>## [1] "hello X and 3.14159265358979!"
</code></pre>
<p>Arbitrary R code:</p>
<pre><code class="language-r">knit_expand(text = 'you cannot see the value of x {{x=rnorm(1)}}but it is indeed created: x = {{x}}')
</code></pre>
<pre><code>## [1] "you cannot see the value of x but it is indeed created: x = 1.24831530310296"
</code></pre>
<pre><code class="language-r">res = knit_expand(text = c(' x | x^2', '{{x=1:5;paste(sprintf("%2d | %3d", x, x^2), collapse = "\n")}}'))
cat(res)
</code></pre>
<pre><code>## x | x^2
## 1 | 1
## 2 | 4
## 3 | 9
## 4 | 16
## 5 | 25
</code></pre>
<p>The m4 example: <a href="https://en.wikipedia.org/wiki/M4_(computer_language)">https://en.wikipedia.org/wiki/M4_(computer_language)</a></p>
<pre><code class="language-r">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)
</code></pre>
<pre><code>## <html>
## <h2>1. First Section</h2>
## <h2>2. Second Section</h2>
## <h2>3. Conclusion</h2>
## </html>
</code></pre>
<p>Build regression models based on a template; loop through all variables in <code>mtcars</code>:</p>
<pre><code class="language-r">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 = '')
</code></pre>
<pre><code># 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
```</code></pre>
</div>
<div class="include-after">
</div>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js" defer></script>
</body>
</html>