h.l*_*l.m 13 r options chunks knitr
嗨,我想更改大块选项,中间块,而不必创建一个新的块..
运行以下代码我希望得到两个非常不同的大小输出,但由于某种原因,似乎并非如此.
第二个情节根本没有绘图...(当你把它改为绘图时(2:1000)......但无论哪种方式,第二个输出都与第一个相同.两者fig.width=7.我在做什么错误?
请注意'mid chunk'的重要性,原因是我想在运行函数时多次更改块选项以获得不同大小的不同输出.
```{r}
sessionInfo()
opts_chunk$set(fig.width=3)
plot(1:1000)
opts_chunk$set(fig.width=10)
plot(1:1000)
```
Run Code Online (Sandbox Code Playgroud)
sessionInfo输出如下:
## 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
Run Code Online (Sandbox Code Playgroud)

根据Yui在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}
Run Code Online (Sandbox Code Playgroud)
因此,关键是将进度和详细设置为F,否则会破坏输出.然后,小函数kexpand扩展了一个内联模板,该模板作为函数的一部分输入为文本.然后,您可以将图表定义为.q,将标题定义为上限,高度等.您可以调整函数以控制其他选项.奇怪的是,.q和标题不一定是函数的参数,你可以在当前环境中设置它们,无论如何它们都被函数拾取.不知道这是好的做法还是为什么有效,但确实如此.
两个问题:当你想要保留两个数字时,请使用
```{r fig.keep='all'}
Run Code Online (Sandbox Code Playgroud)
默认仅保留独特的图(因为您的两个图是相同的,第二个图被删除; 有关详细信息,请参阅knitr图形手册).
当下一个块打开时,全局块选项处于活动状态:
```{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)
```
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2984 次 |
| 最近记录: |