I am trying to create a generic rmarkdown template that will perform analysis on a data frame. I would like to be able to transfer the rmarkdown file in the data frame, and not hardcode it every time.
Below is a snippet that I experimented with. You can see that at the top I have to load the data frame (mtcars). I also manually identify independent variables (ivs) and dependent variables (dvs). I would like to pass them as parameters. I am trying to run a quick and dirty version of 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 would like to have code that looks something like this to generate 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?
source share