Skip to content

Instantly share code, notes, and snippets.

@jfreels
Last active March 27, 2017 10:04
Show Gist options
  • Save jfreels/6504922 to your computer and use it in GitHub Desktop.
Save jfreels/6504922 to your computer and use it in GitHub Desktop.
knit_expand() example
knit_expand() example
========================================================
```{r, label='setup', include=FALSE}
require(data.table)
require(PerformanceAnalytics)
require(reshape2)
opts_chunk$set(eval=TRUE,echo=FALSE)
data(edhec)
edhec<-data.table(melt(data.frame(date=index(edhec),coredata(edhec)),id.vars='date'))
setkey(edhec,variable,date)
```
```{r, results='asis'}
template = c('## Histogram of {{i}}',
'```{r}',
'hist(edhec["{{i}}"]$value)',
'```')
src = lapply(as.character(unique(edhec$variable)), function(i) {
knit_expand(text=c(template))
})
# knit the source
cat(knit(text = unlist(src), quiet = TRUE))
```
@psychemedia
Copy link

What's the idiomatic way of passing in multiple variables to the template, eg where one or two are fixed value variables, and two others come from a particular row in the original dataframe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment