Question

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?

Was it helpful?

Solution 2

I think you can use knit2html from knitr package to do the "magic".

  1. 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)
    }
    
  2. In test.R you prepare your data :

    myDF <- read.delim("mydata.tab")
    ivs <- c("iv1", "iv2", "iv3")
    dvs <- c("dv1", "dv2", "dv3")
    
  3. You compile using knitr

    Knit2html('mydoc.Rmd')
    

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top