I had the same difficulty. Here is one approach that worked:
p1 <- function(x, knitr=FALSE){
plot(x)
if(!knitr) dev.new()
plot(x^2)
}
The argument is knitr=used only in creating a vignette.
Now in the file .Rnwin /vignettesput something like:
%\VignetteEngine{knitr::knitr}
%\VignetteIndexEntry{p1}
\documentclass{article}
\begin{document}
<<setup, include=FALSE>>=
library("knitr")
opts_chunk$set(eval=TRUE,
echo=TRUE,
results=c('markup', 'asis', 'hold', 'hide')[1],
fig.path=c('figure', 'figure/minimal-')[1],
fig.keep=c('high', 'none', 'all', 'first', 'last')[1],
fig.align=c('center', 'left', 'right', 'default')[1],
fig.show=c('hold', 'asis', 'animate', 'hide')[1],
dev=c('pdf', 'png', 'tikz')[1],
fig.width=7, fig.height=7,
fig.env=c('figure', 'marginfigure')[1],
fig.pos=c('', 'h', 't', 'b', 'p', 'H')[1]
)
@
<<plot1>>
library("myPackage")
x <- seq(10)
p1(x, knitr=TRUE)
@
\end{document}