# Re: [R] issues with Sweave and inclusion of graphics in a document

From: Thomas Harte <thomas.harte_at_yahoo.com>
Date: Sat 26 Aug 2006 - 04:05:48 EST

> 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).

	# 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 \verb@x11(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 \verb@\includegraphics@ 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 \verb@x11(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 \verb@\includegraphics{}@ 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 \verb@\includegraphics{}@ 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 \verb@x11(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 \verb@\includegraphics{}@ command.
> > \end{itemize}
> >
> > \end{document}
> > % <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< example-linux.Rnw
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> --
> Brian D. Ripley, ripley@stats.ox.ac.uk
> 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. Received on Sat Aug 26 05:20:19 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 26 Aug 2006 - 22:22:35 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.