1

I have a standard piece of analysis to perform on multiple datasets and want to present them in one report using a template.

The analysis per dataset could look like this:

child.Rmd

## Name of dataset

```{r calculate_stats}
    summary(ds)
    nrows <- nrow(ds)
```
The number of rows in the dataset is `r nrows`

The full report has this structure:

parent.Rmd

# Report

```{r import_all_datasets}
    ...import all datasets form csv...
    ds.list <- c(ds1, ds2, ds3, ...)
```

for ds in ds.list
    run child.Rmd with ds as a parameter

An additional requirement is that I can run the child.Rmd report alone with a specified parameter. The linked answer in comments below uses double curly braces ({{i}}) and knit_expand replaces it with i in the parent environment. This is unsatisfactory as it makes it a faff to call child.Rmd on its own.

Is it possible for the child to be a parametrised report and for the parent to pass the child the list of parameters.

I'm just attempting to do this now by trying:

child.Rmd

---
output: pdf_document
params:
  ds: !r cars
  name: "cars"
---

`r params$name`
=====

```{r}
summary(params$ds)
nrows <- nrow(params$ds)
```

The number of rows in the dataset is `r nrows`

And passing params to child within parent.Rmd

James Owers
  • 7,948
  • 10
  • 55
  • 71
  • 1
    See [this answer](http://stackoverflow.com/questions/21729415/generate-dynamic-r-markdown-blocks/21730473#21730473). – Sam Dickson Dec 08 '15 at 18:28
  • 1
    Use functions vs copy-and-paste. Then you can do anything from `lapply()` to different datasets to simple function calls between `load` and `rm` – alexwhitworth Dec 08 '15 at 18:29
  • @SamDickson that very nearly solves my problem. Thank you. I'm just looking into how to use `knit_expand()` to call an .Rmd file [with parameters](http://rmarkdown.rstudio.com/developer_parameterized_reports.html). If you know how to do this, feel free to answer below and I'll accept. – James Owers Dec 08 '15 at 19:10
  • @Alex, I don't follow your solution. Could you provide an example as an answer below? – James Owers Dec 08 '15 at 19:13
  • 1
    @kungfujam I think your question is too vague for a more specific answer. Unless you create a more concrete example I think closing as a duplicate of Sam Dickson's link makes more sense than trying to write an answer. – Gregor Thomas Dec 08 '15 at 19:16
  • Ok @Gregor . I'm working on it now with a concrete example and will edit above. – James Owers Dec 08 '15 at 19:18
  • Does the new question clarify the difference or highlight my misunderstanding? – James Owers Dec 08 '15 at 20:10
  • It clarifies the question, but I don't think it's possible to do what you are asking for. The `{{i}}` is what makes it possible to call the child. If you want a stand alone `child.Rmd`, I'm afraid you'll have to find and replace `{{i}}` and save it under a different file name. – Sam Dickson Dec 08 '15 at 20:15
  • Damn. That will probably cause a lot of pain later. Is there a way to quickly replace ``{{ii}}`` and render? e.g. `rmarkdown::render(knit_expand('~/Downloads/child2.Rmd', i = "hello"), output_format='pdf_document', output_dir = "~/Downloads")` **(that doesn't work, `rmarkdown::render` expects a file!)** – James Owers Dec 08 '15 at 20:31
  • I guess a workaround could be to use a solution much like the [knit expand example given](http://stackoverflow.com/questions/21729415/generate-dynamic-r-markdown-blocks/21730473#21730473) and make a testParent.Rmd file that simply calls the `child.Rmd` template with one dataset; `child.Rmd` is never itself run, only called as a template. Unless there are any more satisfying solutions, I'll post this as the workaround solution in a few days. Thanks for everyone's input. – James Owers Dec 08 '15 at 21:02
  • Just as an update, this solution currently won't run for me. I'm unsure why. I will try and reproduce the error with an open dataset example soon. – James Owers Dec 09 '15 at 13:17

0 Answers0