<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Statistics, R, Graphics and Fun</title>
	<atom:link href="http://yihui.name/en/feed/" rel="self" type="application/rss+xml" />
	<link>http://yihui.name/en</link>
	<description>Yihui XIE</description>
	<lastBuildDate>Sun, 29 Jan 2012 04:54:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to Become an Efficient and Collaborative R Programmer</title>
		<link>http://yihui.name/en/2011/12/how-to-become-an-efficient-and-collaborative-r-programmer/</link>
		<comments>http://yihui.name/en/2011/12/how-to-become-an-efficient-and-collaborative-r-programmer/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 22:01:02 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[R Programming]]></category>
		<category><![CDATA[Collaboration]]></category>
		<category><![CDATA[GitHub]]></category>
		<category><![CDATA[knitr]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=787</guid>
		<description><![CDATA[I may want to add a subtitle &#8220;Why R-Forge Must Die&#8221; (thinking of Barry Rowlingson&#8217;s talk earlier this year). I have been a GitHub user for two years, and I was mainly influenced by Hadley. Now I even feel a little bit addicted to GitHub (its slogan is &#8220;social coding&#8221;), because it is really convenient [...]]]></description>
			<content:encoded><![CDATA[<p>I may want to add a subtitle &#8220;Why R-Forge Must Die&#8221; (thinking of Barry Rowlingson&#8217;s talk earlier this year). I have been <a href="https://github.com/yihui">a GitHub user</a> for two years, and I was mainly influenced by <a title="Hadley Wickham" href="https://github.com/hadley">Hadley</a>. Now I even feel a little bit addicted to GitHub (its slogan is &#8220;social coding&#8221;), because it is really convenient for collaboration and makes me more productive.</p>
<p>As some readers might have known, I started a new package <strong><a title="knitr: Elegant, flexible and fast dynamic report generation with R" href="https://github.com/yihui/knitr">knitr</a></strong> last month, and this time I decided to try to use the power of social network like Google+ and Twitter (something I used to stay away from), and so far I&#8217;m pretty satisfied with my attempts. I call GitHub the &#8220;Facebook&#8221; of programmers, and it is also very powerful to connect programmers and users. There are a few features that I think R programmers may want to try (I use <strong>knitr</strong> as an example):</p>
<ol>
<li><a href="https://github.com/yihui/knitr">Browse R code online</a>: I hate checking out a whole package to read its source code, and R-Forge is clumsy (following the steps of sourceforge) in this aspect; the code on GitHub is highlighted and easy to thumb through. Besides, you can browse <a href="https://github.com/yihui/knitr/commit/565361d36c1fc161e66aad8501c3c87742e7c76e">each commit</a> and see what was changed (the difference is highlighted).</li>
<li><a href="https://github.com/yihui/knitr/issues">Issues</a>: instead of writing your own TODO list which is often forgotten, both your users and you can create issues, and you can make discussions there; when you have got a fix, you can write a commit message like &#8220;<code>fixed #46</code>&#8221; in GIT and the <a href="https://github.com/yihui/knitr/issues/46">issue 46</a> will be automatically closed. This is super cool little feature to me. What is more, there will be a reference to the commit which fixed the issue, so you can come back in the future and see how the issue was fixed. Currently <strong>knitr</strong> has got 50 issues in total, with 24 of them from users.</li>
<li>Inline comments: you can discuss code directly along the lines; this is a super super cool feature. <a href="https://github.com/ramnathv">Ramnath</a> started to contribute to the code theme feature recently after he forked my repository, and the original author (i.e. me) can go to the fork and check changes; each change can be commented, e.g. we had quite a few discussions <a href="https://github.com/ramnathv/knitr/commit/33a75f40297dc763c1fd1325f12dc83ad2c5ff61">in this commit</a>. This feels like you can sit together with another programmer, and point to the code with a pen, saying &#8220;I like this and you may need to revise that, &#8230;&#8221;. In comparison, the traditional way of collaboration is usually through email &#8212; email patches back and forth, which is way less straightforward. When Ramnath and I feel the work is mature, he can simply send me a pull request, and all the changes can be merged back to my repository. The other example is I saw the <a href="http://rappster.wordpress.com/2011/12/09/running-your-r-and-latex-infrastructure-from-a-portable-usb-drive/">blog post</a> by Songpants the other day, and I suggested he move the work to GitHub so I can make suggestions closer to the R code, and now the code is happily sitting on GitHub (so are <a href="https://github.com/Songpants/RappsterMisc/commit/041267a9bac2be4e65dd36d17c50dece689d5334">my comments</a>).</li>
<li>Wiki: it makes it so easy to quickly set up a documentation page; I have not done it for <strong>knitr</strong> yet, but I did it for <a href="https://github.com/yihui/formatR/wiki">the <strong>formatR</strong> package</a>. It looks better than R&#8217;s documentation, right? Again, other people can collaborate with you in editing the wiki pages. The other way to make your documentation better is to write vignettes in Sweave, which usually takes a lot of efforts (wrestling with LaTeX) unless you use LyX+<strong>knitr</strong> like me; I feel vignettes are easy to make, but this is another story.</li>
<li>Stay tuned with a package: you can watch a repository (use the button in the top-right) so that you can read the updates of a package in the dashboard; alternatively, you can follow a GitHub user like you follow people on Twitter.</li>
<li><a href="http://yihui.github.com/knitr">GitHub pages</a>: this is probably the coolest feature; you can use another branch (called <code>gh-pages</code>) in your GIT repository to build your website based on Jekyll. I made the website for <strong>knitr</strong> in this way, because I really want to make <strong>knitr</strong> a beautiful package, so everything has to be beautiful, then R documentation was ruled out. Of course, Hadley is a pioneer in documenting R packages with websites. In the future, I may want to develop a package based on <strong>knitr</strong> which turns R documentation into a website automatically (with examples parsed and evaluated, plots inserted), so you can host it on GitHub or somewhere else. This is only an idea at the moment, and feel free to contact me if you are interested.</li>
</ol>
<p>I cannot say I&#8217;m already an efficient R programmer, but GitHub did make me much more efficient.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2011/12/knitr-elegant-flexible-and-fast-dynamic-report-generation-with-r/" title="knitr: Elegant, flexible and fast dynamic report generation with R">knitr: Elegant, flexible and fast dynamic report generation with R</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2011/12/how-to-become-an-efficient-and-collaborative-r-programmer/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>knitr: Elegant, flexible and fast dynamic report generation with R</title>
		<link>http://yihui.name/en/2011/12/knitr-elegant-flexible-and-fast-dynamic-report-generation-with-r/</link>
		<comments>http://yihui.name/en/2011/12/knitr-elegant-flexible-and-fast-dynamic-report-generation-with-r/#comments</comments>
		<pubDate>Thu, 01 Dec 2011 06:56:48 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[R Computation]]></category>
		<category><![CDATA[R Graphics]]></category>
		<category><![CDATA[knitr]]></category>
		<category><![CDATA[Sweave]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=783</guid>
		<description><![CDATA[The world has changed. You can feel it on GitHub. You can smell it on Google+. The knitr package, as an alternative tool to Sweave, has features that you have been longing for, and features that you might have never imagined. Thumb through the PDF manual to see some of them. Currently this package is [...]]]></description>
			<content:encoded><![CDATA[<p>The world has changed. You can feel it on <a href="https://github.com/yihui/knitr" target="_blank">GitHub</a>. You can smell it on <a href="https://plus.google.com/u/0/109653178371807724268/posts" target="_blank">Google+</a>. The <strong>knitr</strong> package, as an alternative tool to Sweave, has features that you have been longing for, and features that you might have never imagined. Thumb through <a href="https://github.com/downloads/yihui/knitr/knitr-manual.pdf" target="_blank">the PDF manual</a> to see some of them.</p>
<p>Currently this package is still a beta version, so I&#8217;m looking for feedback from early birds on:</p>
<ol>
<li>is the PDF documentation confusing in any places? e.g. you have no idea on how to install the package because it was not mentioned in the manual;</li>
<li>does <a href="http://yihui.github.com/knitr" target="_blank">the website</a> look ugly in your browser? (I know it does with IE under Windows) I used a font from Google Font API, and it does not seem to be consistent across different web browsers/OS&#8217;es;</li>
<li>what kind of difficulties did you have in switching from Sweave/pgfSweave/whatever-Sweave to <strong>knitr</strong>?</li>
<li>do you like the idea of putting R code/output in a shaded frame in LaTeX? is the default shading (<code>rgb(.97, .97, .97)</code>) too dark or too light? how about the highlighting theme?</li>
<li>have you ever tried to hack at Sweave? I&#8217;d love to listen to your stories;</li>
<li>what else do you expect from <strong>knitr</strong>?</li>
</ol>
<p>Feel free to file a bug report in <a href="https://github.com/yihui/knitr/issues" target="_blank">the Issues page</a> if you find any problems or have any suggestions. I appreciate your efforts in making this <strong>knitr</strong> package even neater!</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2011/12/how-to-become-an-efficient-and-collaborative-r-programmer/" title="How to Become an Efficient and Collaborative R Programmer">How to Become an Efficient and Collaborative R Programmer</a></li><li><a href="http://yihui.name/en/2011/05/sweave-and-pgfsweave-in-lyx-2-0-x-experimental/" title="Sweave and pgfSweave in LyX 2.0.x (experimental)">Sweave and pgfSweave in LyX 2.0.x (experimental)</a></li><li><a href="http://yihui.name/en/2010/12/a-special-graphics-device-in-r-the-null-device/" title="A Special Graphics Device in R: the Null Device">A Special Graphics Device in R: the Null Device</a></li><li><a href="http://yihui.name/en/2010/10/how-to-start-using-pgfsweave-in-lyx-in-one-minute/" title="How to Start Using (pgf)Sweave in LyX in One Minute">How to Start Using (pgf)Sweave in LyX in One Minute</a></li><li><a href="http://yihui.name/en/2010/03/font-families-for-the-r-pdf-device/" title="Font Families for the R PDF Device">Font Families for the R PDF Device</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2011/12/knitr-elegant-flexible-and-fast-dynamic-report-generation-with-r/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>The fun Package: Use R for Fun!</title>
		<link>http://yihui.name/en/2011/08/the-fun-package-use-r-for-fun/</link>
		<comments>http://yihui.name/en/2011/08/the-fun-package-use-r-for-fun/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 03:41:12 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[R Programming]]></category>
		<category><![CDATA[Five in a row]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[Mine sweeper]]></category>
		<category><![CDATA[R Package]]></category>
		<category><![CDATA[sudoku]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=757</guid>
		<description><![CDATA[A couple of days ago we released a package named fun to CRAN, but I did not dare to send an announcement to r-packages@r-project.org as usual. This package is a collection of some classical computer games (e.g. the Mine sweeper and Five in a row) as well as other funny stuff. Some examples: ## install.packages('fun') [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago we released a package named <a href="http://cran.r-project.org/package=fun" target="_blank"><code>fun</code></a> to CRAN, but I did not dare to send an announcement to r-packages@r-project.org as usual. This package is a collection of some classical computer games (e.g. the Mine sweeper and Five in a row) as well as other funny stuff. Some examples:</p>
<pre class="brush: r">## install.packages('fun')
library(fun)
if (.Platform$OS.type == "windows")
         x11() else x11(type = "Xlib")
mine_sweeper()</pre>
<div class="wp-caption aligncenter" style="width: 490px"><img title="Mine Sweeper in R" src="http://i.imgur.com/U4oAT.png" alt="Mine Sweeper in R" width="480" height="480" /><p class="wp-caption-text">Mine Sweeper in R</p></div>
<pre class="brush: r">library(fun)
gomoku()</pre>
<div class="wp-caption aligncenter" style="width: 490px"><img title="Five in a row in R" src="http://i.imgur.com/8jcQl.png" alt="Five in a row in R" width="480" height="480" /><p class="wp-caption-text">Five in a row in R</p></div>
<p>You can take a look at the list of functions in this package by reading the HTML help page (go to <code>help.start()</code>), and I also need to mention the demos, e.g. see <code>demo('TurtleGraphics')</code> for a demo of Turtle graphics (how many people know the old Logo programming language?), and <code>demo(package = 'fun')</code> for a list of all demos in this package.</p>
<pre class="brush: r">demo('RealTurtle', package = 'fun')</pre>
<div class="wp-caption aligncenter" style="width: 375px"><img title="A turtle drawn in R" src="http://i.imgur.com/oWdOu.png" alt="A turtle drawn in R" width="365" height="130" /><p class="wp-caption-text">A turtle drawn in R</p></div>
<p>Although these topics are not new, they can still be good programming exercises.</p>
<p>We started writing this package more than two years ago, but it was almost forgotten later until a few days ago someone mentioned the game &#8220;Five in a row&#8221; in <a href="http://cos.name/cn/" target="_blank">our web forum</a>. This forum is almost the Chinese version of R-help, and it is not unusual for people to bring forward all kinds of funny ideas with R. If you are at useR! 2011 right now, you probably have heard from George Zhang <a href="http://www.warwick.ac.uk/statsdept/useR-2011/schedule/LIGHTNING.HTML" target="_blank">about the Chinese R conferences</a> these years, and this forum has been the sponsor and organizer ever since the first conference (which I initiated). However, please do not get a wrong impression that Chinese useRs are doing mine sweepers with R every day.</p>
<p>Feel free to share with us if you have more fun. The developers&#8217; page is at: <a href="https://github.com/yihui/fun" target="_blank">https://github.com/yihui/fun</a></p>
<p>P. S. This package may remind some people about the <a href="http://cran.r-project.org/package=sudoku" target="_blank">sudoku</a> package (e.g. <a href="https://plus.google.com/109653178371807724268/posts/eGKq27aqK5b" target="_blank">Joshua Wiley has noticed it</a>), and some people may even remember this:</p>
<pre class="brush: r">library(fortunes)
fortune('sudoku')</pre>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2011/01/formatr-update-0-1-6/" title="formatR update (0.1-6)">formatR update (0.1-6)</a></li><li><a href="http://yihui.name/en/2010/12/formatr-update-0-1-5/" title="formatR update (0.1-5)">formatR update (0.1-5)</a></li><li><a href="http://yihui.name/en/2010/12/rd2roxygen-convert-rd-to-roxygen-documentation/" title="Rd2roxygen: Convert Rd to roxygen documentation">Rd2roxygen: Convert Rd to roxygen documentation</a></li><li><a href="http://yihui.name/en/2009/12/happy-new-year-with-r/" title="Happy New Year with R">Happy New Year with R</a></li><li><a href="http://yihui.name/en/2008/06/flash-animations-by-saveswf-in-package-animation/" title="Flash Animations by saveSWF() in Package &#8220;animation&#8221;">Flash Animations by saveSWF() in Package &#8220;animation&#8221;</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2011/08/the-fun-package-use-r-for-fun/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Sweave and pgfSweave in LyX 2.0.x (experimental)</title>
		<link>http://yihui.name/en/2011/05/sweave-and-pgfsweave-in-lyx-2-0-x-experimental/</link>
		<comments>http://yihui.name/en/2011/05/sweave-and-pgfsweave-in-lyx-2-0-x-experimental/#comments</comments>
		<pubDate>Thu, 26 May 2011 02:33:31 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[R Programming]]></category>
		<category><![CDATA[Literate Programming]]></category>
		<category><![CDATA[LyX]]></category>
		<category><![CDATA[pgfSweave]]></category>
		<category><![CDATA[R Language]]></category>
		<category><![CDATA[Sweave]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=726</guid>
		<description><![CDATA[bout half a year ago, I wrote a post on the configuration of (pgf)Sweave and LyX, which was intended to save us some efforts in going through all the details during the configuration. Now many things have changed: LyX 2.0 has internal support for Sweave, and fortunately I have been in touch with the developers [...]]]></description>
			<content:encoded><![CDATA[<span class="alert">Please ignore this post completely, because Sweave support has become mature in LyX since 2.0.2, and I&#8217;m not going to add the pgfSweave module in LyX. For pgfSweave users, you may consider the new knitr module (available since 2.0.3) which uses the R package <a href="http://cran.r-project.org/package=knitr">knitr</a>.</span>
<p><a href="http://yihui.name/en/2011/05/sweave-and-pgfsweave-in-lyx-2-0-x-experimental/"><span class="dropcap-brown">A</span></a>bout half a year ago, I wrote <a title="How to Start Using (pgf)Sweave in LyX in One Minute" href="http://yihui.name/en/2010/10/how-to-start-using-pgfsweave-in-lyx-in-one-minute/" target="_blank">a post on the configuration of (pgf)Sweave and LyX</a>, which was intended to save us some efforts in going through all the details during the configuration. Now many things have changed: LyX 2.0 has internal support for Sweave, and fortunately I have been in touch with the developers on this feature (thanks to <a href="http://ggorjan.blogspot.com/" target="_blank">Gregor</a>); meanwhile, there have also been many changes in the <a href="http://cran.r-project.org/package=pgfSweave" target="_blank">pgfSweave</a> package. In all, we have a number of new features which we should definitely make use of.</p>
<h2>New Features</h2>
<p>A list of new features as far as I can remember:</p>
<ol>
<li>support for Sweave in LyX 2.0 is internal, so there is no need to modify the preferences file manually (the converters have been defined internally)</li>
<li>most importantly, Sweave becomes an independent module now in LyX, which means you can use it with arbitrary layouts!</li>
<li>we can see the messages during compilation in LyX 2.0 (View&#8211;&gt;View Messages), which is really really helpful and I would strongly recommend you to turn on this option when compiling Sweave documents, because you will know which code chunk goes wrong in case of any errors (in the past, you only got an annoying error dialog box which told you almost nothing about the error)</li>
<li>pgfSweave is faster: it uses the GNU make utility to compile graphics, and you can use multi cores if you like; the compilation becomes 3 steps (pdflatex, make, then pdflatex); other nice features include: the R code is put in an environment Hinput now so you can customize it in LaTeX preamble; there will be no longer a huge gap between the R code and the output (<a href="https://github.com/cameronbracken/pgfSweave/pull/26" target="_blank">fixed by Liang Qi</a>)&#8230;</li>
<li>tikzDevice has better support for multi-byte characters (using UTF8)</li>
</ol>
<p>I have been working on improving the Sweave module and adding a new pgfSweave module to LyX, and now I have basically finished what I planned to do. See <a href="http://www.lyx.org/trac/ticket/7555" target="_blank">the ticket #7555 for details</a>. To sum up,</p>
<ol>
<li>LaTeX will not complain about not being able to find Sweave.sty; I used several tricks to guarantee this &#8212; even in the worst case, LaTeX can still use the hard-coded Sweave style;</li>
<li>Spaces and dots in path names or filenames will no longer be a problem;</li>
<li>the pgfSweave module is also working now;</li>
<li>you can export the reformatted R code in a LyX document with the pgfSweave module;</li>
</ol>
<p>I have also tried to document all the cool bells and whistles in two examples, <a href="http://www.lyx.org/trac/raw-attachment/ticket/7555/sweave.lyx">sweave.lyx</a> and <a href="http://www.lyx.org/trac/raw-attachment/ticket/7555/pgfsweave.lyx">pgfsweave.lyx</a>. Or you can directly read the PDF documents, <a href="http://www.lyx.org/trac/raw-attachment/ticket/7555/sweave.pdf.tar.xz">sweave.pdf.tar.xz</a> and <a href="http://www.lyx.org/trac/raw-attachment/ticket/7555/pgfsweave.pdf">pgfsweave.pdf</a>.</p>
<h2>Try the (pgf)Sweave Module</h2>
<p>It seems several people are interested in testing the two modules, and it is actually very easy under Linux. So far I have had no luck with Windows to build LyX from source (I tried once; it took me days to compile and ended up with errors).</p>
<ol>
<li>check out the source code of LyX: <code>svn co svn://svn.lyx.org/lyx/lyx-devel/trunk lyx-devel</code></li>
<li>(<code>cd lyx-devel</code>) apply my patch <a href="http://www.lyx.org/trac/raw-attachment/ticket/7555/sweave-patch.diff">sweave-patch.diff</a> to the svn source you checked out just now: <code>patch -p0 -i /path/to/sweave-patch.diff</code></li>
<li>build LyX
<pre class="brush: plain">./autogen.sh
./configure
make
sudo make install</pre>
</li>
</ol>
<p>Done.</p>
<p>Currently the patch is still waiting on LyX Trac. If you run into any problems before the developers begin to look at the patch, please let me know and we will try to make the two modules more stable and useful. But first of all, please remember to keep all your software packages up-to-date: R 2.13.0, pgfSweave 1.2.1 (run <code>update.packages()</code> in R as frequently as you can) and LaTeX package pgf 2.10 (very important for externalization). <span id="more-726"></span></p>
<h2>For LyX 1.6.x Users</h2>
<p>If you have been using Sweave since LyX 1.6.x and followed the approach in my <a title="How to Start Using (pgf)Sweave in LyX in One Minute" href="http://yihui.name/en/2010/10/how-to-start-using-pgfsweave-in-lyx-in-one-minute/" target="_blank">previous blog post</a>, and you happen to be a Linux user as well, I apologize for the inconvenience: you probably need to remove all the old configurations. One way is to rename <code>~/.lyx/</code>:</p>
<pre class="brush: plain">mv ~/.lyx ~/.lyx16</pre>
<p>Then reconfigure LyX. If you have important configurations in the <code>preferences</code> file and the <code>layouts</code> directory under <code>~/.lyx/</code>, you need to remove all the Sweave-related configurations (including the converters and format definitions), and also delete all the <code>literate-*.layout</code> files under the <code>layouts</code> directory. Then reconfigure LyX.</p>
<h2>For Windows Users</h2>
<p>Compiling LyX from source under Windows was pain for me, so the above approach might not work well for Windows users. Anyway, manual configuration is not terribly hard, as long as you understand how a LyX module works.</p>
<p>First we need to define a new format for pgfSweave (Tools &#8211;&gt; Preferences; do exactly as the below screenshot shows, and note the upper or lower cases):</p>
<div id="attachment_751" class="wp-caption aligncenter" style="width: 597px"><img class="size-full wp-image-751" title="Define the pgfSweave format" src="http://yihui.name/en/wp-content/uploads/2011/05/lyx-pgfsweave-format.png" alt="Define the pgfSweave format" width="587" height="354" /><p class="wp-caption-text">Define the pgfSweave format</p></div>
<p>Then we tell LyX how it should deal with such a format by defining a converter (e.g. select pgfSweave from the left drop-down list and LaTeX (plain) from the right list, then click the Add button; depending on your output type, you may need several similar converters):</p>
<div id="attachment_752" class="wp-caption aligncenter" style="width: 616px"><img class="size-full wp-image-752" title="pgfSweave converters" src="http://yihui.name/en/wp-content/uploads/2011/05/lyx-pgfsweave-converter.png" alt="pgfSweave converters" width="606" height="350" /><p class="wp-caption-text">pgfSweave converters</p></div>
<p>Next we define a module to use the above format and converter, and an R script to do the real job of Sweaving. Download <a href="http://gitorious.org/yihui/lyx2-sweave/blobs/raw/master/pgfsweave.module">pgfsweave.module</a> and <a href="http://gitorious.org/yihui/lyx2-sweave/blobs/raw/master/lyx-pgfsweave.R">lyx-pgfsweave.R</a>. Go to your installation directory of LyX, put <code>pgfsweave.module</code> under <code>Resources/layouts</code> and <code>lyx-pgfsweave.R</code> under <code>Resources/scripts</code>. Reconfigure LyX (Tools &#8211;&gt; Reconfigure) and you are done.</p>
<p>You can test with my example <a href="http://www.lyx.org/trac/raw-attachment/ticket/7555/pgfsweave.lyx">pgfsweave.lyx</a>.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2010/10/how-to-start-using-pgfsweave-in-lyx-in-one-minute/" title="How to Start Using (pgf)Sweave in LyX in One Minute">How to Start Using (pgf)Sweave in LyX in One Minute</a></li><li><a href="http://yihui.name/en/2010/02/press-enter-in-lyx-sweave-as-you-wish/" title="Press Enter in LyX Sweave as You Wish">Press Enter in LyX Sweave as You Wish</a></li><li><a href="http://yihui.name/en/2010/12/a-special-graphics-device-in-r-the-null-device/" title="A Special Graphics Device in R: the Null Device">A Special Graphics Device in R: the Null Device</a></li><li><a href="http://yihui.name/en/2010/03/font-families-for-the-r-pdf-device/" title="Font Families for the R PDF Device">Font Families for the R PDF Device</a></li><li><a href="http://yihui.name/en/2011/12/knitr-elegant-flexible-and-fast-dynamic-report-generation-with-r/" title="knitr: Elegant, flexible and fast dynamic report generation with R">knitr: Elegant, flexible and fast dynamic report generation with R</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2011/05/sweave-and-pgfsweave-in-lyx-2-0-x-experimental/feed/</wfw:commentRss>
		<slash:comments>68</slash:comments>
		</item>
		<item>
		<title>Produce Authentic Math Formulas in R Graphics</title>
		<link>http://yihui.name/en/2011/04/produce-authentic-math-formulas-in-r-graphics/</link>
		<comments>http://yihui.name/en/2011/04/produce-authentic-math-formulas-in-r-graphics/#comments</comments>
		<pubDate>Sun, 01 May 2011 03:29:01 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[R Graphics]]></category>
		<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[math expressions]]></category>
		<category><![CDATA[R Language]]></category>
		<category><![CDATA[tikzDevice]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=719</guid>
		<description><![CDATA[I remember a few weeks ago, there was a challenge in the R-help list to make the prime symbol in R graphics. In LaTeX, we simply write $X'$ or $X^\prime$. R has a rough support for math expressions (see demo(plotmath)) and they are certainly unsatisfactory for LaTeX users. In fact we can write native LaTeX [...]]]></description>
			<content:encoded><![CDATA[<p>I remember a few weeks ago, there was a challenge in the R-help list to make the prime symbol in R graphics. In LaTeX, we simply write <code>$X'$</code> or <code>$X^\prime$</code>. R has a rough support for math expressions (see <code>demo(plotmath)</code>) and they are certainly unsatisfactory for LaTeX users. In fact we can write native LaTeX code in R plots via the <a href="http://cran.r-project.org/package=tikzDevice" target="_blank">tikzDevice</a> package! Why bother to use all kinds of tricks to cheat R? <img src='http://yihui.name/en/wp-content/plugins/tango-smilies/tango/face-smile.png' alt=':)' class='wp-smiley' /> </p>
<p>Here is an example per request of a reader of my blog:</p>
<p><img class="aligncenter size-full wp-image-720" title="authentic math formula in R" src="http://yihui.name/en/wp-content/uploads/2011/04/formula.png" alt="authentic math formula in R" width="400" height="400" /><span id="more-719"></span></p>
<pre class="brush: r">library(tikzDevice)
options(tikzMetricPackages = c("\\usepackage[utf8]{inputenc}",
    "\\usepackage[T1]{fontenc}", "\\usetikzlibrary{calc}",
    "\\usepackage{amssymb}"))
## I need the amssymb package because I use \mathcal and \mathbb
tikz("formula.tex", width = 4, height = 4, standAlone = TRUE,
    packages = c("\\usepackage{tikz}",
                 "\\usepackage[active,tightpage,psfixbb]{preview}",
                 "\\PreviewEnvironment{pgfpicture}",
                 "\\setlength\\PreviewBorder{0pt}",
                 "\\usepackage{amssymb}"))
par(mar = c(4, 4, 0.1, 0.1), mgp = c(2, 0.9, 0))
plot(1, type = "n", xlab = "$x_1$", ylab = "$x_2$")
text(1, c(0.8, 1, 1.2), c("$\\underbrace{1,2,\\cdots,10}_{10}$",
    "$\\mathbb{ABCDEFG}$", "$\\mathcal{HIJKLMN}$"), cex = 2.5)
dev.off()

tools::texi2dvi("formula.tex", pdf = TRUE)
system(paste(getOption("pdfviewer"), "formula.pdf"))</pre>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2010/02/press-enter-in-lyx-sweave-as-you-wish/" title="Press Enter in LyX Sweave as You Wish">Press Enter in LyX Sweave as You Wish</a></li><li><a href="http://yihui.name/en/2009/11/create-animations-in-pdf-documents-using-r/" title="Create Animations in PDF Documents Using R">Create Animations in PDF Documents Using R</a></li><li><a href="http://yihui.name/en/2011/05/sweave-and-pgfsweave-in-lyx-2-0-x-experimental/" title="Sweave and pgfSweave in LyX 2.0.x (experimental)">Sweave and pgfSweave in LyX 2.0.x (experimental)</a></li><li><a href="http://yihui.name/en/2010/10/how-to-start-using-pgfsweave-in-lyx-in-one-minute/" title="How to Start Using (pgf)Sweave in LyX in One Minute">How to Start Using (pgf)Sweave in LyX in One Minute</a></li><li><a href="http://yihui.name/en/2010/10/on-the-gory-loops-in-r/" title="On the Gory Loops in R">On the Gory Loops in R</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2011/04/produce-authentic-math-formulas-in-r-graphics/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>New versions of GGobi and rggobi for Windows users</title>
		<link>http://yihui.name/en/2011/04/new-versions-of-ggobi-and-rggobi-for-windows-users/</link>
		<comments>http://yihui.name/en/2011/04/new-versions-of-ggobi-and-rggobi-for-windows-users/#comments</comments>
		<pubDate>Sat, 09 Apr 2011 05:38:03 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[R Graphics]]></category>
		<category><![CDATA[GGobi]]></category>
		<category><![CDATA[GTK+]]></category>
		<category><![CDATA[rggobi]]></category>
		<category><![CDATA[RGtk2]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=716</guid>
		<description><![CDATA[or those who have been struggling with the installation of GGobi and the rggobi package under Windows: a major update of GGobi 2.1.9 is that GTK+ has been bundled with GGobi, so the installation of GTK+ is no longer required (I recommend you to uninstall it if it is not used elsewhere in your system); [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://yihui.name/en/2011/04/new-versions-of-ggobi-and-rggobi-for-windows-users/"><span class="dropcap-green">F</span></a>or those who have been struggling with the installation of <a href="http://www.ggobi.org" target="_blank">GGobi</a> and the <a href="http://cran.r-project.org/package=rggobi" target="_blank">rggobi package</a> under Windows: a major update of <a href="http://www.ggobi.org/downloads/ggobi-2.1.9.exe" target="_blank">GGobi 2.1.9</a> is that GTK+ has been bundled with GGobi, so the installation of GTK+ is no longer required (I recommend you to uninstall it if it is not used elsewhere in your system); besides, the <code>rggobi</code> package, which interfaces R to GGobi, is now built with the GGobi 2.1.9 on CRAN too. You might know that the Windows binary of rggobi is not available on CRAN in the past (and Prof Ripley kindly provided the binary), but now things have changed. Hopefully this can make our life with GGobi easier.</p>
<p>You may use <code>install.packages('rggobi')</code> to install the new version of <code>rggobi</code> from CRAN.</p>
<p>Also note that if you are a user of the <a href="http://cran.r-project.org/package=RGtk2" target="_blank">RGtk2 package</a>, you don&#8217;t need a standalone installation of GTK+ either if you have already installed GGobi 2.1.9, because the path of GGobi will be written in the PATH variable of your system and <code>RGtk2</code> can load the required dll&#8217;s from GGobi&#8217;s directory.</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2009/03/identify-data-points-in-off-screen-r-graphics-devices/" title="Identify Data Points in Off-Screen R Graphics Devices">Identify Data Points in Off-Screen R Graphics Devices</a></li><li><a href="http://yihui.name/en/2007/10/spring-in-fall/" title="Spring in Fall">Spring in Fall</a></li><li><a href="http://yihui.name/en/2007/10/demonstration-of-confidence-intervals-using-r/" title="Demonstration of Confidence Intervals Using R (Animated)">Demonstration of Confidence Intervals Using R (Animated)</a></li><li><a href="http://yihui.name/en/2010/09/syntaxhighlighter-brush-for-the-r-language/" title="SyntaxHighlighter Brush for the R Language">SyntaxHighlighter Brush for the R Language</a></li><li><a href="http://yihui.name/en/2008/10/quincunx-in-r/" title="Quincunx (Galton box, Bean machine) in R">Quincunx (Galton box, Bean machine) in R</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2011/04/new-versions-of-ggobi-and-rggobi-for-windows-users/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>formatR update (0.1-6)</title>
		<link>http://yihui.name/en/2011/01/formatr-update-0-1-6/</link>
		<comments>http://yihui.name/en/2011/01/formatr-update-0-1-6/#comments</comments>
		<pubDate>Thu, 06 Jan 2011 21:52:26 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[R Programming]]></category>
		<category><![CDATA[Comments]]></category>
		<category><![CDATA[CRAN]]></category>
		<category><![CDATA[formatR]]></category>
		<category><![CDATA[R Package]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=695</guid>
		<description><![CDATA[A new version of the formatR package is available on CRAN now (binary packages are still on the way). There are three major updates: the inline comments will also be preserved in most cases (in earlier versions, only single lines of comments are preserved) tidy.source() gained a new argument 'text' to accept a character vector [...]]]></description>
			<content:encoded><![CDATA[<p>A new version of the <code>formatR</code> package is available <a title="download formatR on CRAN" href="http://cran.r-project.org/package=formatR">on CRAN</a> now (binary packages are still on the way). There are three major updates:</p>
<ol>
<li>the inline comments will also be preserved in most cases (in earlier versions, only single lines of comments are preserved)</li>
<li><code>tidy.source()</code> gained a new argument <code>'text'</code> to accept a character vector as the source code</li>
<li>multi-byte characters are supported in the <code>formatR()</code> GUI now (sorry, this is not completely true in 0.1-6; it has been fixed in 0.1-7)</li>
</ol>
<p>The first feature is a request from Cameron, which is actually from another request  of another user. I also feel this is a necessary feature even from the first version of this package, but dealing with inline comments is not as easy as the single lines of comments, and it can be dangerous. Please read the help page of the function <code>tidy.source()</code> for all the dark and dirty tricks for preserving R comments when formatting the R code. Here is a quick example:</p>
<pre class="brush: r">&gt; library(formatR)
&gt; src = c("# a single line of comments is preserved",
+     "1+1", "if(TRUE){",
+     paste("x=1  ", "# comments begin with at least 2 spaces!"),
+     "}else{", "x=2;print('Oh no... ask the right bracket to go away!')}",
+     "1*3 # this comment will be dropped!")
&gt;
&gt; ## source code
&gt; cat(src, sep = "\n")
# a single line of comments is preserved
1+1
if(TRUE){
x=1   # comments begin with at least 2 spaces!
}else{
x=2;print('Oh no... ask the right bracket to go away!')}
1*3 # this comment will be dropped!
</pre>
<p>We can reformat the code as:</p>
<pre class="brush: r">&gt; ## the formatted version
&gt; tidy.source(text = src)
# a single line of comments is preserved
1 + 1
if (TRUE) {
    x = 1   # comments begin with at least 2 spaces!
} else {
    x = 2
    print("Oh no... ask the right bracket to go away!")
}
1 * 3
</pre>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2010/12/formatr-update-0-1-5/" title="formatR update (0.1-5)">formatR update (0.1-5)</a></li><li><a href="http://yihui.name/en/2007/11/package-animation-statistical-animations-in-r/" title="Package &#8220;animation&#8221;: Statistical Animations in R">Package &#8220;animation&#8221;: Statistical Animations in R</a></li><li><a href="http://yihui.name/en/2011/08/the-fun-package-use-r-for-fun/" title="The fun Package: Use R for Fun!">The fun Package: Use R for Fun!</a></li><li><a href="http://yihui.name/en/2010/12/rd2roxygen-convert-rd-to-roxygen-documentation/" title="Rd2roxygen: Convert Rd to roxygen documentation">Rd2roxygen: Convert Rd to roxygen documentation</a></li><li><a href="http://yihui.name/en/2010/04/formatr-farewell-to-ugly-r-code/" title="formatR: farewell to ugly R code">formatR: farewell to ugly R code</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2011/01/formatr-update-0-1-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Customizing the Theme of Your R HTML Help</title>
		<link>http://yihui.name/en/2011/01/customizing-the-theme-of-your-r-html-help/</link>
		<comments>http://yihui.name/en/2011/01/customizing-the-theme-of-your-r-html-help/#comments</comments>
		<pubDate>Tue, 04 Jan 2011 20:36:44 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[R Language]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[R HTML Help]]></category>
		<category><![CDATA[R.css]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=688</guid>
		<description><![CDATA[R&#8217;s default theme of the HTML help pages is too plain for me to read, but we can easily modify the theme, which is essentially a CSS file. You can find the file under: file.path(R.home('doc'), 'html', 'R.css') Simply replace this file with my version: which looks like: Of course you can design your own R.css [...]]]></description>
			<content:encoded><![CDATA[<p>R&#8217;s default theme of the HTML help pages is too plain for me to read, but we can easily modify the theme, which is essentially a CSS file. You can find the file under:</p>
<pre class="brush: r">file.path(R.home('doc'), 'html', 'R.css')
</pre>
<p>Simply replace this file with my version:</p>
<span class="download">Download <a href="https://github.com/yihui/configuration/raw/master/R.css">R.css</a> (1K)</span>
<p>which looks like:</p>
<div id="attachment_689" class="wp-caption aligncenter" style="width: 597px"><a href="http://yihui.name/en/wp-content/uploads/2011/01/r-html-help-theme.png"><img class="size-full wp-image-689" title="R HTML Help Theme" src="http://yihui.name/en/wp-content/uploads/2011/01/r-html-help-theme.png" alt="R HTML Help Theme" width="587" height="590" /></a><p class="wp-caption-text">R HTML Help Theme</p></div>
<p>Of course you can design your own <code>R.css</code> if you know CSS.</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2007/12/when-does-statistics-become-meaningless/" title="When Does Statistics Become Meaningless?">When Does Statistics Become Meaningless?</a></li><li><a href="http://yihui.name/en/2010/09/a-slider-to-control-two-plotting-windows/" title="A Slider to Control Two Plotting Windows">A Slider to Control Two Plotting Windows</a></li><li><a href="http://yihui.name/en/2007/09/process-of-minimization-by-gradient-descent/" title="Process of Minimization by Gradient Descent (2D)">Process of Minimization by Gradient Descent (2D)</a></li><li><a href="http://yihui.name/en/2009/10/some-interesting-packages-in-r-swfdevice-rgoogletrends-flashmxml-svgannotation/" title="Some Interesting Packages in R: swfDevice, RGoogleTrends, FlashMXML, SVGAnnotation">Some Interesting Packages in R: swfDevice, RGoogleTrends, FlashMXML, SVGAnnotation</a></li><li><a href="http://yihui.name/en/2007/12/listening-is-a-better-way-to-improve-pronunciation/" title="Listening is A Better Way to Improve Pronunciation">Listening is A Better Way to Improve Pronunciation</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2011/01/customizing-the-theme-of-your-r-html-help/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Happy New Year with R (2011, Fireworks!)</title>
		<link>http://yihui.name/en/2011/01/happy-new-year-with-r-2011-fireworks/</link>
		<comments>http://yihui.name/en/2011/01/happy-new-year-with-r-2011-fireworks/#comments</comments>
		<pubDate>Sat, 01 Jan 2011 07:14:28 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[R Graphics]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[Christmas]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[Fireworks]]></category>
		<category><![CDATA[GIF]]></category>
		<category><![CDATA[New Year]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=681</guid>
		<description><![CDATA[ast year I posted an animation created in R to celebrate the new year, and this year I&#8217;ve got a more fabulous animation. Unfortunately our Lord of CRAN (Kurt) has been out of office for several days, so I&#8217;m unable to publish my animation package on CRAN as scheduled. Anyway, for those who are curious [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://yihui.name/en/2011/01/happy-new-year-with-r-2011-fireworks/"><span class="dropcap">L</span></a>ast year I posted an animation created in R to celebrate the new year, and this year I&#8217;ve got a more fabulous animation. Unfortunately our Lord of CRAN (Kurt) has been out of office for several days, so I&#8217;m unable to publish my <a href="http://cran.r-project.org/web/packages/animation/index.html" target="_blank">animation package</a> on CRAN as scheduled. Anyway, for those who are curious about the new version the <code>animation</code> package, you can <a href="https://github.com/yihui/animation" target="_blank">download the version 2.0-0 from my GitHub development page</a>.</p>
<div id="attachment_682" class="wp-caption aligncenter" style="width: 610px"><a href="http://yihui.name/en/wp-content/uploads/2011/01/fireworks-in-R-2011.gif"><img class="size-full wp-image-682" title="Set Fireworks in R 2011" src="http://yihui.name/en/wp-content/uploads/2011/01/fireworks-in-R-2011.gif" alt="Set Fireworks in R 2011" width="600" height="500" /></a><p class="wp-caption-text">Set Fireworks in R 2011</p></div>
<p>The above animation comes from the <code>demo('fireworks')</code> in the R package <code>animation</code> 2.0-0. Thanks for the contribution of Weicheng Zhu.</p>
<p>Another demo I did not mention on this Christmas was <code>demo('Xmas2')</code>:</p>
<div id="attachment_683" class="wp-caption aligncenter" style="width: 490px"><a href="http://yihui.name/en/wp-content/uploads/2011/01/Xmas-snowflakes.gif"><img class="size-full wp-image-683" title="Xmas snowflakes in R" src="http://yihui.name/en/wp-content/uploads/2011/01/Xmas-snowflakes.gif" alt="Xmas snowflakes in R" width="480" height="480" /></a><p class="wp-caption-text">Xmas snowflakes in R</p></div>
<p>Thanks for the contribution of Jing Jiao.</p>
<p>There are a whole bunch of new features in the <code>animation</code> package v2.0-0 (this version is a milestone), but I will keep silent for the time being, since it is not published on CRAN yet.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2009/12/happy-new-year-with-r/" title="Happy New Year with R">Happy New Year with R</a></li><li><a href="http://yihui.name/en/2009/12/merry-christmas-using-r/" title="Merry Christmas Using R">Merry Christmas Using R</a></li><li><a href="http://yihui.name/en/2008/04/animations-for-spatial-patterns/" title="Animations for Spatial Patterns (simulation only!)">Animations for Spatial Patterns (simulation only!)</a></li><li><a href="http://yihui.name/en/2010/12/formatr-update-0-1-5/" title="formatR update (0.1-5)">formatR update (0.1-5)</a></li><li><a href="http://yihui.name/en/2010/07/my-talk-on-animations-at-user-2010/" title="My Talk on Animations at useR! 2010 (NIST, Gaithersburg)">My Talk on Animations at useR! 2010 (NIST, Gaithersburg)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2011/01/happy-new-year-with-r-2011-fireworks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Special Graphics Device in R: the Null Device</title>
		<link>http://yihui.name/en/2010/12/a-special-graphics-device-in-r-the-null-device/</link>
		<comments>http://yihui.name/en/2010/12/a-special-graphics-device-in-r-the-null-device/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 20:49:51 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[R Graphics]]></category>
		<category><![CDATA[cacheSweave]]></category>
		<category><![CDATA[Graphics Device]]></category>
		<category><![CDATA[Null Device]]></category>
		<category><![CDATA[pdf()]]></category>
		<category><![CDATA[pgfSweave]]></category>
		<category><![CDATA[R_GD_nullDevice]]></category>
		<category><![CDATA[Sweave]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=673</guid>
		<description><![CDATA[It is well-known that R has several graphics devices &#8212; either the screen devices (X11(), windows(), &#8230;) or the off-screen devices (pdf(), png(), &#8230;). We can query the default graphics device in options(): getOption('device') In a non-interactive session, the default device is pdf(). This is why Sweave has to create a file named Rplots.pdf no [...]]]></description>
			<content:encoded><![CDATA[<p>It is well-known that R has several graphics devices &#8212; either the screen devices (<code>X11()</code>, <code>windows()</code>, &#8230;) or the off-screen devices (<code>pdf()</code>, <code>png()</code>, &#8230;). We can query the default graphics device in <code>options()</code>:</p>
<pre class="brush: r">getOption('device')</pre>
<p>In a non-interactive session, the default device is <code>pdf()</code>. This is why Sweave has to create a file named <code>Rplots.pdf</code> <em>no matter if you want it or not</em> when you run Sweave on an Rnw file which has code chunks creating plots. Such a behaviour is annoying to me &#8212; the PDF file is not only unnecessary, but also time-consuming (creating this PDF file is completely a waste of time). Is there a way to set a &#8220;null&#8221; device? (like the <code>/dev/null</code> for *nix users) The answer is yes, but not so obvious. I have not found the device below documented anywhere:</p>
<pre class="brush: r">options(device = function(...) {
    .Call("R_GD_nullDevice", PACKAGE = "grDevices")
})</pre>
<p>This device can speed up Sweave a lot when there are many plots to draw. Here is a comparison:</p>
<pre class="brush: r">x = rnorm(1000)
system.time({
    .Call("R_GD_nullDevice", PACKAGE = "grDevices")
    replicate(500, plot(x, pch = 1:21))
    dev.off()
})
#   user  system elapsed
#   1.51    0.02    1.53
system.time({
    pdf(file.path(tempdir(), "Rplots.pdf"))
    replicate(500, plot(x, pch = 1:21))
    dev.off()
})
#   user  system elapsed
#  47.81    0.20   48.10</pre>
<p>One thing I don&#8217;t understand in Sweave is that it evaluates the code chunk <em>twice</em> if its Sweave options contain <code>fig=TRUE</code>. I think this might be a waste of time as well, and this is why I like <a href="http://cran.r-project.org/web/packages/pgfSweave/index.html"><code>pgfSweave</code></a>, which has both the mechanism of caching R objects (using <code>cacheSweave</code>) and a smart way to cache graphics (using pgf).</p>
<p><strong>WARNING</strong>: this null device may not work with plots that contain (math) expressions! (take a look at <code>demo(plotmath)</code> in case you do not know what are expressions in R graphics)</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2010/03/font-families-for-the-r-pdf-device/" title="Font Families for the R PDF Device">Font Families for the R PDF Device</a></li><li><a href="http://yihui.name/en/2011/05/sweave-and-pgfsweave-in-lyx-2-0-x-experimental/" title="Sweave and pgfSweave in LyX 2.0.x (experimental)">Sweave and pgfSweave in LyX 2.0.x (experimental)</a></li><li><a href="http://yihui.name/en/2010/10/how-to-start-using-pgfsweave-in-lyx-in-one-minute/" title="How to Start Using (pgf)Sweave in LyX in One Minute">How to Start Using (pgf)Sweave in LyX in One Minute</a></li><li><a href="http://yihui.name/en/2010/02/press-enter-in-lyx-sweave-as-you-wish/" title="Press Enter in LyX Sweave as You Wish">Press Enter in LyX Sweave as You Wish</a></li><li><a href="http://yihui.name/en/2011/12/knitr-elegant-flexible-and-fast-dynamic-report-generation-with-r/" title="knitr: Elegant, flexible and fast dynamic report generation with R">knitr: Elegant, flexible and fast dynamic report generation with R</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2010/12/a-special-graphics-device-in-r-the-null-device/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

