Yihui Xie 2012-05-08T21:26:44-07:00 http://yihui.name/ Yihui Xie xie@yihui.name Learn formatR in Two Minutes 2012-05-08T00:00:00-07:00 http://yihui.name/en/2012/05/a-formatr-video <p>Anthony made a <a href="http://www.screenr.com/Vap8">video tutorial</a> on how to use the <a href="https://github.com/yihui/formatR/wiki"><strong>formatR</strong></a> package, which I think is pretty cool:</p> <iframe src="http://www.screenr.com/embed/Vap8" width="600" height="365" frameborder="0"></iframe> <p>I wish I could speak English as fast as him...</p> How to Make HTML5 Slides with knitr 2012-05-01T00:00:00-07:00 http://yihui.name/en/2012/05/how-to-make-html5-slides-with-knitr <p>One week ago I <a href="/en/2012/04/fancy-html5-slides-with-knitr-and-pandoc/">made an early announcement</a> about the markdown support in the <strong>knitr</strong> package and RStudio, and now the version 0.5 of <strong>knitr</strong> <a href="http://cran.r-project.org/package=knitr">is on CRAN</a>, so I'm back to show you how I made the HTML5 slides. For those who are not familiar with markdown, you may read the <a href="http://daringfireball.net/projects/markdown/">traditional documentation</a>, but RStudio has a quicker reference (see below). The problem with markdown is that the original invention seems to be too simple, so quite a few variants were derived later (e.g. to support tables); that is another story, and you do not need to worry much about it.</p> <p>Before you get started, make sure your <strong>knitr</strong> version is at least 0.5:</p> <div class="highlight"><pre><code class="r"><span class="c1"># install.packages(c(&#39;knitr&#39;, &#39;XML&#39;, &#39;RCurl&#39;))</span> update.packages<span class="p">(</span>ask <span class="o">=</span> <span class="kc">FALSE</span><span class="p">)</span> packageVersion<span class="p">(</span><span class="s">&#39;knitr&#39;</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="m">0.5</span> </code></pre> </div> <h2>Editor: RStudio</h2> <p>You need to install the <a href="http://www.rstudio.org/download/preview">RStudio preview version</a> to use its new features on markdown support. With this version, you will see an interface like this when you create an R markdown file (<code>File --&gt; New --&gt; R Markdown</code>):</p> <p><img src="http://i.imgur.com/9YlzL.png" alt="R markdown interface in RStudio" /></p> <p>The button <code>MD</code> in the toolbar shows a quick reference of the markdown syntax, which I believe you can learn in 3 minutes. To start with, you can use my example on Github: <a href="https://github.com/yihui/knitr/blob/master/inst/examples/knitr-slides.Rmd">knitr-slides.Rmd</a>, or quickly cook up your own by <code>Ctrl + Shift + &gt;</code> to insert code chunks.</p> <p>You can write headers with <code>#</code> and bullet points with <code>-</code>. It is both quick to write and easy to remember (and readable too). When you are done, just hit the button <code>Knit HTML</code>, and you get a nice HTML page showing you R code and the output. You do not have to learn LaTeX in order to step into the realm of reproducible research. (Did you see the Binomial pmf there?!)</p> <h2>Converter: Pandoc</h2> <p>What happens behind the scene is that RStudio calls <strong>knitr</strong> to compile the Rmd document to a markdown file (you can see it under the same directory as the Rmd file), and convert this file to HTML. This is a very nice feature, and we can actually go further.</p> <p><a href="http://johnmacfarlane.net/pandoc/">Pandoc</a> claims itself to be a universal document converter, and it is indeed very powerful. For the above example, we can convert the markdown output (not Rmd source) to many other formats like HTML, LaTeX, Open Office or Microsoft Office documents. HTML5 slides are also supported. This is the single command that I used to convert <code>knitr-slides.md</code> to DZslides:</p> <div class="highlight"><pre><code class="bash">pandoc -s -S -i -t dzslides --mathjax knitr-slides.md -o knitr-slides.html </code></pre> </div> <p>Then you get an HTML file <a href="http://yihui.name/slides/knitr-slides.html">knitr-slides.html</a> which you can view in a modern web browser. Enjoy.</p> <h2>Final words</h2> <p>HTML5 slides is just one tiny thing that you can play with markdown; check out the pandoc documentation to see more possibilities. That being said, I feel most excited about the RStudio integration with <strong>knitr</strong> and markdown. LaTeX is beautiful but difficult to learn and laborious to write. MS Word is most widely used but you know...</p> <p>I believe this combination makes reproducible research much more accessible to the general audience, and I hope to see it being used in statistical courses so that students no longer do tedious jobs of copy &amp; paste, and professors no longer suffer from ugly Word reports.</p> <p>Now I have done pretty much what I planned in the beginning. The next step will be our <a href="http://www.google-melange.com/gsoc/project/google/gsoc2012/cloud_wei/16001">GSoC project</a>, in which we will make the toolchain smoother, and work out better ways for R users to document packages and publish web pages (e.g. blogging like a hacker). If you want to follow our latest changes, you may</p> <ul> <li>watch the development repository on Github <a href="https://github.com/yihui/knitr">https://github.com/yihui/knitr</a></li> <li>or join the Google mailing list <a href="https://groups.google.com/group/knitr">https://groups.google.com/group/knitr</a></li> </ul> <p>And final ads: I will be presenting <strong>knitr</strong> at <a href="http://biostat.mc.vanderbilt.edu/wiki/Main/UseR-2012">useR! 2012</a> with JJ from RStudio. I'm looking forward to meeting more knitters in Nashville :)</p> Fancy HTML5 Slides with knitr and pandoc 2012-04-22T00:00:00-07:00 http://yihui.name/en/2012/04/fancy-html5-slides-with-knitr-and-pandoc <p>Karthik Ram gave an <a href="http://inundata.org/R_talks/meetup/">Introduction to R</a> a couple of weeks ago, and I strongly recommend you to take a look at his cool HTML5 slides. I started <a href="/slides/2011-r-dev-lessons.html">trying HTML5 slides</a> last year, and now it is difficult for me to go back to beamer, which I have used for a few years for my presenations. It is horrible to see beamer slides everywhere at academic conferences (especially the classic blue themes).</p> <p><img src="http://i.imgur.com/CiY3K.png" alt="Traces of all vessels" /></p> <p>You probably have heard of an interesting blog post by Ben Schmidt about <a href="http://sappingattention.blogspot.co.uk/2012/04/visualizing-ocean-shipping.html">ocean shipping animations</a> in the 18th and 19th centuries. I also played with the dataset a little bit, and made some slides named <a href="/slides/stat585x-shipping-yihui-xie.html">Voyages of Sinbad the Sailor</a> (use Left/Right or Up/Down to navigate). The source file was written in markdown, compiled by <a href="/knitr/"><strong>knitr</strong></a>, then converted to DZSlides by <a href="http://johnmacfarlane.net/pandoc/">pandoc</a>.</p> <p>I'm using the development version of <strong>knitr</strong>, which you can install from <a href="https://github.com/yihui/knitr/#readme">Github</a>. I plan to release the version 0.5 this weekend, and this version will particularly feature the markdown support. You can always read the <a href="https://github.com/yihui/knitr/blob/master/NEWS">NEWS</a> file to know what is going on in the development.</p> <p>Another piece of news which may be a little bit early to announce is the corresponding support in RStudio. I'm not going to say any details about it right now, but I'm pretty sure the so-called reproducible research and dynamic report generation can be easier than ever very soon! No LaTeX. No worries about HTML/CSS. A simple text file and a single click will give your a reasonably beautiful HTML page. Stay tuned.</p> Ideas on A Really Fast Statistics Journal 2012-03-15T00:00:00-07:00 http://yihui.name/en/2012/03/a-really-fast-statistics-journal <p>I was writing comments on the blog post <a href="http://simplystatistics.tumblr.com/post/19289280474/a-proposal-for-a-really-fast-statistics-journal">A proposal for a really fast statistics journal</a>, and I realized the comment box was too small to write down my ideas. I like the proposal a lot, and I feel really bad about the current model of submitting and reviewing papers: it is just too slow! We should not blame it on editors and reviewers; everybody is busy. The question is how to reduce unnecessary efforts in this process. My ideas are close to one possible actual implementation of Jeff's proposal.</p> <p>There are a few stupid rules in some journals which I hate a lot and I believe they are wasting the time of everybody, e.g.</p> <ul> <li>file format of figures; I especially despite journals which require authors to use EPS (postscript) or WMF (Windows metafile)</li> <li>double-spaced typesetting: oh man, stop killing trees! It makes no sense to typeset with a large line height, unless you mail the papers to reviewers and ask them to write reviews with a pen, then mail the review back to the author; you do not really do this, so why ask authors to use the ugly double-spaced typesetting?</li> <li>leave figures/tables blank in the body of the paper (write <em>Insert Figure 1 Here</em>) and put them in other places</li> </ul> <p>I have to remind myself that this is the 21st century. These are actually not a big deal in terms of statistics papers. The more important problem is journals are operated by a very small group of people relative to the large number of authors. The even more important problem is reproducibility, which Roger Peng often mentions in that blog.</p> <h1>An open-access journal</h1> <p>This journal is electronic like <a href="http://www.jstatsoft.org/">JSS</a>, and you can see everything in the website. For authors, LaTeX is too laborious, so we abandon LaTeX. For website maintainers, HTML is laborious as well, and we abandon HTML.</p> <p>We turn to <a href="http://daringfireball.net/projects/markdown/">markdown</a>. Why? It is simple. I hate writing a paper with <code>\documentclass{}</code> as my first line -- I want to start with the title directly in plain text, then write my first paragraph in a simple text editor. Who cares about <code>\title{}</code>? Similarly, when I write a webpage, I do not like starting with <code>&lt;html&gt;</code> as my first six characters.</p> <p>My own website is built on <a href="https://github.com/mojombo/jekyll">Jekyll</a>, a blog engine based on markdown files to build static HTML pages. The first few days of building this website might be hard, but once the design is done, the rest of the job becomes extremely easy. Wordpress is good, but it is still too heavy compared to Jekyll. See my <a href="https://github.com/yihui/en">blog source code</a>, and this blog site is hosted on GitHub, which automatically compiles my markdown files to HTML pages using Jekyll when I submit new posts or changes to the source repository.</p> <p>With a good web designer, this journal website can be set up probably in a week. In the future, neither maintainers nor paper authors need to deal with HTML any more. Paper source code looks like:</p> <div class="highlight"><pre><code class="text"># Title ## Abstract Here is the abstract. ## First section Write write and write. plot(1) # code to generate figures ... </code></pre> </div> <p>I will explain how we maintain this journal and talk about <code>plot(1)</code> later.</p> <h1>Faster submission and review</h1> <p>I do not see much difference in submitting computer code and papers, so I'd like to use a version control model for papers as well. As I mentioned in my <a href="/en/2011/12/how-to-become-an-efficient-and-collaborative-r-programmer/">last blog post</a>, GIT and GitHub can make this process fast and easy.</p> <p>We put the journal website under version control, and all the source code is on GitHub. Authors fork the journal repository and put their papers in the repository. Paper submission is through <a href="http://help.github.com/send-pull-requests/">pull requests</a>, which means the author wants to merge his/her changes (paper) back to the main repository.</p> <p>Now the chief editor sees the request, and he/she writes a comment under the request like: <code>@jeff</code> and <code>@roger</code>, please review the new paper. Then Jeff and Roger start looking at the paper. If they want to make comments, they comment <em>right below</em> the lines of the texts, because GitHub supports inline comments in your code. (So why the hell should we continue writing double-spaced papers?)</p> <p>After they are done, the editor asks the author to read the reviews, revise the paper, commit back again and the reviews can easily see what was changed in the revision, because GIT gives you full history of the revision. You see that the reviewers do not need to re-read the whole paper -- they just look at the revision history. If the problems of the paper are too serious, reviews can write bug reports (GitHub issues) in the author's repository and ask the author to fix these "bugs".</p> <p>This process can go back and forth, until the editor decides to accept the pull request. Once the new paper is pulled in, Jekyll will recompile the website automatically.</p> <p>Besides the designated reviewers, all other people can see and comment on the pull request as well, so the paper can be essentially under public review like open source software! If the two reviewers are busy, there might be other good public reviewers helping the review.</p> <p>You may doubt on the "quality" of public review, but just think about the quality Linux and open source software. If we only rely on Linus Torvalds or Richard Stallman to write code, you can imagine how the open source world looks like today.</p> <p>The journal can find plenty of good and <em>active</em> reviewers automatically after a while -- they come as a result of <em>natural selection</em>; this is better than requesting someone to be a reviewer because it is the "internal force" that drives the public reviewers. In the current journal model, reviewers may feel it is a <em>job</em> to review papers, so motivation becomes a problem.</p> <h1>Reproducible papers</h1> <p>I'm quite interested in reproducible research, and I'm all for reproducible results in papers. Well, if you are a statistician, R and Sweave have probably come into your mind. Sweave is great, and the idea of literate programming is awesome. It is just a shame that great ideas were implemented with a narrow vision. I can understand Sweave was tied to LaTeX, but that is a narrow choice. I feel painful to introduce Sweave to other people because I have to teach them LaTeX first.</p> <p>I do not need to repeat <a href="https://github.com/yihui/knitr#readme">my motivations</a> to write another R package <a href="/knitr/">knitr</a> to replace Sweave. The relevant point is reproducible research should be easier for the general public, and LaTeX is no way to go. Again, markdown is better. You will never be trapped in all sorts of weird TeX errors. Open a notepad (can be literally Windows Notepad or other text editors), write the text you want, then the code; compile this text file, you get your paper/report. This should be the case if we want to popularize reproducible research. If you have spent many hours on writing a TeX document and carefully embedding R code in it, then compile it...</p> <ul> <li>Boom! TeX errors!</li> <li>Boom! Figures too wide/narrow!</li> <li>Boom! Figures floating away!</li> <li>Boom! Editors ask you to use EPS instead of PDF figures!</li> <li>Boom! R errors!</li> <li>...</li> </ul> <p>The <strong>knitr</strong> package supports markdown naturally. You can see a <a href="https://github.com/yihui/knitr/blob/master/inst/examples/knitr-minimal.md">hello-world example</a> and <a href="http://t.co/eBcm59sW">a real example</a>.</p> <p>I'm not yet very confident about working with markdown and <strong>knitr</strong> because I have not started using this combination; in the past few months I have been focusing on the LaTeX support, but I'll start eating my own markdown dog food soon.</p> <p>People keep on telling you LaTeX frees your mind, because you only focus on writing and do not worry about styles, and that is a beautiful lie. You worry about styles all day long, and the tricks about styles are usually gory. I would rather say CSS frees you.</p> <p>OK, this journal only accepts simple markdown files. No more argument. As I showed you, you can embed R code in markdown files to compile your papers dynamically. In the future, the server of the journal may compile the papers periodically as well to check if they are still valid. It is easy for reviewers to validate the results too. All authors are required to provide datasets associated with the paper, or at least access to the datasets (e.g. URL's).</p> <p>We use web pages to present papers. No PDF. A careful design of CSS can beat LaTeX/PDF and give us beautiful web pages. If readers have strong objections to this, we use <a href="http://johnmacfarlane.net/pandoc/">pandoc</a> to convert markdown papers to PDF. Math expressions? No problem! Use <a href="http://www.mathjax.org/">MathJax</a>. There are no page limits, and all figures are inserted in appropriate places (they will not float and readers do not need to turn the pages desparately to find your <code>Figure 7</code>).</p> <p>If we do everything in web pages, there are yet more possibilities.</p> <ul> <li>Use SVG graphics or PNG (<strong>knitr</strong> supports more than 20 formats, and it is easy to switch). Let EPS and WMF die.</li> <li>Feel R plots are ugly? Want animations? Interactive graphics? No problem! How about using <a href="http://mbostock.github.com/d3/">D3</a>? How about <a href="http://processingjs.org/">Processing.js</a>?</li> <li>Want to talk to authors? No problem! Leave a comment under the paper just like you comment on blog posts.</li> <li>Want to rate a paper? Yes, hit the "Like" button. We do not only let reviewers of the journal rate papers.</li> <li>Web statistics about a paper? Yes, we have page views and other statistics. We no longer use the number of times that a paper is referenced to evaluate a paper; we see how many times it has been read. People may add references for various reasons (not necessarily related to the paper).</li> </ul> <h1>Summary</h1> <p>We have been in the age of Web 2.0 for a few years, but journals rarely make good use of the "crowd". We build the world on a small group of people. Our good papers are used to fill in the shelves of libraries and collect dust, when bad articles are occupying the web (like my blog posts) and being discussed actively. We make all sorts of rules to tie the hands of authors, and we wait, wait and wait for the anonymous reviewers, like <a href="http://en.wikipedia.org/wiki/Waiting_for_Godot">waiting for Godot</a>.</p> How to Become an Efficient and Collaborative R Programmer 2011-12-12T00:00:00-08:00 http://yihui.name/en/2011/12/how-to-become-an-efficient-and-collaborative-r-programmer <p>I may want to add a subtitle "Why R-Forge Must Die" (thinking of Barry Rowlingson'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 href="https://github.com/hadley">Hadley</a>. Now I even feel a little bit addicted to GitHub (its slogan is "social coding"), 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 <a href="https://github.com/yihui/knitr">knitr</a> 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'm pretty satisfied with my attempts. I call GitHub the "Facebook" 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 <code>fixed #46</code> 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 "I like this and you may need to revise that, ...". In comparison, the traditional way of collaboration is usually through email -- 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'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.name/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'm already an efficient R programmer, but GitHub did make me much more efficient.</p> knitr: Elegant, flexible and fast dynamic report generation with R 2011-12-01T00:00:00-08:00 http://yihui.name/en/2011/12/knitr-elegant-flexible-and-fast-dynamic-report-generation-with-r <p>The world has changed. You can feel it on <a href="https://github.com/yihui/knitr">GitHub</a>. You can smell it on <a href="https://plus.google.com/u/0/109653178371807724268/posts">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">the PDF manual</a> to see some of them.</p> <p>Currently this package is still a beta version, so I'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.name/knitr">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'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'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">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> The fun Package: Use R for Fun! 2011-08-16T00:00:00-07:00 http://yihui.name/en/2011/08/the-fun-package-use-r-for-fun <p>A couple of days ago we released a package named <a href="http://cran.r-project.org/package=fun">fun</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> <div class="highlight"><pre><code class="r"><span class="c1">## install.packages(&#39;fun&#39;)</span> library<span class="p">(</span>fun<span class="p">)</span> <span class="kr">if</span> <span class="p">(</span><span class="m">.</span>Platform<span class="p">$</span>OS.type <span class="o">==</span> <span class="s">&quot;windows&quot;</span><span class="p">)</span> x11<span class="p">()</span> <span class="kr">else</span> x11<span class="p">(</span>type <span class="o">=</span> <span class="s">&quot;Xlib&quot;</span><span class="p">)</span> mine_sweeper<span class="p">()</span> </code></pre> </div> <p><img src="http://i.imgur.com/U4oAT.png" alt="Mine Sweeper in R" /></p> <div class="highlight"><pre><code class="r">library<span class="p">(</span>fun<span class="p">)</span> gomoku<span class="p">()</span> </code></pre> </div> <p><img src="http://i.imgur.com/8jcQl.png" alt="Five in a row in R" /></p> <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> <div class="highlight"><pre><code class="r">demo<span class="p">(</span><span class="s">&#39;RealTurtle&#39;</span><span class="p">,</span> package <span class="o">=</span> <span class="s">&#39;fun&#39;</span><span class="p">)</span> </code></pre> </div> <p><img src="http://i.imgur.com/oWdOu.png" alt="A turtle drawn in R" /></p> <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 "Five in a row" in <a href="http://cos.name/cn/">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">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' page is at: <a href="https://github.com/yihui/fun">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">sudoku</a> package (e.g. <a href="https://plus.google.com/109653178371807724268/posts/eGKq27aqK5b">Joshua Wiley has noticed it</a>), and some people may even remember this:</p> <div class="highlight"><pre><code class="r">library<span class="p">(</span>fortunes<span class="p">)</span> fortune<span class="p">(</span><span class="s">&#39;sudoku&#39;</span><span class="p">)</span> </code></pre> </div> Sweave and pgfSweave in LyX 2.0.x (experimental) 2011-05-25T00:00:00-07:00 http://yihui.name/en/2011/05/sweave-and-pgfsweave-in-lyx-2-0-x-experimental <p>Please ignore this post completely, because Sweave support has become mature in LyX since 2.0.2, and I no longer plan to add the pgfSweave module in LyX. For pgfSweave users, you may consider the new <a href="http://yihui.name/knitr/demo/lyx/">knitr module</a> (available since 2.0.3) which uses the R package <a href="http://cran.r-project.org/package=knitr">knitr</a>.</p> <p><img src="http://i.imgur.com/jEKSh.png" alt="knitr module in LyX 2.0.3" /></p> Produce Authentic Math Formulas in R Graphics 2011-04-30T00:00:00-07:00 http://yihui.name/en/2011/04/produce-authentic-math-formulas-in-r-graphics <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">tikzDevice</a> package! Why bother to use all kinds of tricks to cheat R? :)</p> <p>Here is an example per request of a reader of my blog:</p> <p><img src="http://i.imgur.com/3mWKw.png" alt="authentic math formula in R" /></p> <div class="highlight"><pre><code class="r">library<span class="p">(</span>tikzDevice<span class="p">)</span> options<span class="p">(</span>tikzMetricPackages <span class="o">=</span> c<span class="p">(</span><span class="s">&quot;\\usepackage[utf8]{inputenc}&quot;</span><span class="p">,</span> <span class="s">&quot;\\usepackage[T1]{fontenc}&quot;</span><span class="p">,</span> <span class="s">&quot;\\usetikzlibrary{calc}&quot;</span><span class="p">,</span> <span class="s">&quot;\\usepackage{amssymb}&quot;</span><span class="p">))</span> <span class="c1">## I need the amssymb package because I use \mathcal and \mathbb</span> tikz<span class="p">(</span><span class="s">&quot;formula.tex&quot;</span><span class="p">,</span> width <span class="o">=</span> <span class="m">4</span><span class="p">,</span> height <span class="o">=</span> <span class="m">4</span><span class="p">,</span> standAlone <span class="o">=</span> <span class="kc">TRUE</span><span class="p">,</span> packages <span class="o">=</span> c<span class="p">(</span><span class="s">&quot;\\usepackage{tikz}&quot;</span><span class="p">,</span> <span class="s">&quot;\\usepackage[active,tightpage,psfixbb]{preview}&quot;</span><span class="p">,</span> <span class="s">&quot;\\PreviewEnvironment{pgfpicture}&quot;</span><span class="p">,</span> <span class="s">&quot;\\setlength\\PreviewBorder{0pt}&quot;</span><span class="p">,</span> <span class="s">&quot;\\usepackage{amssymb}&quot;</span><span class="p">))</span> par<span class="p">(</span>mar <span class="o">=</span> c<span class="p">(</span><span class="m">4</span><span class="p">,</span> <span class="m">4</span><span class="p">,</span> <span class="m">0.1</span><span class="p">,</span> <span class="m">0.1</span><span class="p">),</span> mgp <span class="o">=</span> c<span class="p">(</span><span class="m">2</span><span class="p">,</span> <span class="m">0.9</span><span class="p">,</span> <span class="m">0</span><span class="p">))</span> plot<span class="p">(</span><span class="m">1</span><span class="p">,</span> type <span class="o">=</span> <span class="s">&quot;n&quot;</span><span class="p">,</span> xlab <span class="o">=</span> <span class="s">&quot;$x_1$&quot;</span><span class="p">,</span> ylab <span class="o">=</span> <span class="s">&quot;$x_2$&quot;</span><span class="p">)</span> text<span class="p">(</span><span class="m">1</span><span class="p">,</span> c<span class="p">(</span><span class="m">0.8</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">1.2</span><span class="p">),</span> c<span class="p">(</span><span class="s">&quot;$\\underbrace{1,2,\\cdots,10}_{10}$&quot;</span><span class="p">,</span> <span class="s">&quot;$\\mathbb{ABCDEFG}$&quot;</span><span class="p">,</span> <span class="s">&quot;$\\mathcal{HIJKLMN}$&quot;</span><span class="p">),</span> cex <span class="o">=</span> <span class="m">2.5</span><span class="p">)</span> dev.off<span class="p">()</span> tools<span class="p">::</span>texi2pdf<span class="p">(</span><span class="s">&quot;formula.tex&quot;</span><span class="p">)</span> system<span class="p">(</span>paste<span class="p">(</span>getOption<span class="p">(</span><span class="s">&quot;pdfviewer&quot;</span><span class="p">),</span> <span class="s">&quot;formula.pdf&quot;</span><span class="p">))</span> </code></pre> </div> New versions of GGobi and rggobi for Windows users 2011-04-09T00:00:00-07:00 http://yihui.name/en/2011/04/new-versions-of-ggobi-and-rggobi-for-windows-users <p>For those who have been struggling with the installation of <a href="http://www.ggobi.org">GGobi</a> and the <a href="http://cran.r-project.org/package=rggobi">rggobi package</a> under Windows: a major update of <a href="http://www.ggobi.org/downloads/ggobi-2.1.9.exe">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">RGtk2 package</a>, you don'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's from GGobi's directory.</p>