3

I'm essentially trying to modify this answer to programmatically produce chunks with plots for each level of a variable.

In my particular case, however, I'm passing along a character vector to be used for subsequent subsetting, which seems to be the source of the code failure.

# My report (test.Rmd)

```{r}
library(ggplot2)
library(knitr)
data(diamonds)
diamonds$cut <- factor(gsub(" ", "_", diamonds$cut)) # Get rid of spaces
cut.levels <- levels(diamonds$cut)
```

## Generate report for each level of diamond cut
```{r, include=FALSE}
src <- lapply(cut.levels, function(cut) knit_expand(file = "template.Rmd"))
```

`r knit(text = unlist(src))`

And the template (template.Rmd):

```{r, results='asis', echo = FALSE}
cat("### {{cut}} cut")
```

```{r {{cut}}-cut, eval = FALSE}
with(subset(diamonds, cut == "{{cut}}"), 
     plot(carat, price, main = paste("{{cut}}", "cut"))
)

```

Running this with the second chunk in template.Rmd set to eval=FALSE produces the expected output - a series of headers for each chunk with the echoed code. However, the substituted values from the cut.levels character string in the subset call have lost their quotes which, I expect, causes the following error when the eval=FALSE chunk option is removed:

Quitting from lines 6-8 (test.Rmd) 
Quitting from lines 12-12 (test.Rmd) 
Error in eval(expr, envir, enclos) : object 'Fair' not found
Calls: <Anonymous> ... with -> subset -> subset.data.frame -> eval -> eval

It's now looking for the object Fair rather than those records with cut == "Fair".

Thanks for your assistance!

> sessionInfo()
R version 3.2.1 (2015-06-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     
Community
  • 1
  • 1
adamdsmith
  • 920
  • 1
  • 9
  • 15

1 Answers1

5

I see 2 problems.

First, as you pointed out, {{cut}} is expanded without quotes so you'll need to wrap the tag in quotes. Second, your closing paren for plot() is misplaced. It should run with the following edits:

### {{cut}} cut

```{r {{cut}}-cut}
with(subset(diamonds, cut == "{{cut}}"), 
     plot(carat, price, main = paste("{{cut}}", "cut"))
)
```
aaronwolen
  • 3,723
  • 1
  • 20
  • 21
  • That's painfully obvious. Thanks @aaronwolen. Although, now only the first header is recognized as an actual header in the html document. – adamdsmith Sep 21 '15 at 16:22
  • I usually iterate on a vector of integers (of the same length as `cut.levels`) and get the i-th position of the character vector in the template. – Tutuchan Sep 21 '15 at 16:30
  • The header issue is the result of setting `echo = FALSE` in the first chunk of `template.Rmd`. The headers work as expected with `echo = TRUE`. I wonder if there's a way to get the headers without echoing the code? – adamdsmith Sep 21 '15 at 16:42
  • Sure. You actually don't need to generate the headers within a chunk. I edited my answer to demonstrate, just note there should be a blank space preceding the header. – aaronwolen Sep 21 '15 at 16:47