4

My package produces a number of graphs, often more than one at once.

Using x11 or windows devices there is no problem, knitr builds the vignette with images included.

However a requirement of submission to the CRAN repository is the use of dev.new for platform independent plotting. If I replace x11 or windows with dev.new then no images appear in my vignette.

Is there a solution to this? At first I thought this was related to plotting in RStudio but use of the new argument dev.new(noRStudioGD = FALSE) did not help. Additionally building the package from the command line did not solve the problem.

Cheers,

Tom

(Windows 7 x64) (R 3.1.1) (RStudio 0.98.507)

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
Tom C
  • 146
  • 5
  • 2
    typically you don't need to open a device yourself, knitr does it for you. The devices you mention are for interactive use, how do you plan the interaction with knitr, which runs in its own non-interactive session? – baptiste Sep 09 '14 at 11:00

2 Answers2

2

The short answer is, you do not use dev.new() at all (or dev.off() or dev.whatever...). If you want a longer answer, please include a minimal reproducible example demonstrating what your problem really is.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • Thanks Yihui and baptiste, you're dead right of course, I didn't need to open a new device at all. Cheers, Tom – Tom C Sep 09 '14 at 19:55
  • 2
    What if I have an analysis function that produces multiple plots? I need to use `dev.new()` in order to have it work interactively. And even if it produces just a single plot, I like my functions to not override previous plots without having to manually do `dev.new()`... is there no way to get such functions to work with `knitr`? – Eike P. Apr 01 '15 at 12:33
1

I had the same difficulty. Here's one approach that worked:

p1 <- function(x, knitr=FALSE){
    plot(x)
    if(!knitr) dev.new()
    plot(x^2)
}

The argument knitr= is only used in vignette building.

Now, in the .Rnw file in /vignettes put something like:

%\VignetteEngine{knitr::knitr}
%\VignetteIndexEntry{p1}
\documentclass{article}

\begin{document}

<<setup, include=FALSE>>=
library("knitr")
### Set global chunk options
opts_chunk$set(eval=TRUE,
   ## text results
   echo=TRUE,
   results=c('markup', 'asis', 'hold', 'hide')[1],
   ## plots
   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, #inches
   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}
dardisco
  • 5,086
  • 2
  • 39
  • 54