[R] how to label two figures in the same chunk independently with knitr

Yihui Xie xie at yihui.name
Wed Jan 9 19:36:53 CET 2013

Hi Francesco,

This is an advanced topic in knitr; it is called a chunk hook:

Sorry for the confusion on the name "par"; you can call it anything, e.g. mypar

knit_hooks$set(mypar = function(before, options, envir) {
  if (before) par(mar = c(4, 4, .1, .1))
opts_chunk$set(mypar = TRUE)

For par(bg=rgb(runif(1), runif(1), runif(1))), it is nothing but a
line of normal R code.

Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA

On Wed, Jan 9, 2013 at 2:59 AM, Francesco Sarracino
<f.sarracino at gmail.com> wrote:
> Dear Yihui,
> thanks a lot for your kind reply. Your solution is very elegant and
> versatile.
> However, there is a point that is obscure to me and I didn't manage to fully
> understand them after looking at the Knitr manual and graphic manual.
> The issue concerns the hook:
> knit_hooks$set(par = function(before, options, envir) {
>   if (before) par(mar = c(4, 4, .1, .1))
> })
> why do you set par as a function?
> moreover, below you write:
> par(bg=rgb(runif(1), runif(1), runif(1)))
> does this mean that before = rgb(runif(1)); options = runif(1) and envir =
> runif(1) ?
> and what does this produce? I don't understand what's going on, can you
> please help me or address me to some documentation?
> thanks in advance for your kind help,
> f.

More information about the R-help mailing list