Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass variables into an R markdown .Rmd file?

Tags:

r

r-markdown

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?

like image 487
Jim Avatar asked Sep 21 '13 06:09

Jim


People also ask

How do I assign a variable in Markdown?

So I looked for a way to assign a variable in Markdown. The idea is pretty simple, Markdown has a concept of reference links or reference-style links that allows us to define a value to a reference variable and then refer to that value later in the same document. This is my [random value] located somewhere in my Markdown document.

How do I create a template in Rmarkdown?

First, we need an RMarkdown file (.Rmd). This is largely the same as your usual .Rmd file, and I strongly encourage you to develop it like one. i.e. write your single .Rmd file and convert it into a special use case to be a template. Working like this makes debugging a whole lot easier. Here’s an example of a “normal” .Rmd:

How do I split a Markdown document in R?

When you feel an R Markdown document is too long, you may consider splitting it into shorter documents, and include them as child documents of the main document via the chunk option child. The childoption takes a character vector of paths to the child documents, e.g.,

What is a Markdown reference link?

The idea is pretty simple, Markdown has a concept of reference links or reference-style links that allows us to define a value to a reference variable and then refer to that value later in the same document. This is my [random value] located somewhere in my Markdown document.


3 Answers

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.

like image 142
Pete900 Avatar answered Oct 26 '22 16:10

Pete900


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')
    
like image 44
agstudy Avatar answered Oct 26 '22 15:10

agstudy


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

like image 36
SamGG Avatar answered Oct 26 '22 17:10

SamGG