http://mpastell.com/ Articles about R - mpastell.com 2010-03-24T22:00:00Z Matti Pastell http://mpastell.com tag:mpastell.com,2010-03-25:/2010/03/25/create-odf-pdf-and-html-report-from-a-single-sweave-document/ Create odf, pdf and html report from a single Sweave document 2010-03-24T22:00:00Z 2010-03-24T22:00:00Z <p>A lot of us know about Sweave and Latex and they work very well in creating elegant dynamic reports from R computation. However, sometimes we would like to also produce a word processing document for a colleague or a html version of the same report. Now there are tools for producing these like <a href="http://cran.r-project.org/web/packages/odfWeave/index.html">odfWeave</a>. But wouldn't it be nice to get all of the output formats from the source document with same mark up?</p> <p>Today I'm going to write about a good option how to get a <em>nice looking</em> odf, pdf and html report from the same source: ascii package with <a href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> (reST) markup. reStructuredText is a simple plain text mark up that can be converted to several formats using the Python <a href="http://docutils.sourceforge.net/index.html">Docutils</a>. It is simpler to write than Latex and it also the syntax that is used in <a href="http://sphinx.pocoo.org">Sphinx</a> documents. The <a href="http://eusebe.github.com/ascii/">ascii</a> package has a Sweave driver for weaving R code from documents written with reST and some other mark up languages as well. I have created a simple example document <a href="http://files.mpastell.com/ascii/ascii-example.Rnw">ascii-example.Rnw</a>, which demonstrates the capabilities of the combination. The source file is a Sweave document with reST mark up instead of Latex. I had to make small modifications to the in ReST driver in ascii package to get this example to work<sup><a href="#fn1" class="footnoteRef" id="fnref1">1</a></sup> (download it here: <a href="http://files.mpastell.com/ascii/newRest.R">newRest.R</a>). I then processed the example with the new driver in R:</p> <div class="highlight"><pre><span class="o">&gt;</span> library<span class="p">(</span>ascii<span class="p">)</span> <span class="o">&gt;</span> source<span class="p">(</span><span class="s">&#39;newRest.R&#39;</span><span class="p">)</span> <span class="o">&gt;</span> ReST<span class="p">(</span><span class="s">&#39;ascii-example.Rnw&#39;</span><span class="p">)</span> </pre></div> <p>Weaving produced this <a href="http://files.mpastell.com/ascii/ascii-example.rst">ascii-example.rst</a> reST document which I then converted to odf, html, pdf with pdflatex and pdf with <a href="http://code.google.com/p/rst2pdf/">rst2pdf</a> using:</p> <div class="highlight"><pre>rst2odt ascii-example.rst ascii-example.odt rst2html ascii-example.rst ascii-example.html rst2latex ascii-example.rst ascii-example.tex <span class="o">&amp;&amp;</span> pdflatex ascii-example.tex rst2pdf ascii-example.rst </pre></div> <p><strong>And here are the results:</strong> <a href="http://files.mpastell.com/ascii/ascii-example.odt">odt</a>, <a href="http://files.mpastell.com/ascii/ascii-example.html">html</a>, <a href="http://files.mpastell.com/ascii/ascii-example1.pdf">pdf</a> from latex and <a href="http://files.mpastell.com/ascii/ascii-example.pdf">pdf</a> from rst2pdf. You'll need to have <a href="http://docutils.sourceforge.net/index.html">Docutils</a> and <a href="http://www.rexx.com/~dkuhlman/odtwriter.html">odtwriter</a> for the conversion.</p> <p>I have used the default options, but there are a lot options that can be passed to the docutils writers to customize the output, such as custom stylesheets. The image format in the pdfs is not optimal, but in can be changed with the &quot;res&quot; option in code chunk or you can also choose to use pdf format instead of jpg, but then it won't show up in html documents. I think using reST with ascii package is a good option for producing reports and tutorials in multiple output formats. That is also because I'm already familiar with reST directives and also use Sphinx for other purposes too. The reST syntax is also very fast to learn and I think it is definitely worth exploring. I also recommend using Sphinx if you only want to get html and pdf output, because it has more directives than plain reST e.g. math support.</p> <div class="footnotes"> <hr /> <ol> <li id="fn1"><p>I changed the driver to output code chunks in reST literal environment <code>::</code> instead of <code>.. codeblock:: r</code>, because the codeblock directive is not supported in odt conversion and very poorly supported in latex conversion. <a href="#fnref1" class="footnoteBackLink" title="Jump back to footnote 1">↩</a></p></li> </ol> </div> tag:mpastell.com,2010-02-09:/2010/02/09/python-in-sweave-document/ Python in Sweave document 2010-02-08T22:00:00Z 2010-02-08T22:00:00Z <div id="TOC"><h2>Table of contents</h2> <ul> <li><a href="#modifications-to-the-custom-driver">Modifications to the custom driver:</a></li> <li><a href="#example-usage">Example usage</a></li> </ul> </div> <p>Lately I have been using a lot of Python for signal processing and I quite like SciPy. However, I have been missing something like Sweave, which is great literate programming environment for R. Today I managed to look a bit more into it and found this hack on how to use <a href="http://romainfrancois.blog.free.fr/index.php?post/2009/01/21/Python-and-Sweave">Python code in Sweave document</a> by Romain Francois. However, I wanted to be able to catch the input as well as output so I made a small changes to his Sweave Driver.</p> <h4 id="modifications-to-the-custom-driver"><a href="#TOC">Modifications to the custom driver:</a></h4> <div class="highlight"><pre>driver <span class="o">&lt;-</span> RweaveLatex<span class="p">()</span> runcode <span class="o">&lt;-</span> driver<span class="p">$</span>runcode driver<span class="p">$</span>runcode <span class="o">&lt;-</span> <span class="kr">function</span><span class="p">(</span>object<span class="p">,</span> chunk<span class="p">,</span> options<span class="p">){</span> <span class="kr">if</span><span class="p">(</span> options<span class="p">$</span>engine <span class="o">==</span> <span class="s">&quot;python&quot;</span><span class="p">)</span> <span class="p">{</span> driver<span class="p">$</span>writedoc<span class="p">(</span> object<span class="p">,</span> c<span class="p">(</span><span class="s">&quot;\\\\begin{verbatim}&quot;</span><span class="p">,</span> chunk<span class="p">,</span> <span class="s">&quot;\\\\end{verbatim}&quot;</span><span class="p">,</span> <span class="s">&quot;\\\\begin{python}&quot;</span><span class="p">,</span> chunk<span class="p">,</span><span class="s">&quot;\\\\end{python}&quot;</span><span class="p">)</span> <span class="p">)</span> <span class="p">}</span> <span class="kr">else</span><span class="p">{</span> runcode<span class="p">(</span> object<span class="p">,</span> chunk<span class="p">,</span> options <span class="p">)</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <p>The driver now catches the input in verbatim environment (which can be easily changed to listings) and the output to python environment. The tex document can then be processed with the <a href="http://files.mpastell.com/python.sty">python latex package</a> to evaluate the python expressions. Tho use the driver you need to put the option &quot;engine=&quot;python&quot; in your code chunks.</p> <h4 id="example-usage"><a href="#TOC">Example usage</a></h4> <p>My example is python code that calculates and plots the frequency response of a moving average filter. Here is the code in the Sweave document <a href="http://files.mpastell.com/ma.Rnw">ma.Rnw</a>. Process the file in R using the custom driver above:</p> <div class="highlight"><pre>Sweave<span class="p">(</span> <span class="s">&quot;ma.Rnw&quot;</span><span class="p">,</span> driver <span class="o">=</span> driver<span class="p">)</span> </pre></div> <p>It should produce <a href="http://files.mpastell.com/ma.tex">ma.tex</a>. Then run latex (make sure you have the python package installed):</p> <div class="highlight"><pre>pdflatex -shell-escape ma.tex </pre></div> <p>Which should then in turn output this <a href="http://files.mpastell.com/sweave/ma.pdf">ma.pdf</a> file.</p> tag:mpastell.com,2009-11-25:/2009/11/25/ess-on-mac-os-x/ ESS on Mac OS X 2009-11-24T22:00:00Z 2009-11-24T22:00:00Z <p>One of the search terms that bring people frequently to my site is &quot;install ESS on Mac OS X&quot; or something like that. As it turns out installing <a href="http://ess.r-project.org/">ESS</a> on OS X is really easy, but Google search does not really bring up good instructions. There are at least two easy options:</p> <ol style="list-style-type: decimal"> <li>Use <a href="http://aquamacs.org/">Aquamacs</a>, it comes bundled with ESS</li> <li>Use <a href="http://homepage.mac.com/zenitani/emacs-e.html">Carbon Emacs</a>, it also comes with ESS, but you need to add the line (require 'ess-site) to your .emacs file in your home directory in order to enable it.</li> </ol> <p><strong>That's it!</strong></p> tag:mpastell.com,2009-09-11:/2009/09/11/matlab-style-stem-plot-with-r/ MATLAB style stem plot with R 2009-09-10T21:00:00Z 2009-09-10T21:00:00Z <p>Recently I wanted to plot an impulse response function with R and missed the MATLAB style stem plot for doing it. I couldn't find an R function for it with a quick Google search so I made my own. So here is the function and a small example:</p> <div class="highlight"><pre><span class="c1">#The function</span> stem <span class="o">&lt;-</span> <span class="kr">function</span><span class="p">(</span>x<span class="p">,</span>y<span class="p">,</span>pch<span class="o">=</span><span class="m">16</span><span class="p">,</span>linecol<span class="o">=</span><span class="m">1</span><span class="p">,</span>clinecol<span class="o">=</span><span class="m">1</span><span class="p">,</span><span class="m">...</span><span class="p">){</span> <span class="kr">if</span> <span class="p">(</span>missing<span class="p">(</span>y<span class="p">)){</span> y <span class="o">=</span> x x <span class="o">=</span> <span class="m">1</span>:length<span class="p">(</span>x<span class="p">)</span> <span class="p">}</span> plot<span class="p">(</span>x<span class="p">,</span>y<span class="p">,</span>pch<span class="o">=</span>pch<span class="p">,</span><span class="m">...</span><span class="p">)</span> <span class="kr">for</span> <span class="p">(</span>i in <span class="m">1</span>:length<span class="p">(</span>x<span class="p">)){</span> lines<span class="p">(</span>c<span class="p">(</span>x<span class="p">[</span>i<span class="p">],</span>x<span class="p">[</span>i<span class="p">]),</span> c<span class="p">(</span><span class="m">0</span><span class="p">,</span>y<span class="p">[</span>i<span class="p">]),</span>col<span class="o">=</span>linecol<span class="p">)</span> <span class="p">}</span> lines<span class="p">(</span>c<span class="p">(</span>x<span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="o">-</span><span class="m">2</span><span class="p">,</span>x<span class="p">[</span>length<span class="p">(</span>x<span class="p">)]</span><span class="o">+</span><span class="m">2</span><span class="p">),</span> c<span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">),</span>col<span class="o">=</span>clinecol<span class="p">)</span> <span class="p">}</span> <span class="c1">#An example</span> x <span class="o">&lt;-</span> seq<span class="p">(</span><span class="m">0</span><span class="p">,</span> <span class="m">2</span><span class="o">*</span>pi<span class="p">,</span> by <span class="o">=</span> <span class="m">0.2</span><span class="p">)</span> par<span class="p">(</span>mfrow <span class="o">=</span> c<span class="p">(</span> <span class="m">2</span><span class="p">,</span> <span class="m">1</span><span class="p">))</span> stem<span class="p">(</span>x<span class="p">,</span> sin<span class="p">(</span>x<span class="p">),</span> main <span class="o">=</span> <span class="s">&#39;Default style&#39;</span><span class="p">)</span> stem<span class="p">(</span>x<span class="p">,</span> sin<span class="p">(</span>x<span class="p">)</span> <span class="p">,</span>main<span class="o">=</span><span class="s">&#39;With MATLAB colors&#39;</span><span class="p">,</span> col<span class="o">=</span> <span class="m">4</span><span class="p">,</span> linecol<span class="o">=</span> <span class="m">4</span><span class="p">,</span> clinecol<span class="o">=</span> <span class="m">2</span><span class="p">)</span> </pre></div> <img alt="Stem plot with R" src="http://mpastell.com/images/stem.png" width="600"/> tag:mpastell.com,2009-08-06:/2009/08/06/r-64-bit-on-mac-osx-with-ess/ R 64 bit on Mac OSX with ESS 2009-08-05T21:00:00Z 2009-08-05T21:00:00Z <div id="TOC"><h2>Table of contents</h2> <ul> <li><a href="#compiling">Compiling</a></li> <li><a href="#getting-ess-to-work">Getting ESS to work</a></li> </ul> </div> <p><strong>The current <a href="http://www.r-project.org">R</a> version comes also with a 64-bit version so its not necessary to compile it from source anymore, but you do need to tell ESS how to use the 64bit version</strong></p> <p>So today I compiled 64 bit R 2.91 on Mac OS X 10.5.7 in order to get maximum speed for my analyses. The speed increase I got was 15-25 %, when I ran some tests with matrix calculation and intensive loops. Note that you can also download optimized binaries from <a href="http://r.research.att.com/">http://r.research.att.com/</a>, although they are not the latest version. It took me some effort and googling to get it done so I'm going to post the instructions here, so that I know how to do it next time and I hope that it also benefits others. After I got the installation done I ran into a problem getting <a href="http://ess.r-project.org">ESS</a> to recognize my 64 bit version, but I managed to solve that as well. So here is how I did it:</p> <h3 id="compiling"><a href="#TOC">Compiling</a></h3> <p>First I installed the necessary developer tools from <a href="http://r.research.att.com/tools/">http://r.research.att.com/tools/</a>. I already had gcc 4.2 from apple so installed <a href="http://r.research.att.com/tools/gcc-4.2-5566-darwin8-all.tar.gz">gfortran</a> and libreadline <a href="http://R.research.att.com/readline-5.2-1-quad.tar.gz">readline-5.2-1-quad.tar.gz</a>. Readline is a precompiled binary and you extract is using</p> <div class="highlight"><pre>sudo tar fvxz readline-5.2-1-quad.tar.gz -C / </pre></div> <p>After installing the depencies I extracted R-2.9.1.tar.gz downloaded from cran and executed configure in the extracted directory with the following options (a bit modified from <a href="http://r.research.att.com/building.html">http://r.research.att.com/building.html</a>):</p> <div class="highlight"><pre>./configure <span class="nv">SHELL</span><span class="o">=</span><span class="s1">&#39;/bin/bash&#39;</span> <span class="nv">r_arch</span><span class="o">=</span>x86_64 <span class="se">\</span> <span class="nv">CC</span><span class="o">=</span><span class="s1">&#39;gcc -arch x86_64 -std=gnu99&#39;</span> <span class="nv">CXX</span><span class="o">=</span><span class="s1">&#39;g++ -arch x86_64&#39;</span> <span class="se">\</span> <span class="nv">OBJC</span><span class="o">=</span><span class="s1">&#39;gcc -arch x86_64&#39;</span> <span class="nv">F77</span><span class="o">=</span><span class="s1">&#39;gfortran -arch x86_64&#39;</span> <span class="se">\</span> <span class="nv">FC</span><span class="o">=</span><span class="s1">&#39;gfortran -arch x86_64&#39;</span> --with-system-zlib --with-blas<span class="o">=</span><span class="s1">&#39;-framework vecLib&#39;</span> <span class="se">\</span> --with-lapack --host<span class="o">=</span>x86_64-apple-darwin9.7.0 --build<span class="o">=</span>x86_64-apple-darwin9.7.0 </pre></div> <p>After running &quot;make&quot; and &quot;make install&quot; I had a working installation that can be started using:</p> <div class="highlight"><pre>R --arch x86_64 </pre></div> <p>Just running R tries to run 32 bit version of R which I don't have, so I made an alias R='R--arch=x86_64' to .profile and R starts form terminal with just the familiar R.</p> <h3 id="getting-ess-to-work"><a href="#TOC">Getting ESS to work</a></h3> <p>Ok, all was well so far but then I discovered that ESS can't find R anymore, because it is trying to execute the 32 bit version and doesn't work. So after looking at the ESS source I discovered that you can modify R startup options in the file &quot;essd-r.el&quot;. I use <a href="http://homepage.mac.com/zenitani/emacs-e.html">Carbon Emacs</a>, but this methods should work with other Emacs variants. If you are wondering how to install ESS on OS X have a look at <a href="http://mpastell.com/2009/11/25/ess-on-mac-os-x/">this</a> post. First give yourself a permission to modify the file:</p> <div class="highlight"><pre>chmod u+w essd-r.el </pre></div> <p>Then open it and look for a line that says:</p> <div class="highlight"><pre><span class="nv">inferior-R-args</span> <span class="s">&quot; &quot;</span> <span class="c1">; add space just in case</span> </pre></div> <p>And chance it to:</p> <div class="highlight"><pre><span class="nv">inferior-R-args</span> <span class="s">&quot;--arch x86_64 &quot;</span> <span class="c1">; add space just in case</span> </pre></div> <p>Thats it, you should now have a working ESS set up with a 64 bit R!</p>