Not registered yet?
Register now! It is easy and done in 1 minute and gives you access to special discounts and much more!
This works for me, based on hints from Yui on github.
opts_knit$set(progress = F, verbose = F)
opts_chunk$set(comment=NA, warning=FALSE,message=FALSE,fig.width=6, echo=F)
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.
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
Two questions: When you want both figures to be keep, use
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:
# Our figure is 10 wide, not 2
# Our figure is 2 wide, not 10
Agreed on the fig.keep='all' part. Thanks for that, I appreciate it...but the whole point of the main part of the question was NOT to break up the code into separate chunks...as I mentioned, I am running a single function that produces different size output images to be created and setting the fig.width for the whole chunk to be ok for some images makes others either too small or too big, thus I wanted to edit the code to put a opts_current$set(fig.width=x) in between the plots in the function.
@h.l.m the short answer is no, it is not possible to use opts_current in that way.
@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...?
@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