deman23
deman23

Reputation: 87

Knitr R : Grouping multiple plots in to HTML

I am very new to Knitr and I am trying to do the following in a R markdown file.

I have a set of names, for each name I make two plots. I need to get a HTML file for each name, containing the respective plots.

{r, echo=FALSE} for name in setofNames{ barplot(xx) barplot(yy) }

I am quite lost on how to do this. Does any one have any ideas?

EDIT: I am able to generate different HTML files now for each name, using stitch(). However, I don't get all the plots, the code I've retains only the last iteration. I've also explored the opts_chunk() feature, but in vain. It probably has something to clear the cache with, but I am not sure.

Below is the piece of code:

for name in setofNames{
opts_chunk$set(echo=FALSE, fig.keep='all', fig.show='asis')
fname=paste(name,".html")
stitch_rhtml("../testSub.r",output=fname,envir=globalenv())
}

===testSub.r file===
barplot(xx)
barplot(yy)

Would appreciate some inputs.

Upvotes: 0

Views: 940

Answers (2)

CL.
CL.

Reputation: 14957

One solution is to use a "control" file that calls knitr several times (once per name). Each time knitr processes the same Rmd-template but with different data.

In the code below I exploited the fact that knitr by default uses the objects in the calling environment (see ?knit: envir = parent.frame()). Hence it is possible to modify an object in the "control" file and a subsequent call of knitr will use that object when processing the template.

(Of course, global variables could be avoided. Then, the control file would need to assign objects in a specific environment and pass this environment to knitr.)

The "control" file (control.R) could look like this:

library(knitr)

## Generate data
set.seed(1)
n <- 1000
dat <- data.frame(
  name = sample(x = LETTERS, size = n, replace = TRUE),
  value = rnorm(n))

## knit the template once per "name"

lapply(X = levels(dat$name), FUN = function(name) {
  currentSubset <- dat[dat$name == name, ]
  knit2html(input = "template.Rmd", output = sprintf("output_%s.html", name))
})

template.Rmd:

```{r}
op <- par(mfrow = c(1, 2))

plot(currentSubset$value, col = "green", main = name)
plot(currentSubset$value, col = "red", main = name)

par(op)
```

This generates a separate HTML file (output_[Letter].html) for each letter in levels(dat$name).

Note that each call to knit2html overrides the plots in the figure directory. However, this does not matter because the HTML files do not reference external figures but contain the figures in data URIs. This is due to markdown::markdownToHTML() which is called from knitr::knit2html():

Any local images linked using the <img> tag will be base64 encoded and included in the output HTML.

(Source: markdownToHTML)

Upvotes: 0

ephackett
ephackett

Reputation: 259

You could use the par function to get what you need. Also, I usually remove the "echo=FALSE" because it messes up my knitted html.

http://www.statmethods.net/advgraphs/layout.html.

Here is an example of text that gets entered together for the knitr:

```{r}
df<- replicate(100, runif(n=20))
par(mfrow=c(2,3))
for (i in 2:7) hist(df[,i],main=colnames(df)[i])
```

Let me know if you need more specific help and I'll edit this post.

Upvotes: 1

Related Questions