1

I have a long .Rnw document which consists mostly of text (typeset in LaTeX) with a few chunks here and there. I have also written a chunk which outputs a specific figure. The figure contains a plot, the values for the plot are currently read from a .csv file and some parameters like colors defined manually within the chunk.

Now I want to have the same figure in a different place in the document, but with different values for the plot and a few other parameters different. Ideally, I would like to include the chunk as a child twice, and pass parameters to it somehow, including the name of the .csv to be used for the plot values. I would hate to copy paste the chunk code with hardcoded parameters, as it is complex enough that potential changes will be difficult to synchronize.

How can I do such "parameterized reuse" of chunks?

update

As requested, a small example


This is saved as include-chunk-reuse.Rnw

<<toReuse, echo=FALSE, result='asis'>>=

l <- 25

@

\newlength{\mylength}
\setlength{\mylength}{\Sexpr{l}pt}

%Omitted: a lot of complicated LaTeX commands 

\rule{\mylength}{1pt}

This is the document which is supposed to reuse the chunk. It doesn't even compile, as it complains that the same label is used twice: Error in parse_block(g[-1], g[1], params.src) : duplicate label 'toReuse'

\documentclass{article}

\begin{document}

This is some text. And now comes a 25 pt wide line. 

<<first-figure, child='include-chunk-reuse.Rnw'>>=
@

This is some text. The next line is also 25 pt wide. But I would like to call the chunk in a way which makes it 50 pt wide instead. 


<<second-figure, child='include-chunk-reuse.Rnw'>>=

@

\end{document}
Community
  • 1
  • 1
rumtscho
  • 2,474
  • 5
  • 28
  • 42
  • Wrap the code into function, then call it with different inputs? – zx8754 Jul 08 '16 at 11:11
  • @zx8754 that's the exact question, how do call a chunk with inputs? – rumtscho Jul 08 '16 at 11:12
  • Can you provide small [reproducible example?](http://stackoverflow.com/questions/5963269) – zx8754 Jul 08 '16 at 11:14
  • I could probably spend some time creating an example, but I don't understand why such a general mechanism would depend on an example – rumtscho Jul 08 '16 at 11:16
  • I am not sure about .Rnw. But perhaps this [link](http://yihui.name/knitr/demo/externalization/) helps? In my Rmd files I also wrote R-functions which I can reuse in every chunk... – Christoph Jul 08 '16 at 11:31
  • 1
    @zx8754 I added an example of how I have been trying to solve it until now. – rumtscho Jul 08 '16 at 11:37
  • 1
    @Christoph I don't want to reuse an R function, I want to reuse the whole chunk, which is mostly TikZ. R is only used to read the necessary values from the .csv and pass them as dimensions to the TikZ code. – rumtscho Jul 08 '16 at 11:43

2 Answers2

0

For the knitr part to work simply leave out the chunk-name in the child document, then you don't have the duplicated label and the knitr part works.

Passing Parameters does not really work as far as I know, but you can just set a global variable before including the child. (For example \Sexpr{l <- 200}

You are still redefining \mylength which is why LaTeX will throw an error, so move the first definition of \mylength from the child to the main document.

snaut
  • 2,261
  • 18
  • 37
0

The example below demonstrates two ways to reuse and parametrize a chunk.

Reusing Chunks

The mechanism is explained here. Basically, the simplest way to reuse a chunk is to add another empty chunk with the same label. Alternatively, the chunk option ref.label lets a chunk inherit another chunks code.

Both approaches of reusing chunks are largely equivalent – with one exception: figures generated in chunks are saved as chunklabel-i.pdf, where i is the figure index counted by chunk. Therefore, if a chunk is reused by repeating its label, figure i from the second use will overwrite figure i from the first use. This is the reason why I use ref.label (and thus distinct chunk labels) in the example below (otherwise, the points on both plots would be green).

In the example below, I used eval = FALSE in order to prevent evaluation of the masterchunk where it is defined. An alternative would be to externalize the chunk and read it by read_chunk().

Parameterizing Chunks

The two most straightforward options to "pass" parameters to a chunk are

  • chunk options and
  • global variables

Also when reusing chunks, each use can set different chunk options. The example below exploits this to set different captions.

As all chunks run in the same environment, setting a variable in an early chunk affects subsequent chunks accessing this variable. In the example below, mycolor is modified this way.


\documentclass{article}
\begin{document}
<<masterchunk, eval = FALSE>>=
  plot(1:10, col = mycolor)
@

<<config1>>=
mycolor <- "red"
@

<<use1, ref.label = "masterchunk", fig.cap = "Red dots">>=
@

<<config2>>=
mycolor <- "green"
@

<<use2, ref.label = "masterchunk", fig.cap = "Green dots">>=
@
\end{document}
CL.
  • 14,577
  • 5
  • 46
  • 73