Last active
March 27, 2017 10:04
-
-
Save jfreels/6504922 to your computer and use it in GitHub Desktop.
knit_expand() example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?