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

From: Thomas Harte <>
Date: Sat 26 Aug 2006 - 22:38:58 EST

> >>>>> On Fri, 25 Aug 2006 11:05:48 -0700 (PDT),
> >>>>> Thomas Harte (TH) wrote:

> > i can get pretty close to this in linux by writing a function to save the
> > plot to a pdf device:
> > <<, 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)
> >
> > }
> > # 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<-, n=800)
> > plot(sp, type="l", col="blue")
> > # width & height via dynamic scoping in savePlotAsPdf
> > savePlotAsPdf("ar.pdf")
> > @
> Umm, maybe I don't get your point, but in what way does the complicated
> code above do anythingdifferent from
> **********************************************************
> <<results=hide>>=
> sp<-, n=800)
> @
> <<ar,fig=true,width=20,height=5,include=false>>=
> plot(sp, type="l", col="blue")
> @
> \begin{figure}
>   \includegraphics[width=14.5cm]{myprefix-ar}
> \end{figure}
> **********************************************************

hallo, friedrich, and thanks for your reply.

if i Stangle your code i get:

        sp<-, n=800)
        plot(sp, type="l", col="blue")

whereas if i Stangle my code, i get:

        width<- 20; height<- 5
        x11(width=width, height=height)
        sp<-, n=800)
        plot(sp, type="l", col="blue")

this, i think, is the problem in a nutshell.

i want my R code to look like this because i (often) want to open a separate device (for example, to have specific dimensions as above) and i may wish to save its contents. on the other hand, i may not wish to save a device's contents: i may wish to open several devices for comparison purposes and i may wish to save only certain devices, in keeping with the Sweave options echo=false and results=hide.

if i use your, admittedly uncomplicated, code chunk, then i am limited to seeing the output in the final document. i can't cut and paste the code chunk into an R session and see the results as i wish to see them before saving.

currently for each project i work on, i usually end up with a very lengthy .R file with all the code (and embedded mathematical annotation in LaTeX), and then i have to write a set of notes or a report in LaTeX duplicating or expanding on the mathematics. i'm hoping to supplant this, rather inefficient, method with a single .Rnw file and the use of Sweave. this, i believe, is what Sweave was designed to do. but i do wish to write my R code (warts and all) the way that i want to in the .Rnw file and only include the output (graphics or text) of certain parts of that code in the document. i hope that seems reasonable.

perhaps i was not clear, or succinct enough, in my original post (i tried to get the point across by providing example .Rnw files, but i'm aware that it's asking a lot to wade through a lengthy example to retrieve a point).

thanks again for your reply.


thomas. mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Sat Aug 26 22:42:01 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 Sun 27 Aug 2006 - 02:25:23 EST.

Mailing list information is available at Please read the posting guide before posting to the list.