Re: [R] issues with Sweave and inclusion of graphics in a document
On Fri, 25 Aug 2006 11:05:48 -0700 (PDT), Thomas Harte (TH) wrote: --- 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) @ 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} ** ??? Best, -- --- Prof. Dr. Friedrich Leisch Institut für Statistik Tel: (+49 89) 2180 3165 Ludwig-Maximilians-Universität Fax: (+49 89) 2180 5308 Ludwigstraße 33 D-80539 München http://www.stat.uni-muenchen.de/~leisch __ 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.
Re: [R] issues with Sweave and inclusion of graphics in a document
--- Friedrich Leisch [EMAIL PROTECTED] 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. __ 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.
Re: [R] issues with Sweave and inclusion of graphics in a document
On Sat, 26 Aug 2006 05:38:58 -0700 (PDT), Thomas Harte (TH) wrote: 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). OK, no I think I understand what you want: running the *tangled* code should open windows with the same height/width ratio as in the Latex document, right? Your first posting suggested to me more the other way round ... No, what you want is currently not directly possible with Sweave, you will have to use workarounds like the one you posted. You can even easily write your own tangle function doing it automatically (Rtangle is really trivial can be easily adjusted to ones personal needs). Best, Fritz __ 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.
Re: [R] issues with Sweave and inclusion of graphics in a document
--- Friedrich Leisch [EMAIL PROTECTED] wrote: On Sat, 26 Aug 2006 05:38:58 -0700 (PDT), Thomas Harte (TH) wrote: 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). OK, no I think I understand what you want: running the *tangled* code should open windows with the same height/width ratio as in the Latex document, right? yes, that is correct. the goal is to develop R code in exactly the same way as i always do (a monolithic script with lots of inroads into a problem, solutions i tried and rejected, helpful pointers to the real solution, and so on), but to have a document (.pdf, .ps, whatever) that goes along with the monolithic script noting all the key points. hence, six months after writing the code i want to be able to go back, refresh my memory with the document and then delve into the R code if i have to. maintaining both the R code and the LaTeX documentation in one .Rnw file is the genius of your Sweave package and this is what i want to adopt as my standard development methodology. Your first posting suggested to me more the other way round ... mea maxima culpa. No, what you want is currently not directly possible with Sweave, you will have to use workarounds like the one you posted. You can even easily write your own tangle function doing it automatically (Rtangle is really trivial can be easily adjusted to ones personal needs). thank you for the clarification. i feel a lot less awkward in using that complicated workaround code ;) cheers / gruss, thomas. __ 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.
Re: [R] issues with Sweave and inclusion of graphics in a document
--- 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
Re: [R] issues with Sweave and inclusion of graphics in a document
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). 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:
[R] issues with Sweave and inclusion of graphics in a document
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