By h.l.m


2012-07-26 12:04:53 8 Comments

Hi I would like to change chunk options, mid chunk, without having to create a new chunk..

running the following code I would expect to get two very different size outputs, but for some reason this does not seem to be the case.

Also the second plot doesn't plot at all...(it does when you change it to plot(2:1000)...but either way the second output is the same size as the first. both fig.width=7. What am I doing wrong?

Pls note the importance of 'mid chunk' the reason for this is that I would like to change the chunk options several times when running a function to get different outputs of different sizes.

```{r}
sessionInfo()

opts_chunk$set(fig.width=3)

plot(1:1000)

opts_chunk$set(fig.width=10)

plot(1:1000)

```

the sessionInfo output is as follows:

## R version 2.15.1 (2012-06-22)
## Platform: i386-pc-mingw32/i386 (32-bit)
## 
## locale:
## [1] LC_COLLATE=English_United Kingdom.1252 
## [2] LC_CTYPE=English_United Kingdom.1252   
## [3] LC_MONETARY=English_United Kingdom.1252
## [4] LC_NUMERIC=C                           
## [5] LC_TIME=English_United Kingdom.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices datasets  utils     methods   base     
## 
## other attached packages:
## [1] knitr_0.7
## 
## loaded via a namespace (and not attached):
## [1] digest_0.5.2   evaluate_0.4.2 formatR_0.5    parser_0.0-16 
## [5] plyr_1.7.1     Rcpp_0.9.13    stringr_0.6    tools_2.15.1  

html preview

2 comments

@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
opts_chunk$set(fig.width=10)
plot(1:1000)
```

@h.l.m 2012-07-26 14:03:15

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.

@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

Related Questions

Sponsored Content

64 Answered Questions

[SOLVED] How do you split a list into evenly sized chunks?

23 Answered Questions

[SOLVED] How to make a great R reproducible example

  • 2011-05-11 11:12:02
  • Andrie
  • 313396 View
  • 2473 Score
  • 23 Answer
  • Tags:   r r-faq

5 Answered Questions

[SOLVED] Couldn't quit R session or couldn't quit RStudio

  • 2015-12-30 15:27:40
  • Windtalker
  • 17145 View
  • 10 Score
  • 5 Answer
  • Tags:   r rstudio

1 Answered Questions

Error related to load table in R

  • 2018-07-12 18:32:08
  • unitedsaga
  • 305 View
  • 0 Score
  • 1 Answer
  • Tags:   r

1 Answered Questions

Trouble with googlevis using Knitr HTML in RStudio 0.98.94

2 Answered Questions

[SOLVED] htmlParse fails to load external entity

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

1 Answered Questions

[SOLVED] Run R script on Amazon EC2

Sponsored Content