#### @Steve Powell 2013-02-22 08:02:37

This works for me, based on hints from Yui on github.

\documentclass{article}

\begin{document}
<<setup,echo=FALSE>>=
opts_knit$set(progress = F, verbose = F) opts_chunk$set(comment=NA, warning=FALSE,message=FALSE,fig.width=6, echo=F)
kexpand=function(){
cat(knit(
text=knit_expand(text=
"<<yfig-{{cap}}-,fig.cap='{{cap}}',results='markup',echo=FALSE,fig.height={{figheight}},out.height={{outheight}}>>=\n
.q\n
@"
)
))}
@

<<results='asis'>>=
library(ggplot2)
.q=qplot(1:10);cap="first caption";figheight=9;outheight=90
kexpand()
.q=qplot(1:20);cap="second caption";figheight=12;outheight=160
kexpand()

@
\end{document}


So one key thing is to set progress and verbose to F otherwise they destroy the output. Then the little function kexpand expands an inline template which is typed as text as part of the function. Then you can define your plot as .q and your caption as cap, and your heights etc. You could adapt the function to control other options. Strangely, .q and the caption don't have to be parameters for the function, you can just set them in the current environment and they get picked up by the function anyway. Don't know if this is good practice or why it works but it does.

#### @rumtscho 2016-06-23 19:24:31

For some reason, this only works for me if I follow your example exactly. If I have plots other than ggplot, (base R, metafor forest plot) it doesn't work. Even weirder, if I have the heights in a vector and loop through the vector creating figures, it does not work either. Do you have any idea if your method can be changed to work for my case? stackoverflow.com/questions/37998364

#### @Dieter Menne 2012-07-26 12:58:23

Two questions: When you want both figures to be keep, use

{r fig.keep='all'}


Default only keeps the unique plots (because your two plots are identical, the second one is removed; see the knitr graphics manual for details).

Global chunk options are active when the next chunk(s) open:

{r}
opts_chunk$set(fig.width=10)  {r} opts_chunk$set(fig.width=2)
# Our figure is 10 wide, not 2
plot(1:1000)


{r}
# Our figure is 2 wide, not 10

#### @Yihui Xie 2012-07-26 15:00:57

@h.l.m the short answer is no, it is not possible to use opts_current in that way.

#### @h.l.m 2012-07-27 02:03:12

@Yihui the lack of ability to change chunk options mid chunk...is that likely to change or is that going to remain? Is it possible to request it as an enhancement for the future...?

#### @Yihui Xie 2012-07-27 04:40:27

@h.l.m at best I can support fig.width=a_numeric_vector`, but that will not be an ideal solution; it gives you the correct output size but not the original recording size; figures may look ugly in that way. If that is acceptable, a github issue is fine

### [SOLVED] How to make a great R reproducible example

• 2011-05-11 11:12:02
• Andrie
• 285519 View
• 2474 Score
• Tags:   r r-faq

### [SOLVED] Can't get bookdown rmd_subdir ["dir"] to work

• 2018-07-31 06:58:24
• 136 View
• 2 Score
• Tags:   r yaml bookdown

### Error related to load table in R

• 2018-07-12 18:32:08
• unitedsaga
• 283 View
• 0 Score
• Tags:   r

### [SOLVED] htmlParse fails to load external entity

• 2014-05-02 14:28:17
• Tumbledown
• 8636 View
• 7 Score
• Tags:   xml r