--- Prof Brian Ripley <[EMAIL PROTECTED]> wrote:

> savePlot is just an internal version of dev.copy, part of the support for 
> the menus on the windows() graphics device.
> 
> It is described in `An Introduction to R' (the most basic R manual).

"the most basic R manual" doesn't quite answer my question. 

by itself, dev.copy doesn't copy the width and height of the device whereas 
savePlot
copies whatever is displayed on the screen giving me 
what-you-see-is-what-you-save
capabilities (but only under the Windows OS). 

i can get pretty close to this in linux by writing a function to save the
plot to a pdf device:
<<label=first.ar.1, results=hide>>=
        # no savePlot in Linux ... so write my own function
        savePlotAsPdf<- function(pdfname, from=dev.cur()) {
                from<- from
                pdf(pdfname, width=width, height=height)
                to<- dev.cur()
                dev.set(from)
                dev.copy(which=to)
                dev.off()
        }
        # a long AR process is best viewed in a wide window ... 
        # width & height are now variables
        width<- 20; height<- 5
        x11(width=width, height=height)
        sp<- make.ar.1(alpha=.5, n=800)
        plot(sp, type="l", col="blue")
        # width & height via dynamic scoping in savePlotAsPdf
        savePlotAsPdf("ar.pdf")
@

the only (minor) inconvenience is that i have to specify width and height as 
variables to
take advantage of dynamic scoping in order to minimize mess.

while this is a workaround, via dev.copy, as you pointed out, it still doesn't 
answer why
Sweave doesn't like x11() devices (or windows() devices under the Windows OS 
for that
matter) within figure environments. 

perhaps this is a question for the package maintainer? but i was hoping that 
all the avid
Sweave users would pitch in with how they work with graphics in practice.

here is a revised .Rnw example illustrating the above:

% >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> example-linux2.Rnw
\documentclass[a4paper]{article}

\begin{document}

\noindent This is an example of how I can use \texttt{Sweave} under Linux. 

<<echo=false,results=hide>>=
        # create a simple AR process:
        make.ar.1<- function(alpha=1,n=300) {
                Z<- rnorm(n); 
                Y<- numeric(n); 
                Y[1]<- Z[1]; 
                for (i in 2:n) Y[i]<- alpha*Y[i-1]+Z[i]; 
                return(Y)
        }
@

\texttt{Sweave} doesn't like the [EMAIL PROTECTED](width=width, height=height)@ 
command 
in the following code chunk if it is placed within a \texttt{figure} 
environment.
Instead, I have to save the plot to a file and then I use [EMAIL PROTECTED]@ in 
the \texttt{figure} environment. This isn't a bad thing, as it allows me to 
fine-tune
the \LaTeX\ graphics placement.
<<label=first.ar.1, results=hide>>=
        # no savePlot in Linux ... so write my own function
        savePlotAsPdf<- function(pdfname, from=dev.cur()) {
                from<- from
                pdf(pdfname, width=width, height=height)
                to<- dev.cur()
                dev.set(from)
                dev.copy(which=to)
                dev.off()
        }
        # a long AR process is best viewed in a wide window ... 
        # width & height are now variables
        width<- 20; height<- 5
        x11(width=width, height=height)
        sp<- make.ar.1(alpha=.5, n=800)
        plot(sp, type="l", col="blue")
        # width & height via dynamic scoping in savePlotAsPdf
        savePlotAsPdf("ar.pdf")
@
\begin{figure}
\begin{center}
%       i retain direct control over graphics in LaTeX; i can fine-tune the 
%       the graphics placement as much as i want:
        \includegraphics[width=14.5cm]{./ar.pdf}
\caption{An AR(1) process of length~\protect\Sexpr{length(sp)} 
is best viewed in a wide window.}
\end{center}
\end{figure}

Under X, then,
\begin{itemize}
\item Why doesn't \texttt{Sweave} like [EMAIL PROTECTED](width=width, 
height=height)@?
\end{itemize}
There are, however, advantages to doing things this way:
\begin{itemize}
\item I can save the plot to a file without writing any other code;
\item I can include the saved plot in my \LaTeX\ figure, allowing me to 
        fine-tune with the [EMAIL PROTECTED]@ command.
\end{itemize}

\end{document}
% <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< example-linux2.Rnw




> 
> On Sat, 19 Aug 2006, Thomas Harte wrote:
> 
> > the problem is a little hard to explain; the .Rnw files (below)
> > probably do a better job, but here goes ...
> > 
> > Sweave doesn't like it when i size a graphical device in a code
> > chunk using either, e.g.:
> > 
> >     windows(width=20, height=5)
> > 
> > in Windows, or, e.g.
> > 
> >     x11(width=20, height=5)
> > 
> > under X, when i then plot something in said device and try to 
> > include this graphical output in the resulting document.
> > 
> > Sweave does not object to my writing code chunks in the above
> > manner, so long as i do not wish to include the code in a LaTeX 
> > figure environment.
> > 
> > oftentimes i want to do precisely what Sweave doesn't appear
> > to allow. for example, with time-series data, i want to see a 
> > wide window on the screen as i code, and then i want to include 
> > the graphical output in my document the way that i fine tuned 
> > it on the screen. i don't want to write two pieces of code:
> > the first, to view output on the sceen; the second, to save
> > the output to a .pdf file for inclusion in the document.
> > 
> > some example .Rnw files should illustrate my plight.
> > suggestions on a workaround (i.e. how to do what i describe in 
> > linux/X) welcome.
> > 
> > 
> > % >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> example-windows.Rnw
> > \documentclass[a4paper]{article}
> > 
> > \begin{document}
> > 
> > \noindent This is an example of what I can do on Windows. Unhappily, I seem 
> > to be
> > able to squeeze marginally more out of \texttt{Sweave} \emph{chez\/} Bill 
> > Gates
> > than I can under Linux. Ho, hum.
> > 
> > <<echo=false,results=hide>>=
> >     # create a simple AR process:
> >     make.ar.1<- function(alpha=1,n=300) {
> >             Z<- rnorm(n); 
> >             Y<- numeric(n); 
> >             Y[1]<- Z[1]; 
> >             for (i in 2:n) Y[i]<- alpha*Y[i-1]+Z[i]; 
> >             return(Y)
> >     }
> > @
> > 
> > <<label=ar.1>>=
> >     # a long AR process is best viewed in a wide window:
> >     windows(width=20, height=5)
> >     sp<- make.ar.1(alpha=.5, n=800)
> >     plot(sp, type="l", col="blue")
> >     # WISIWIS: What I See Is What I Save ;)
> >     savePlot("ar",type="pdf")
> > @
> > \begin{figure}
> > \begin{center}
> > %   imporantly, by saving the plot i have direct control over graphics in 
> > LaTeX, 
> > %   and i can fine-tune the the graphics placement as much as i want:
> >     \includegraphics[width=14.5cm]{./ar.pdf}
> > \caption{An AR(1) process of length~\protect\Sexpr{length(sp)} 
> > is best viewed in a wide window.}
> > \end{center}
> > \end{figure}
> > 
> > 
> > \noindent Had I tried to do the following, \texttt{Sweave} would have blown 
> > up!
> > \begin{verbatim}
> >     <<label=ar.1>>=
> >             windows(width=20, height=5)     # <- this is the offending 
> > command:
> >             sp<- make.ar.1(alpha=.5, n=800)
> >             plot(sp, type="l", col="blue")
> >     @
> >     \begin{figure}
> >     \begin{center}
> >     <<fig=true>>=
> >     <<ar.1>>
> >     @
> >     \caption{An AR(1) process of length~\protect\Sexpr{length(sp)} 
> >     is best viewed in a wide window.}
> >     \end{center}
> >     \end{figure}
> > \end{verbatim}
> > 
> > 
> > \noindent The take-home message is that \texttt{savePlot} saves the day 
> > under
> Windows.
> > As far as I know, there is no equivalent under Linux, or rather, under X.
> > 
> > In Windows, then,
> > \begin{itemize}
> > \item I can plot the way I want on the screen;
> > \item I can save that plot to a file without writing any other code;
> > \item I can include the saved plot in my \LaTeX\ figure, allowing me to 
> >     fine-tune with the [EMAIL PROTECTED]@ command.
> > \end{itemize}
> > Strike one for the Evil Empire.
> > 
> > \end{document}
> > % <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< example-windows.Rnw
> > 
> > 
> > 
> > % >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> example-linux.Rnw
> > \documentclass[a4paper]{article}
> > 
> > \begin{document}
> > 
> > \noindent This is an example of the hapless state of my \texttt{Sweave}ing 
> > under
> Linux. 
> > 
> > <<echo=false,results=hide>>=
> >     # create a simple AR process:
> >     make.ar.1<- function(alpha=1,n=300) {
> >             Z<- rnorm(n); 
> >             Y<- numeric(n); 
> >             Y[1]<- Z[1]; 
> >             for (i in 2:n) Y[i]<- alpha*Y[i-1]+Z[i]; 
> >             return(Y)
> >     }
> > @
> > 
> > \noindent Because of the [EMAIL PROTECTED](width=20, height=5)@ command, 
> > I can't embed the graphical output that the following piece of code 
> > produces in my document, although I can view the results on screen:
> > <<label=first.ar.1>>=
> >     # a long AR process is best viewed in a wide window:
> >     x11(width=20, height=5)
> >     sp<- make.ar.1(alpha=.5, n=800)
> >     plot(sp, type="l", col="blue")
> >     # no savePlot ... can't seem to do anything with this plot
> >     # if i try to include this code in a figure environment then
> >     # Sweave blows up
> >     # so i have to stop here :(
> > @
> > 
> > \noindent Instead, I have to do something like the following, which has the
> unfortunate 
> > side effects of disallowing me from seeing the graphical output on the 
> > screen, and,
> > probably
> > more importantly, of duplicating the above code:
> > <<label=ar.1,echo=true>>=
> >     sp<- make.ar.1(alpha=.5, n=800)
> >     pdf("ar.pdf", width=20, height=5)
> >     plot(sp, type="l", col="blue")
> >     dev.off()
> > @
> > \begin{figure}
> > \begin{center}
> > %   at least i still retain direct control over graphics in LaTeX; i can 
> > fine-tune the
> 
> > %   the graphics placement as much as i want:
> >     \includegraphics[width=14.5cm]{./ar.pdf}
> > \caption{An AR(1) process of length~\protect\Sexpr{length(sp)} 
> > is best viewed in a wide window.}
> > \end{center}
> > \end{figure}
> > 
> > Under X, then,
> > \begin{itemize}
> > \item I have to use a device such as \texttt{pdf} and I lose the ability to 
> > first 
> >     see the output on screen;
> > \item I can still save that plot to a file without writing any other code;
> > \item I can still include the saved plot in my \LaTeX\ figure, allowing me 
> > to 
> >     fine-tune with the [EMAIL PROTECTED]@ command.
> > \end{itemize}
> > 
> > \end{document}
> > % <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< example-linux.Rnw
> > 
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> > 
> 
> -- 
> Brian D. Ripley,                  [EMAIL PROTECTED]
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to