[R] including Sweave tangled code in '.Rnw' document
Hello List, I have been using Sweave for my homework this last quarter and have been very impressed at how much time and effort it saves me. I, however, have run up against a problem which I have not been able to solve using any of the Sweave/LaTeX tricks I know. I work through my homework one problem at a time, typesetting equations and writing R code, etc. and occasionally use 'Stangle()' from within R to extract the code if I need to pin down a problem or experiment interactively. I would like to be able to include this resultant code in a verbatim environment in my final TeX'ed up document as an appendix so that the grader can look it over, etc. I could implement this in any number of ways using linux tools, but would like to know if there is a clean way to do so using LaTeX or Sweave so that the latest version of the code is included in my homework each time I run 'R CMD Sweave HW4.Rnw; pdflatex HW4.tex'. e.g I envision something such as: \begin{document} <>= plot(runif(1000)) @ TA here is my code: % some manner of Sweave/LaTeX macro to include the R code tangled out of this '.Rnw' document % in this case it would just be 'plot(runif(1000))', perhaps manually enclosed in a verbatim environment \end{document} I am running R-2.6.2 on Ubuntu Hardy Heron. Thanks for all your help with Sweave. I think it is a fantastic tool. Kyle [[alternative HTML version deleted]] __ R-help@r-project.org 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] including Sweave tangled code in '.Rnw' document
G'day Kyle, On Sat, 29 Nov 2008 11:11:37 -0800 "Kyle Matoba" <[EMAIL PROTECTED]> wrote: > I have been using Sweave for my homework this last quarter [...] but > would like to know if there is a clean way to do so using LaTeX or > Sweave so that the latest version of the code is included in my > homework each time I run 'R CMD Sweave HW4.Rnw; pdflatex HW4.tex'. > > e.g I envision something such as: A file HW4.Rnw such as the following seem to do what you want: ++ start HW4.Rnw ++ \documentclass[a4paper,11pt]{article} \usepackage{listings} \begin{document} <>= plot(runif(1000)) @ <<>>= mean(runif(1000)) @ TA here is my code: % some manner of Sweave/LaTeX macro to include the R code tangled out % of this '.Rnw' document in this case it would just be % 'plot(runif(1000))', perhaps manually enclosed in a verbatim % environment <>= Stangle(file="HW4.Rnw", output="XYZ.R", annotate=FALSE) @ \lstinputlisting[emptylines=0]{XYZ.R} \end{document} ++ end HW4.Rnw If the last Stangle command should be included in the listing, remove the "split=TRUE" option. And the "file=" argument to Stangle would have to be correctly set for each .Rnw file; did not find a way to automate this. > I am running R-2.6.2 on Ubuntu Hardy Heron. R 2.6.2 is quite old by now, the current version is R 2.8.0. When you upgrade, be aware of: o The default for 'stylepath' in Sweave's (default) RweaveLatex driver is now FALSE rather than TRUE if SWEAVE_STYLEPATH_DEFAULT is unset : see ?RweaveLatex. To support this, tools::texi2dvi adds the R 'texmf' directory to the input search path. which could mean that the way you process .Rnw file might no longer work. HTH. Cheers, Berwin === Full address = Berwin A TurlachTel.: +65 6516 4416 (secr) Dept of Statistics and Applied Probability+65 6516 6650 (self) Faculty of Science FAX : +65 6872 3919 National University of Singapore 6 Science Drive 2, Blk S16, Level 7 e-mail: [EMAIL PROTECTED] Singapore 117546http://www.stat.nus.edu.sg/~statba __ R-help@r-project.org 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] including Sweave tangled code in '.Rnw' document
You could achieve this from the LaTeX side of things using the attachfile package: http://www.ctan.org/tex-archive/macros/latex/contrib/attachfile/ I use this package to attach data files and R code to pdf's generated with Sweave + pdflatex, so that the entire analysis can be reproduced from a single document. -Ista > -- Forwarded message -- > From: "Kyle Matoba" <[EMAIL PROTECTED]> > To: r-help@r-project.org > Date: Sat, 29 Nov 2008 11:11:37 -0800 > Subject: [R] including Sweave tangled code in '.Rnw' document > Hello List, > > I have been using Sweave for my homework this last quarter and have been > very impressed at how much time and effort it saves me. I, however, have > run up against a problem which I have not been able to solve using any of > the Sweave/LaTeX tricks I know. I work through my homework one problem at a > time, typesetting equations and writing R code, etc. and occasionally use > 'Stangle()' from within R to extract the code if I need to pin down a > problem or experiment interactively. I would like to be able to include > this resultant code in a verbatim environment in my final TeX'ed up document > as an appendix so that the grader can look it over, etc. I could implement > this in any number of ways using linux tools, but would like to know if > there is a clean way to do so using LaTeX or Sweave so that the latest > version of the code is included in my homework each time I run 'R CMD Sweave > HW4.Rnw; pdflatex HW4.tex'. > > e.g I envision something such as: > > \begin{document} > <>= > plot(runif(1000)) > @ > > TA here is my code: > > % some manner of Sweave/LaTeX macro to include the R code tangled out of > this '.Rnw' document > % in this case it would just be 'plot(runif(1000))', perhaps manually > enclosed in a verbatim environment > > \end{document} > I am running R-2.6.2 on Ubuntu Hardy Heron. > > > Thanks for all your help with Sweave. I think it is a fantastic tool. > > Kyle > __ R-help@r-project.org 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] including Sweave tangled code in '.Rnw' document
Kyle, In addition to "listings" already mentioned by Berwin, you may find useful the "answers" package. This is what I use to create assignments/ exams/exercise sets with solutions all in one file. HTH Vincent --- Vincent Goulet, Associate Professor École d'actuariat Université Laval, Québec [EMAIL PROTECTED] http://vgoulet.act.ulaval.ca Le sam. 29 nov. à 14:11, Kyle Matoba a écrit : Hello List, I have been using Sweave for my homework this last quarter and have been very impressed at how much time and effort it saves me. I, however, have run up against a problem which I have not been able to solve using any of the Sweave/LaTeX tricks I know. I work through my homework one problem at a time, typesetting equations and writing R code, etc. and occasionally use 'Stangle()' from within R to extract the code if I need to pin down a problem or experiment interactively. I would like to be able to include this resultant code in a verbatim environment in my final TeX'ed up document as an appendix so that the grader can look it over, etc. I could implement this in any number of ways using linux tools, but would like to know if there is a clean way to do so using LaTeX or Sweave so that the latest version of the code is included in my homework each time I run 'R CMD Sweave HW4.Rnw; pdflatex HW4.tex'. e.g I envision something such as: \begin{document} <>= plot(runif(1000)) @ TA here is my code: % some manner of Sweave/LaTeX macro to include the R code tangled out of this '.Rnw' document % in this case it would just be 'plot(runif(1000))', perhaps manually enclosed in a verbatim environment \end{document} I am running R-2.6.2 on Ubuntu Hardy Heron. Thanks for all your help with Sweave. I think it is a fantastic tool. Kyle [[alternative HTML version deleted]] __ R-help@r-project.org 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. __ R-help@r-project.org 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] including Sweave tangled code in '.Rnw' document
Vincent, Ista, Berwin: Thank you all for your help. It is great that a student like myself can receive help from Professors and experts such as yourselves. To the list, I found Berwin's solution to work excellently, though I chose to change the font of the included text, e.g. \texttt{ \lstinputlisting[emptylines=0]{XYZ.R} } Ista's suggestion also seems excellent, though I am required to submit a paper copy of the work and Ista's is a bit easier to manage with this goal in mind. Vincent's comment is also quite appropriate, unfortunately I have enough trouble with LaTeX as it is and the problem has been solved to my satisfaction I have chosen to put off exploration of this package for another day. Best, Kyle On Sun, Nov 30, 2008 at 8:42 AM, Vincent Goulet < [EMAIL PROTECTED]> wrote: > Kyle, > > In addition to "listings" already mentioned by Berwin, you may find useful > the "answers" package. This is what I use to create > assignments/exams/exercise sets with solutions all in one file. > > HTH > > Vincent > > --- > Vincent Goulet, Associate Professor > École d'actuariat > Université Laval, Québec > [EMAIL PROTECTED] http://vgoulet.act.ulaval.ca > > Le sam. 29 nov. à 14:11, Kyle Matoba a écrit : > > Hello List, >> >> I have been using Sweave for my homework this last quarter and have been >> very impressed at how much time and effort it saves me. I, however, have >> run up against a problem which I have not been able to solve using any of >> the Sweave/LaTeX tricks I know. I work through my homework one problem at >> a >> time, typesetting equations and writing R code, etc. and occasionally use >> 'Stangle()' from within R to extract the code if I need to pin down a >> problem or experiment interactively. I would like to be able to include >> this resultant code in a verbatim environment in my final TeX'ed up >> document >> as an appendix so that the grader can look it over, etc. I could >> implement >> this in any number of ways using linux tools, but would like to know if >> there is a clean way to do so using LaTeX or Sweave so that the latest >> version of the code is included in my homework each time I run 'R CMD >> Sweave >> HW4.Rnw; pdflatex HW4.tex'. >> >> e.g I envision something such as: >> >> \begin{document} >> <>= >> plot(runif(1000)) >> @ >> >> TA here is my code: >> >> % some manner of Sweave/LaTeX macro to include the R code tangled out of >> this '.Rnw' document >> % in this case it would just be 'plot(runif(1000))', perhaps manually >> enclosed in a verbatim environment >> >> \end{document} >> I am running R-2.6.2 on Ubuntu Hardy Heron. >> >> >> Thanks for all your help with Sweave. I think it is a fantastic tool. >> >> Kyle >> >>[[alternative HTML version deleted]] >> >> __ >> R-help@r-project.org 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. >> > > > > > [[alternative HTML version deleted]] __ R-help@r-project.org 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.