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

Thomas Harte thomas.harte at yahoo.com
Sat Aug 26 14:38:58 CEST 2006


--- Friedrich Leisch <Friedrich.Leisch at stat.uni-muenchen.de> wrote:

> >>>>> 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:
>   > <<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")
>   > @
> 
> Umm, maybe I don't get your point, but in what way does the complicated
> code above do anythingdifferent from
> 
> **********************************************************
> 
> <<results=hide>>=
> sp<- make.ar.1(alpha=.5, 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<- make.ar.1(alpha=.5, 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<- make.ar.1(alpha=.5, n=800)
        plot(sp, type="l", col="blue")
        savePlotAsPdf("ar.pdf")

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.

cheers,

thomas.



More information about the R-help mailing list