<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/assets/rss.xsl" type="text/xsl"?><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>Sacha Chua - tag - R</title>
	<atom:link href="https://sachachua.com/blog/tag/R/feed/index.xml" rel="self" type="application/rss+xml" />
	<atom:link href="https://sachachua.com/blog/tag/R" rel="alternate" type="text/html" />
	<link>https://sachachua.com/blog/tag/R/feed/index.xml</link>
	<description>Emacs, sketches, and life</description>
  
	<lastBuildDate>Mon, 06 Jul 2026 16:20:27 GMT</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>daily</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>11ty</generator>
  <item>
		<title>Getting data from Org Mode tables</title>
		<link>https://sachachua.com/blog/2015/01/getting-data-org-mode-tables/</link>
		<dc:creator><![CDATA[Sacha Chua]]></dc:creator>
		<pubDate>Fri, 30 Jan 2015 21:00:00 GMT</pubDate>
    <category>emacs</category>
<category>org</category>
		<guid isPermaLink="false">https://sachachua.com/blog/?p=27871</guid>
		<description><![CDATA[<p><a href="http://orgmode.org/">Org Mode</a> is an amazingly powerful package for Emacs. I&#8217;ve been learning a lot about how to use its support for plain-text tables and spreadsheet calculations.</p>
<div id="outline-container-unnumbered-1" class="outline-3">
<h3 id="unnumbered-1">Using table data in Emacs Lisp with the :var argument</h3>
<div id="text-unnumbered-1" class="outline-text-3">
<p>For example, I wanted to be able to define my abbreviations in an Org Mode table in my <a href="https://sachachua.com/dotemacs">config</a>. I remembered coming across this technique a few weeks ago, but I couldn&#8217;t find the webpage with the code. It turned out to be simple to write from scratch. Here&#8217;s the plain text I added to my config.</p>
<div class="org-src-container">
<pre class="src src-org"><span class="org-org-meta-line">#+NAME: abbrev</span>
<span class="org-org-table">| Base  | Expansion                             |</span>
<span class="org-org-table">|&#45;&#45;&#45;&#45;&#45;&#45;-+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;-|</span>
<span class="org-org-table">| bc    | because                               |</span>
<span class="org-org-table">| wo    | without                               |</span>
<span class="org-org-table">| wi    | with                                  |</span>
<span class="org-org-table">| ex    | For example,                          |</span>
<span class="org-org-table">| qm    | sacha@sachachua.com                   |</span>
<span class="org-org-table">| qe    | </span><span class="org-org-link">http://sachachua.com/dotemacs</span><span class="org-org-table">         |</span>
<span class="org-org-table">| qw    | </span><span class="org-org-link">http://sachachua.com/</span><span class="org-org-table">                 |</span>
<span class="org-org-table">| qb    | </span><span class="org-org-link">http://sachachua.com/blog/</span><span class="org-org-table">            |</span>
<span class="org-org-table">| qc    | </span><span class="org-org-link">http://sachachua.com/blog/emacs-chat/</span><span class="org-org-table"> |</span>

<span class="org-org-block-begin-line">#+begin_src emacs-lisp :exports code :var data=abbrev</span>
(mapc (<span class="org-keyword">lambda</span> (x) (define-global-abbrev (car x) (cadr x))) data)
<span class="org-org-block-end-line">#+end_src</span>
</pre>
</div>
<p>The <code>:var data=abbrev</code> argument to the Emacs Lisp source block is where all the magic happens. Here, it takes the data from the table named &#8220;abbrev&#8221; (which I set using <code>#+NAME:</code> before the table) and makes it available to the code. Emacs evaluates that data when the code is tangled (or exported) to my configuration. The code that&#8217;s in my <code>Sacha.el</code> looks like this:</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-keyword">let</span> <span class="org-rainbow-delimiters-depth-2">(</span><span class="org-rainbow-delimiters-depth-3">(</span>data <span class="org-rainbow-delimiters-depth-4">(</span>quote <span class="org-rainbow-delimiters-depth-5">(</span><span class="org-rainbow-delimiters-depth-6">(</span><span class="org-string">"bc"</span> <span class="org-string">"because"</span><span class="org-rainbow-delimiters-depth-6">)</span>
                    <span class="org-rainbow-delimiters-depth-6">(</span><span class="org-string">"wo"</span> <span class="org-string">"without"</span><span class="org-rainbow-delimiters-depth-6">)</span>
                    <span class="org-rainbow-delimiters-depth-6">(</span><span class="org-string">"wi"</span> <span class="org-string">"with"</span><span class="org-rainbow-delimiters-depth-6">)</span>
                    <span class="org-rainbow-delimiters-depth-6">(</span><span class="org-string">"ex"</span> <span class="org-string">"For example,"</span><span class="org-rainbow-delimiters-depth-6">)</span>
                    <span class="org-rainbow-delimiters-depth-6">(</span><span class="org-string">"email"</span> <span class="org-string">"sacha@sachachua.com"</span><span class="org-rainbow-delimiters-depth-6">)</span>
                    <span class="org-rainbow-delimiters-depth-6">(</span><span class="org-string">"dote"</span> <span class="org-string">"http://sachachua.com/dotemacs"</span><span class="org-rainbow-delimiters-depth-6">)</span>
                    <span class="org-rainbow-delimiters-depth-6">(</span><span class="org-string">"web"</span> <span class="org-string">"http://sachachua.com/"</span><span class="org-rainbow-delimiters-depth-6">)</span>
                    <span class="org-rainbow-delimiters-depth-6">(</span><span class="org-string">"blog"</span> <span class="org-string">"http://sachachua.com/blog/"</span><span class="org-rainbow-delimiters-depth-6">)</span>
                    <span class="org-rainbow-delimiters-depth-6">(</span><span class="org-string">"ec"</span> <span class="org-string">"http://sachachua.com/blog/emacs-chat/"</span><span class="org-rainbow-delimiters-depth-6">)</span><span class="org-rainbow-delimiters-depth-5">)</span><span class="org-rainbow-delimiters-depth-4">)</span><span class="org-rainbow-delimiters-depth-3">)</span><span class="org-rainbow-delimiters-depth-2">)</span>
  <span class="org-rainbow-delimiters-depth-2">(</span>mapc <span class="org-rainbow-delimiters-depth-3">(</span><span class="org-keyword">lambda</span> <span class="org-rainbow-delimiters-depth-4">(</span>x<span class="org-rainbow-delimiters-depth-4">)</span> <span class="org-rainbow-delimiters-depth-4">(</span>define-global-abbrev <span class="org-rainbow-delimiters-depth-5">(</span>car x<span class="org-rainbow-delimiters-depth-5">)</span> <span class="org-rainbow-delimiters-depth-5">(</span>cadr x<span class="org-rainbow-delimiters-depth-5">)</span><span class="org-rainbow-delimiters-depth-4">)</span><span class="org-rainbow-delimiters-depth-3">)</span> data<span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-unnumbered-2" class="outline-3">
<h3 id="unnumbered-2">Looking up data with org-lookup-first, org-lookup-last, and org-lookup-all</h3>
<div id="text-unnumbered-2" class="outline-text-3">
<p>You can do more complex things with Org tables, too. Inspired by Eric Boyd&#8217;s talk on his <a href="http://quest.digitalcrusader.ca/">Epic Quest of Awesome</a> (which he based on <a href="http://nerdfitness.com/blog/epic-quest/">Steve Kamb</a>&#8216;s), I started putting together <i>my own</i>. I made a list of little achievements, guessed at the years, and assigned arbitrary experience points.</p>
<p>The <code>achievements</code> table had rows like this:</p>
<table border="2" frame="hsides" rules="groups" cellspacing="0" cellpadding="6">
<colgroup>
<col class="right">
<col class="left">
<col class="right">
<col class="left">
<col class="left"> </colgroup>
<thead>
<tr>
<th class="right" scope="col">Approximate date</th>
<th class="left" scope="col">Category</th>
<th class="right" scope="col">XP</th>
<th class="left" scope="col">Description</th>
<th class="left" scope="col">ID</th>
</tr>
</thead>
<tbody>
<tr>
<td class="right">2014</td>
<td class="left">Life</td>
<td class="right">50</td>
<td class="left">Became a Canadian citizen &#8211; <a href="https://sachachua.com/blog/2014/09/becoming-filipino-canadian/">link</a></td>
<td class="left">L_CAN</td>
</tr>
<tr>
<td class="right">2014</td>
<td class="left">Programming</td>
<td class="right">20</td>
<td class="left">Used NodeJS and AngularJS for a client project &#8211; <a href="https://sachachua.com/blog/2014/09/yay-rocked/">link</a></td>
<td class="left">P_NOD</td>
</tr>
<tr>
<td class="right">2014</td>
<td class="left">Programming</td>
<td class="right">5</td>
<td class="left">Pulled information out of Evernote</td>
<td class="left"></td>
</tr>
</tbody>
</table>
<p>I wanted to summarize the points by year: points gained, total points, level (according to a lookup table based on D&amp;D experience points), and description. The lookup table was structured like this:</p>
<div class="org-src-container">
<pre class="src src-org"><span class="org-org-meta-line">#+TBLNAME: levels</span>
<span class="org-org-table">| Total XP | Level | Adjective             |</span>
<span class="org-org-table">|&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;+&#45;&#45;&#45;&#45;&#45;&#45;-+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;-|</span>
<span class="org-org-table">|        0 |     1 | trained-initiate      |</span>
<span class="org-org-table">|     1000 |     2 | experienced           |</span>
<span class="org-org-table">|     2250 |     3 | savvy                 |</span>
<span class="org-org-table">|     3750 |     4 | veteran               |</span>
<span class="org-org-table">|     5500 |     5 | unusually experienced |</span>
</pre>
</div>
<p>Now for the summary table. I created rows for different years, and then I used Org Mode to fill in the rest. (Org Mode! Wow.)</p>
<div class="org-src-container">
<pre class="src src-org"><span class="org-org-table">| Year | Points gained | Cumulative points | Level | Adjective        |</span>
<span class="org-org-table">|&#45;&#45;&#45;&#45;&#45;&#45;+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;-+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;-+&#45;&#45;&#45;&#45;&#45;&#45;-+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;|</span>
<span class="org-org-table">| 1997 |             0 |                 0 |     1 | trained-initiate |</span>
<span class="org-org-table">| 1998 |            10 |                10 |     1 | trained-initiate |</span>
<span class="org-org-table">| 1999 |            50 |                60 |     1 | trained-initiate |</span>
<span class="org-org-table">| 2000 |            50 |               110 |     1 | trained-initiate |</span>
<span class="org-org-table">| 2001 |           100 |               210 |     1 | trained-initiate |</span>
<span class="org-org-table">| 2002 |            60 |               270 |     1 | trained-initiate |</span>
<span class="org-org-table">| 2003 |           245 |               515 |     1 | trained-initiate |</span>
<span class="org-org-table">| 2004 |           115 |               630 |     1 | trained-initiate |</span>
<span class="org-org-table">| 2005 |           140 |               770 |     1 | trained-initiate |</span>
<span class="org-org-table">| 2006 |            60 |               830 |     1 | trained-initiate |</span>
<span class="org-org-table">| 2007 |           270 |              1100 |     2 | experienced      |</span>
<span class="org-org-table">| 2008 |           290 |              1390 |     2 | experienced      |</span>
<span class="org-org-table">| 2009 |           205 |              1595 |     2 | experienced      |</span>
<span class="org-org-table">| 2010 |           215 |              1810 |     2 | experienced      |</span>
<span class="org-org-table">| 2011 |           115 |              1925 |     2 | experienced      |</span>
<span class="org-org-table">| 2012 |           355 |              2280 |     3 | savvy            |</span>
<span class="org-org-table">| 2013 |           290 |              2570 |     3 | savvy            |</span>
<span class="org-org-table">| 2014 |           350 |              2920 |     3 | savvy            |</span>
<span class="org-org-table">| 2015 |            45 |              2965 |     3 | savvy            |</span>
<span class="org-org-meta-line">#+TBLFM: $2='(calc-eval (format "vsum(%s)" (vconcat (org-lookup-all $1 '(remote(accomplishments,@2$1..@&gt;$1)) '(remote(accomplishments,@2$3..@&gt;$3))))))::$3=vsum(@2$2..@+0$2)::$4='(org-lookup-last $3 '(remote(levels,@2$1..@&gt;$1)) '(remote(levels,@2$2..@&gt;$2)) '&gt;=);N::$5='(org-lookup-last $3 '(remote(levels,@2$1..@&gt;$1)) '(remote(levels,@2$3..@&gt;$3)) '&gt;=);L</span>
</pre>
</div>
<p>The <code>TBLFM</code> (table formula) line is very long, so let me break it down.</p>
<p><b>Points gained:</b></p>
<div class="org-src-container">
<pre class="src src-emacs-lisp"><span class="org-rainbow-delimiters-depth-1">(</span>calc-eval
 <span class="org-rainbow-delimiters-depth-2">(</span>format <span class="org-string">"vsum(%s)"</span>
         <span class="org-rainbow-delimiters-depth-3">(</span>vconcat
          <span class="org-rainbow-delimiters-depth-4">(</span>org-lookup-all
           $1
           '<span class="org-rainbow-delimiters-depth-5">(</span>remote<span class="org-rainbow-delimiters-depth-6">(</span>accomplishments,@2$1..@&gt;$1<span class="org-rainbow-delimiters-depth-6">)</span><span class="org-rainbow-delimiters-depth-5">)</span>
           '<span class="org-rainbow-delimiters-depth-5">(</span>remote<span class="org-rainbow-delimiters-depth-6">(</span>accomplishments,@2$3..@&gt;$3<span class="org-rainbow-delimiters-depth-6">)</span><span class="org-rainbow-delimiters-depth-5">)</span><span class="org-rainbow-delimiters-depth-4">)</span><span class="org-rainbow-delimiters-depth-3">)</span><span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<p>This uses <code>org-lookup-all</code> to look up the value of the first column (<code>$1</code>) in the <code>accomplishments</code> table, from the second row to the last row <code>@2..@&gt;</code>, looking in the first column (<code>$1</code>). It returns the values from the third column of the matching rows (<code>$3</code>). This is then passed through calc&#8217;s <code>vsum</code> function to calculate the sum.</p>
<p><b>Cumulative points:</b> <code>vsum(@2$2..@+0$2)</code> is the sum of the second column <code>$2</code> from the second row <code>@2</code> to the current row <code>@+0</code>.</p>
<p><b>Level:</b> This uses <code>org-lookup-last</code> to find the last value where the operator function returns true. In this case, testing the level from column <code>$3</code> against each of the values in the <code>levels</code> table&#8217;s column <code>$1</code> while the given level is greater than or equal to the value from <code>levels</code>. When it finds the last matching row, it returns the <code>$2</code> second column from it. <code>;N</code> means treat everything as a number.</p>
<p><code>org-lookup-first</code> is like <code>org-lookup-last</code>, but it returns the first matching row.</p>
<p><b>Adjective:</b> This one works like <b>Level</b> does, but it returns the value from column <code>$3</code> instead. I found that it converted the return values to <code>0</code> if I used <code>;N</code>, so I used <code>;L</code> instead.</p>
</div>
</div>
<div id="outline-container-unnumbered-3" class="outline-3">
<h3 id="unnumbered-3">Passing data to R or other external programs</h3>
<div id="text-unnumbered-3" class="outline-text-3">
<p>Of course, you&#8217;re not limited to things that Emacs can do. I wanted to summarize the data in graphs, so here&#8217;s what I did.</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">#+RESULTS: category_analysis

#+name: category_analysis
#+begin_src R <span class="org-builtin">:var</span> data=accomplishments <span class="org-builtin">:exports</span> both <span class="org-builtin">:results</span> graphics <span class="org-builtin">:file</span> quest_category.png <span class="org-builtin">:height</span> 300
library<span class="org-rainbow-delimiters-depth-1">(</span>plyr<span class="org-rainbow-delimiters-depth-1">)</span>
library<span class="org-rainbow-delimiters-depth-1">(</span>ggplot2<span class="org-rainbow-delimiters-depth-1">)</span>
categories &lt;- ddply<span class="org-rainbow-delimiters-depth-1">(</span>data, c<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-string">"Category"</span><span class="org-rainbow-delimiters-depth-2">)</span>, summarize, Points=sum<span class="org-rainbow-delimiters-depth-2">(</span>XP<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>
cat_sorted &lt;- transform<span class="org-rainbow-delimiters-depth-1">(</span>categories, Category=reorder<span class="org-rainbow-delimiters-depth-2">(</span>Category, Points<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>
plot &lt;- ggplot<span class="org-rainbow-delimiters-depth-1">(</span>data = cat_sorted, aes<span class="org-rainbow-delimiters-depth-2">(</span>x = Category, y = Points<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>
plot &lt;- plot + geom_bar<span class="org-rainbow-delimiters-depth-1">(</span>stat=<span class="org-string">"identity"</span><span class="org-rainbow-delimiters-depth-1">)</span>
plot &lt;- plot + geom_text<span class="org-rainbow-delimiters-depth-1">(</span>aes<span class="org-rainbow-delimiters-depth-2">(</span>label = Points, x = Category, y = Points + 10, hjust=0<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>
plot &lt;- plot + scale_y_continuous<span class="org-rainbow-delimiters-depth-1">(</span>expand=c<span class="org-rainbow-delimiters-depth-2">(</span>0,70<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>
plot &lt;- plot + coord_flip<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-rainbow-delimiters-depth-1">)</span>
print<span class="org-rainbow-delimiters-depth-1">(</span>plot<span class="org-rainbow-delimiters-depth-1">)</span>
#+end_src
</pre>
</div>
<p>I like including source code in published pages for fellow geeks, but having the results come first gives people more context for the source block. So I named the source block using the <code>#+name:</code> directive and defined a <code>#+RESULTS:</code> directive before it. The source block used the <code>:var</code> argument to bring the data in from the <code>accomplishments</code> table. With R blocks, the data becomes available as a data frame that you can then do interesting things with. I used the <code>:file</code> argument to save the output to <code>quest_category.png</code>.</p>
<p>Those are a few ways that you can get data out of Org Mode tables and into Emacs Lisp, other Org Mode tables, or external programs. As I learn more about Org Mode, I find myself using it for more of the things that I used to use Microsoft Excel for &#8211; tracking, analyzing, and even graphing. I found it a little difficult to piece together what I needed to do from the manuals and examples on the Web, so I hope this explanation will help you (and that it&#8217;ll help me when I forget the syntax, as I&#8217;m sure I will). If you come up with something really neat that uses Org Mode tables, tell me what you&#8217;ve figured out!</p>
</div>
</div>

<p>You can <a href="https://sachachua.com/blog/2015/01/getting-data-org-mode-tables/#comment">view 5 comments</a> or <a href="mailto:sacha@sachachua.com?subject=Comment%20on%20https%3A%2F%2Fsachachua.com%2Fblog%2F2015%2F01%2Fgetting-data-org-mode-tables%2F&body=Name%20you%20want%20to%20be%20credited%20by%20(if%20any)%3A%20%0AMessage%3A%20%0ACan%20I%20share%20your%20comment%20so%20other%20people%20can%20learn%20from%20it%3F%20Yes%2FNo%0A">e-mail me at sacha@sachachua.com</a>.</p>]]></description>
		</item><item>
		<title>Getting R and ggplot2 to work in Emacs Org Mode Babel blocks; also, tracking the number of TODOs</title>
		<link>https://sachachua.com/blog/2014/05/getting-r-ggplot2-work-emacs-org-mode-babel-blocks-also-tracking-number-todos/</link>
		<dc:creator><![CDATA[Sacha Chua]]></dc:creator>
		<pubDate>Fri, 02 May 2014 12:00:00 GMT</pubDate>
    <category>emacs</category>
<category>org</category>
<category>quantified</category>
		<guid isPermaLink="false">https://sachachua.com/blog/?p=27211</guid>
		<description><![CDATA[<p>I started tracking the number of tasks I had in Org Mode so that I could find out if my TODO list tended to shrink or grow. It was easy to write a function in Emacs Lisp to count the number of tasks in different states and summarize them in a table.</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span class="org-keyword">defun</span> <span class="org-function-name">sacha/org-count-tasks-by-status</span> ()
  (interactive)
  (<span class="org-keyword">let</span> ((counts (make-hash-table <span class="org-builtin">:test</span> 'equal))
        (today (format-time-string <span class="org-string">"%Y-%m-%d"</span> (current-time)))
        values output)
    (org-map-entries
     (<span class="org-keyword">lambda</span> ()
       (<span class="org-keyword">let*</span> ((status (elt (org-heading-components) 2)))
         (<span class="org-keyword">when</span> status
           (puthash status (1+ (or (gethash status counts) 0)) counts))))
     nil
     'agenda)
    (setq values (mapcar (<span class="org-keyword">lambda</span> (x)
                           (or (gethash x counts) 0))
                         '(<span class="org-string">"DONE"</span> <span class="org-string">"STARTED"</span> <span class="org-string">"TODO"</span> <span class="org-string">"WAITING"</span> <span class="org-string">"DELEGATED"</span> <span class="org-string">"CANCELLED"</span> <span class="org-string">"SOMEDAY"</span>)))
    (setq output
          (concat <span class="org-string">"| "</span> today <span class="org-string">" | "</span>
                  (mapconcat 'number-to-string values <span class="org-string">" | "</span>)
                  <span class="org-string">" | "</span>
                  (number-to-string (apply '+ values))
                  <span class="org-string">" | "</span>
                  (number-to-string
                   (round (/ (* 100.0 (car values)) (apply '+ values))))
                  <span class="org-string">"% |"</span>))
    (<span class="org-keyword">if</span> (called-interactively-p 'any)
        (insert output)
      output)))
(sacha/org-count-tasks-by-status)
</pre>
</div>
<p>I ran this code over several days. Here are my results as of 2014-05-01:</p>
<div style="overflow-x: scroll;">
<table id="burndown" border="2" frame="hsides" rules="groups" cellspacing="0" cellpadding="6">
<colgroup>
<col class="right">
<col class="right">
<col class="right">
<col class="right">
<col class="right">
<col class="right">
<col class="right">
<col class="right">
<col class="right">
<col class="right">
<col class="right">
<col class="right">
<col class="right">
<col class="right">
<col class="left"> </colgroup>
<thead>
<tr>
<th class="right" scope="col">Date</th>
<th class="right" scope="col">DONE</th>
<th class="right" scope="col">START.</th>
<th class="right" scope="col">TODO</th>
<th class="right" scope="col">WAIT.</th>
<th class="right" scope="col">DELEG.</th>
<th class="right" scope="col">CANC.</th>
<th class="right" scope="col">SOMEDAY</th>
<th class="right" scope="col">Total</th>
<th class="right" scope="col">% done</th>
<th class="right" scope="col">+ done</th>
<th class="right" scope="col">+canc.</th>
<th class="right" scope="col">+ total</th>
<th class="right" scope="col">+ t &#8211; d &#8211; c</th>
<th class="left" scope="col">Note</th>
</tr>
</thead>
<tbody>
<tr>
<td class="right">2014-04-16</td>
<td class="right">1104</td>
<td class="right">1</td>
<td class="right">403</td>
<td class="right">3</td>
<td class="right">1</td>
<td class="right">104</td>
<td class="right">35</td>
<td class="right">1651</td>
<td class="right">67%</td>
<td class="right"></td>
<td class="right"></td>
<td class="right"></td>
<td class="right"></td>
<td class="left"></td>
</tr>
<tr>
<td class="right">2014-04-17</td>
<td class="right">1257</td>
<td class="right">0</td>
<td class="right">114</td>
<td class="right">4</td>
<td class="right">1</td>
<td class="right">171</td>
<td class="right">107</td>
<td class="right">1654</td>
<td class="right">76%</td>
<td class="right">153</td>
<td class="right">67</td>
<td class="right">3</td>
<td class="right">-217</td>
<td class="left">Lots of trimming</td>
</tr>
<tr>
<td class="right">2014-04-18</td>
<td class="right">1292</td>
<td class="right">0</td>
<td class="right">74</td>
<td class="right">4</td>
<td class="right">5</td>
<td class="right">183</td>
<td class="right">100</td>
<td class="right">1658</td>
<td class="right">78%</td>
<td class="right">35</td>
<td class="right">12</td>
<td class="right">4</td>
<td class="right">-43</td>
<td class="left">A little bit more trimming</td>
</tr>
<tr>
<td class="right">2014-04-20</td>
<td class="right">1305</td>
<td class="right">0</td>
<td class="right">80</td>
<td class="right">4</td>
<td class="right">5</td>
<td class="right">183</td>
<td class="right">100</td>
<td class="right">1677</td>
<td class="right">78%</td>
<td class="right">13</td>
<td class="right">0</td>
<td class="right">19</td>
<td class="right">6</td>
<td class="left"></td>
</tr>
<tr>
<td class="right">2014-04-21</td>
<td class="right">1311</td>
<td class="right">1</td>
<td class="right">78</td>
<td class="right">4</td>
<td class="right">4</td>
<td class="right">184</td>
<td class="right">99</td>
<td class="right">1681</td>
<td class="right">78%</td>
<td class="right">6</td>
<td class="right">1</td>
<td class="right">4</td>
<td class="right">-3</td>
<td class="left"></td>
</tr>
<tr>
<td class="right">2014-04-22</td>
<td class="right">1313</td>
<td class="right">2</td>
<td class="right">75</td>
<td class="right">4</td>
<td class="right">4</td>
<td class="right">184</td>
<td class="right">99</td>
<td class="right">1681</td>
<td class="right">78%</td>
<td class="right">2</td>
<td class="right">0</td>
<td class="right">0</td>
<td class="right">-2</td>
<td class="left"></td>
</tr>
<tr>
<td class="right">2014-04-23</td>
<td class="right">1369</td>
<td class="right">4</td>
<td class="right">66</td>
<td class="right">4</td>
<td class="right">5</td>
<td class="right">186</td>
<td class="right">101</td>
<td class="right">1735</td>
<td class="right">79%</td>
<td class="right">56</td>
<td class="right">2</td>
<td class="right">54</td>
<td class="right">-4</td>
<td class="left">Added sharing/index.org</td>
</tr>
<tr>
<td class="right">2014-04-24</td>
<td class="right">1371</td>
<td class="right">3</td>
<td class="right">69</td>
<td class="right">4</td>
<td class="right">5</td>
<td class="right">186</td>
<td class="right">101</td>
<td class="right">1739</td>
<td class="right">79%</td>
<td class="right">2</td>
<td class="right">0</td>
<td class="right">4</td>
<td class="right">2</td>
<td class="left"></td>
</tr>
<tr>
<td class="right">2014-04-25</td>
<td class="right">1379</td>
<td class="right">3</td>
<td class="right">60</td>
<td class="right">3</td>
<td class="right">5</td>
<td class="right">189</td>
<td class="right">103</td>
<td class="right">1742</td>
<td class="right">79%</td>
<td class="right">8</td>
<td class="right">3</td>
<td class="right">3</td>
<td class="right">-8</td>
<td class="left"></td>
</tr>
<tr>
<td class="right">2014-04-26</td>
<td class="right">1384</td>
<td class="right">3</td>
<td class="right">65</td>
<td class="right">3</td>
<td class="right">5</td>
<td class="right">192</td>
<td class="right">103</td>
<td class="right">1755</td>
<td class="right">79%</td>
<td class="right">5</td>
<td class="right">3</td>
<td class="right">13</td>
<td class="right">5</td>
<td class="left"></td>
</tr>
<tr>
<td class="right">2014-04-27</td>
<td class="right">1389</td>
<td class="right">2</td>
<td class="right">66</td>
<td class="right">3</td>
<td class="right">5</td>
<td class="right">192</td>
<td class="right">103</td>
<td class="right">1760</td>
<td class="right">79%</td>
<td class="right">5</td>
<td class="right">0</td>
<td class="right">5</td>
<td class="right">0</td>
<td class="left"></td>
</tr>
<tr>
<td class="right">2014-04-28</td>
<td class="right">1396</td>
<td class="right">3</td>
<td class="right">67</td>
<td class="right">3</td>
<td class="right">5</td>
<td class="right">192</td>
<td class="right">103</td>
<td class="right">1769</td>
<td class="right">79%</td>
<td class="right">7</td>
<td class="right">0</td>
<td class="right">9</td>
<td class="right">2</td>
<td class="left"></td>
</tr>
<tr>
<td class="right">2014-04-29</td>
<td class="right">1396</td>
<td class="right">3</td>
<td class="right">67</td>
<td class="right">3</td>
<td class="right">5</td>
<td class="right">192</td>
<td class="right">103</td>
<td class="right">1769</td>
<td class="right">79%</td>
<td class="right">0</td>
<td class="right">0</td>
<td class="right">0</td>
<td class="right">0</td>
<td class="left"></td>
</tr>
<tr>
<td class="right">2014-04-30</td>
<td class="right">1404</td>
<td class="right">4</td>
<td class="right">70</td>
<td class="right">4</td>
<td class="right">5</td>
<td class="right">192</td>
<td class="right">103</td>
<td class="right">1782</td>
<td class="right">79%</td>
<td class="right">8</td>
<td class="right">0</td>
<td class="right">13</td>
<td class="right">5</td>
<td class="left"></td>
</tr>
<tr>
<td class="right">2014-05-01</td>
<td class="right">1413</td>
<td class="right">4</td>
<td class="right">80</td>
<td class="right">3</td>
<td class="right">4</td>
<td class="right">193</td>
<td class="right">103</td>
<td class="right">1800</td>
<td class="right">79%</td>
<td class="right">9</td>
<td class="right">1</td>
<td class="right">18</td>
<td class="right">8</td>
<td class="left"></td>
</tr>
</tbody>
</table>
</div>
<p>Here's the source for that table:</p>
<div class="org-src-container">
<pre class="src src-org"><span class="org-org-meta-line">#+NAME: burndown</span>
<span class="org-org-meta-line">#+RESULTS:</span>
<span class="org-org-table">|       Date | DONE | START. | TODO | WAIT. | DELEG. | CANC. | SOMEDAY | Total | % done | + done | +canc. | + total | + t - d - c | Note                       |</span>
<span class="org-org-table">|&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;+&#45;&#45;&#45;&#45;&#45;&#45;+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;+&#45;&#45;&#45;&#45;&#45;&#45;+&#45;&#45;&#45;&#45;&#45;&#45;-+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;+&#45;&#45;&#45;&#45;&#45;&#45;-+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;-+&#45;&#45;&#45;&#45;&#45;&#45;-+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;-+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;-+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;|</span>
<span class="org-org-table">| 2014-04-16 | 1104 |      1 |  403 |     3 |      1 |   104 |      35 |  1651 |    67% |        |        |         |             |                            |</span>
<span class="org-org-table">| 2014-04-17 | 1257 |      0 |  114 |     4 |      1 |   171 |     107 |  1654 |    76% |    153 |     67 |       3 |        -217 | Lots of trimming           |</span>
<span class="org-org-table">| 2014-04-18 | 1292 |      0 |   74 |     4 |      5 |   183 |     100 |  1658 |    78% |     35 |     12 |       4 |         -43 | A little bit more trimming |</span>
<span class="org-org-table">| 2014-04-20 | 1305 |      0 |   80 |     4 |      5 |   183 |     100 |  1677 |    78% |     13 |      0 |      19 |           6 |                            |</span>
<span class="org-org-table">| 2014-04-21 | 1311 |      1 |   78 |     4 |      4 |   184 |      99 |  1681 |    78% |      6 |      1 |       4 |          -3 |                            |</span>
<span class="org-org-table">| 2014-04-22 | 1313 |      2 |   75 |     4 |      4 |   184 |      99 |  1681 |    78% |      2 |      0 |       0 |          -2 |                            |</span>
<span class="org-org-table">| 2014-04-23 | 1369 |      4 |   66 |     4 |      5 |   186 |     101 |  1735 |    79% |     56 |      2 |      54 |          -4 | Added sharing/index.org    |</span>
<span class="org-org-table">| 2014-04-24 | 1371 |      3 |   69 |     4 |      5 |   186 |     101 |  1739 |    79% |      2 |      0 |       4 |           2 |                            |</span>
<span class="org-org-table">| 2014-04-25 | 1379 |      3 |   60 |     3 |      5 |   189 |     103 |  1742 |    79% |      8 |      3 |       3 |          -8 |                            |</span>
<span class="org-org-table">| 2014-04-26 | 1384 |      3 |   65 |     3 |      5 |   192 |     103 |  1755 |    79% |      5 |      3 |      13 |           5 |                            |</span>
<span class="org-org-table">| 2014-04-27 | 1389 |      2 |   66 |     3 |      5 |   192 |     103 |  1760 |    79% |      5 |      0 |       5 |           0 |                            |</span>
<span class="org-org-table">| 2014-04-28 | 1396 |      3 |   67 |     3 |      5 |   192 |     103 |  1769 |    79% |      7 |      0 |       9 |           2 |                            |</span>
<span class="org-org-table">| 2014-04-29 | 1396 |      3 |   67 |     3 |      5 |   192 |     103 |  1769 |    79% |      0 |      0 |       0 |           0 |                            |</span>
<span class="org-org-table">| 2014-04-30 | 1404 |      4 |   70 |     4 |      5 |   192 |     103 |  1782 |    79% |      8 |      0 |      13 |           5 |                            |</span>
<span class="org-org-table">| 2014-05-01 | 1413 |      4 |   80 |     3 |      4 |   193 |     103 |  1800 |    79% |      9 |      1 |      18 |           8 |                            |</span>
<span class="org-org-meta-line">#+TBLFM: @3$11..@&gt;$11=$2-@-1$2::@3$13..@&gt;$13=$9-@-1$9::@3$14..@&gt;$14=$13-$11-($7-@-1$7)::@3$12..@&gt;$12=$7-@-1$7</span>
</pre>
</div>
<p>I wanted to graph this with Gnuplot, but it turns out that Gnuplot is difficult to integrate with Emacs on Microsoft Windows. I gave up after a half an hour of poking at it, since search results indicated there were long-standing problems with how Gnuplot got input from Emacs. Besides, I'd been meaning to learn more <a href="http://www.r-project.org/">R</a> anyway, and R is more powerful when it comes to statistics and data visualization.</p>
<p>Getting R to work with Org Mode babel blocks in Emacs on Windows was a challenge. Here are some of the things I ran into.</p>
<p>The first step was easy: Add R to the list of languages I could evaluate in a source block (I already had dot and ditaa from previous experiments).</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(org-babel-do-load-languages
 'org-babel-load-languages
 '((dot . t)
   (ditaa . t) 
   (R . t)))
</pre>
</div>
<p>But my code didn't execute at all, even when I was trying something that printed out results instead of drawing images. I got a little lost trying to dig into <code>org-babel-execute:R</code> with <code>edebug</code>, eventually ending up in <code>comint.el</code>. The real solution was even easier. I had incorrectly set <code>inferior-R-program-name</code> to the path of R in my configuration, which made <code>M-x R</code> work but which meant that Emacs was looking in the wrong place for the options to pass to R (which Org Babel relied on). The correct way to do this is to leave <code>inferior-R-program-name</code> with the default value (<code>Rterm</code>) and make sure that my system path included <b>both</b> the <code>bin</code> directory and the <code>bin\x64</code> directory.</p>
<p>Then I had to pick up the basics of R again. It took me a little time to figure out that I needed to parse the columns I pulled in from Org, using <code>strptime</code> to convert the date column and <code>as.numeric</code> to convert the numbers. Eventually, I got it to plot some results with the regular <code>plot</code> command.</p>
<div class="org-src-container">
<pre class="src src-R">dates &lt;- strptime(as.character(data$Date), "%Y-%m-%d")
tasks_done &lt;- as.numeric(data$DONE)
tasks_uncancelled &lt;- as.numeric(data$Total) - as.numeric(data$CANC.)
df &lt;- data.frame(dates, tasks_done, tasks_uncancelled)
plot(x=dates, y=tasks_uncancelled, ylim=c(0,max(tasks_uncancelled)))
lines(x=dates, y=tasks_uncancelled, col="blue", type="o")
lines(x=dates, y=tasks_done, col="green", type="o")
</pre>
</div>
<div class="figure">
<p><a href="https://sachachua.com/blog/wp-content/uploads/2014/04/r-plot.png"><img loading="lazy" class="alignnone size-full wp-image-27219" src="https://sachachua.com/blog/wp-content/uploads/2014/04/r-plot.png" alt="r-plot" width="480" height="480" srcset="https://sachachua.com/blog/wp-content/uploads/2014/04/r-plot.png 480w, https://sachachua.com/blog/wp-content/uploads/2014/04/r-plot-200x200.png 200w" sizes="(max-width: 480px) 100vw, 480px"></a></p>
</div>
<p>I wanted prettier graphs, though. I installed the <code>ggplot2</code> package and started figuring it out. No matter what I did, though, I ended up with a blank white image instead of my graph. If I used <code>M-x R</code> instead of evaluating the src block, the code worked. Weird! Eventually I found out that adding <code>print(...)</code> around my <code>ggplot</code> made it display the image correctly. Yay! Now I had what I wanted.</p>
<div class="org-src-container">
<pre class="src src-R">library(ggplot2)
dates &lt;- strptime(as.character(data$Date), "%Y-%m-%d")
tasks_done &lt;- as.numeric(data$DONE)
tasks_uncancelled &lt;- as.numeric(data$Total) - as.numeric(data$CANC.)
df &lt;- data.frame(dates, tasks_done, tasks_uncancelled)
plot = ggplot(data=df, aes(x=dates, y=tasks_done, ymin=0)) + geom_line(color="#009900") + geom_point() + geom_line(aes(y=tasks_uncancelled), color="blue") + geom_point(aes(y=tasks_uncancelled))
print(plot)
</pre>
</div>
<div class="figure">
<p> <img loading="lazy" class="alignnone size-full wp-image-27220" src="https://sachachua.com/blog/wp-content/uploads/2014/04/r-graph.png" alt="r-graph" width="480" height="480" srcset="https://sachachua.com/blog/wp-content/uploads/2014/04/r-graph.png 480w, https://sachachua.com/blog/wp-content/uploads/2014/04/r-graph-200x200.png 200w" sizes="(max-width: 480px) 100vw, 480px"></p>
</div>
<p>The blue line represents the total number of tasks (except for the cancelled ones), and the green line represents tasks that are done.</p>
<p>Here's something that looks a little more like a <a href="http://en.wikipedia.org/wiki/Burn_down_chart">burn down chart</a>, since it shows just the number of things to be done:</p>
<div class="org-src-container">
<pre class="src src-R">library(ggplot2)
dates &lt;- strptime(as.character(data$Date), "%Y-%m-%d")
tasks_remaining &lt;- as.numeric(data$Total) - as.numeric(data$CANC.) - as.numeric(data$DONE)
df &lt;- data.frame(dates, tasks_remaining)
plot = ggplot(data=df, aes(x=dates, y=tasks_remaining, ymin=0)) + geom_line(color="#009900") + geom_point()
print(plot)
</pre>
</div>
<div class="figure">
<p><a href="https://sachachua.com/blog/wp-content/uploads/2014/04/r-graph-2.png"><img loading="lazy" class="alignnone size-full wp-image-27221" src="https://sachachua.com/blog/wp-content/uploads/2014/04/r-graph-2.png" alt="r-graph-2" width="480" height="480" srcset="https://sachachua.com/blog/wp-content/uploads/2014/04/r-graph-2.png 480w, https://sachachua.com/blog/wp-content/uploads/2014/04/r-graph-2-200x200.png 200w" sizes="(max-width: 480px) 100vw, 480px"></a></p>
</div>
<p>The drastic decline there is me realizing that I had lots of tasks that were no longer relevant, not me being super-productive. =)</p>
<p>As it turns out, I tend to add new tasks at about the rate that I finish them (or slightly more). I think this is okay. It means I'm working on things that have next steps, and next steps, and steps beyond that. If I add more tasks, that gives me more variety to choose from. Besides, I have a lot of repetitive tasks, so those never get marked as DONE over here.</p>
<p>Anyway, cool! Now that I've gotten R to work on my system, you'll probably see it in even more of these blog posts. =D Hooray for Org Babel and R!</p>
<div class="update">
Update 2014-05-09: Stephen suggested <a href="http://blogs.neuwirth.priv.at/software/2012/03/28/r-and-emacs-with-org-mode/">http://blogs.neuwirth.priv.at/software/2012/03/28/r-and-emacs-with-org-mode/</a> for more tips on setting up Org Mode with R and Emacs Speaks Statistics (ESS).</div>

<p>You can <a href="https://sachachua.com/blog/2014/05/getting-r-ggplot2-work-emacs-org-mode-babel-blocks-also-tracking-number-todos/#comment">view 9 comments</a> or <a href="mailto:sacha@sachachua.com?subject=Comment%20on%20https%3A%2F%2Fsachachua.com%2Fblog%2F2014%2F05%2Fgetting-r-ggplot2-work-emacs-org-mode-babel-blocks-also-tracking-number-todos%2F&body=Name%20you%20want%20to%20be%20credited%20by%20(if%20any)%3A%20%0AMessage%3A%20%0ACan%20I%20share%20your%20comment%20so%20other%20people%20can%20learn%20from%20it%3F%20Yes%2FNo%0A">e-mail me at sacha@sachachua.com</a>.</p>]]></description>
		</item>
	</channel>
</rss>