在knitr中仅显示缓存中生成的图形的选定数字

时间:2014-05-01 12:05:39

标签: r knitr

如果我有一个生成4个数字的块并且我想保留它们(fig.keep = all),是否可以使用缓存选项仅显示第二个?我看到echo = 2:5可以选择,但似乎fig.show = 2是不可能的。是\ includegraphcis {fig2.pdf}的最佳方法吗?

感谢。

1 个答案:

答案 0 :(得分:0)

如果您不想保留所有数据,您只需使用: 使用参数fig.keep=c(2,4)

可重复的例子:

\documentclass[a4paper]{article}
\begin{document}
<<load_libraries, echo = FALSE, eval = TRUE, results ="hide">>=
library(knitr) 
@

<<multiplefig, fig.keep=c(2,4),fig.height=4,fig.with=10, out.width='.8\\linewidth'>>=
mapply(function(X)plot(X,main=X),c(1:4))
@

\end{document}

enter image description here

但是,由于你想保留数字,我会用旧方法进行扫描,使用pngpdf保存数字。

可重复的例子:

\documentclass[a4paper]{article}
\usepackage{graphicx} 
\graphicspath{{figure/}}
\begin{document}


<<multiplefig,  echo=FALSE>>=
for (i in 1:4){
  png(file=paste0(getwd(),"/figure/fig",i,".png"))
  plot(1,1,main=i,cex.main=4)
  dev.off()
}
@
This plot shows only figure \ref{fig:fig2} but 4 other are saved
\begin{figure}[htbp]
\centering
\includegraphics[width=0.5\textwidth]{fig2.png}
\caption{}
\label{fig:fig2}
\end{figure}

\end{document}

enter image description here

相关问题