Reputation: 11379
I'm trying to create a generic rmarkdown template that will do analysis on a data frame. I'd like to be able to pass in a data frame to a rmarkdown file instead of hard-coding it each time.
Below is a snippet I've been experimenting with. You can see that at the top I have to load the data frame (mtcars). I also manually identity the independent variables (ivs) and dependent variables (dvs). I'd like to pass these in as parameters. I'm trying to do a quick and dirty version of the SPSS Explore functionality. "Explore.Rmd":
```{r}
library(ggplot2)
data(mtcars)
mtcars$am <- factor(mtcars$am, levels=c(0,1), labels=c("Manual", "Automatic"))
df <- mtcars
ivs <- c("cyl", "disp", "hp", "drat", "wt", "am", "qsec")
dvs <- c("mpg", "qsec")
```
Histograms
-------------------------------------
```{r}
for (v in union(ivs, dvs))
{
hist <- ggplot(df, aes_string(x=v)) + geom_histogram()
print(hist)
}
```
I'd like to have code that looks something like this to generate the HTML using knitr or something similar.
myDF <- read.delim("mydata.tab")
ivs <- c("iv1", "iv2", "iv3")
dvs <- c("dv1", "dv2", "dv3")
magic("Explore.Rmd", myDF, ivs, dvs) # <- how do I do this part?
So, is it possible to have a static rmarkdown file and pass parameters to it? Or would there be another way to accomplish what I'm trying to do?
Upvotes: 13
Views: 18121
Reputation: 2166
Another option is to list your variables using params
in the rmarkdown::render
function, see: http://rmarkdown.rstudio.com/developer_parameterized_reports.html.
First, declare and provide default values for parameters in the YAML of the rmarkdown document:
---
title: My Document
output: html_document
params:
df: !r data(mtcars); mtcars
ivs: ["cyl", "disp", "hp", "drat", "wt", "am", "qsec"]
dvs: ["mpg", "qsec"]
---
These are then accessible in the report body through the list params
:
Histograms
-------------------------------------
```{r}
for (v in union(params$ivs, params$dvs))
{
hist <- ggplot(params$df, aes_string(x=v)) + geom_histogram()
print(hist)
}
```
Finally, override the default values by passing a list of named arguments to rmarkdown::render
:
myDF <- read.delim("mydata.tab")
ivs <- c("iv1", "iv2", "iv3")
dvs <- c("dv1", "dv2", "dv3")
rmarkdown::render("MyDocument.Rmd",
params = list(df = myDF, ivs = ivs, dvs = dvs))
Since the YAML defines default values, one need provide only what one wishes to override, e.g.,
rmarkdown::render("MyDocument.Rmd", params = list(ivs = c("cyl", "wt")))
will still use the mtcars
dataset, but only plots histograms for cyl
, wt
, mpg
, and qsec
.
Upvotes: 31
Reputation: 71
I think an alternative is given at https://github.com/yihui/knitr/issues/567
you will have to create these arguments in advance, e.g.
args='2013'
knit('../my.Rmd','test.html')
then knit()
will recognize args inside my.Rmd;
see the envir
argument if you want to understand the details
Upvotes: 1
Reputation: 121568
I think you can use knit2html
from knitr
package to do the "magic".
You define your markdown file like this and save it as mydoc.Rmd
```{r}
source('test.R')
```
```{r}
library(ggplot2)
for (v in union(ivs, dvs))
{
hist <- ggplot(myDF, aes_string(x=v)) + geom_histogram()
print(hist)
}
In test.R you prepare your data :
myDF <- read.delim("mydata.tab")
ivs <- c("iv1", "iv2", "iv3")
dvs <- c("dv1", "dv2", "dv3")
You compile using knitr
Knit2html('mydoc.Rmd')
Upvotes: 4