<?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>Hacking is believing</title>
	<atom:link href="http://blog.itoshi.tv/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.itoshi.tv</link>
	<description>@itoshi.tv</description>
	<lastBuildDate>Mon, 22 Mar 2010 05:54:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ggplot2 は The Grammar of Graphics の実装</title>
		<link>http://blog.itoshi.tv/2010/03/theory_of_ggplot2/</link>
		<comments>http://blog.itoshi.tv/2010/03/theory_of_ggplot2/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 07:15:33 +0000</pubDate>
		<dc:creator>dritoshi</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[ggplot2]]></category>

		<guid isPermaLink="false">http://blog.itoshi.tv/?p=88</guid>
		<description><![CDATA[ggplot2 はほかの R パッケージにくらべて不思議な実装になっています。これには理論的な背景がちゃんとあります。そのあたりを勉強しながら tweet したものを集めました。
The grammar of graph [...]]]></description>
			<content:encoded><![CDATA[<p>ggplot2 はほかの R パッケージにくらべて不思議な実装になっています。これには理論的な背景がちゃんとあります。そのあたりを勉強しながら tweet したものを集めました。</p>
<h3>The grammar of graphics と オブジェクト指向グラフシステム</h3>
<ul>
<li><em>14:18</em> ggplot2が不自由なのでちゃんと勉強するわ&#8230; <a href="http://twitter.com/dritoshi/statuses/7893084326">#</a></li>
<li><em>15:13</em> ggplot2 は「グラフはオブジェクト指向で書けるよ」と言った Wilkinson, L.: The Grammar of Graphics の実装なのか。グラフの表現は composite pattern っぽいし、レイヤーを重ねていくところは builder っぽい。 <a href="http://twitter.com/dritoshi/statuses/7894490319">#</a></li>
<li><em>15:18</em> Grammar of Graphics では、Object oriented graph system (OOGS) を提案している。OOGSでは、グラフ作成には specification, assembly, display の3つのステージがある、とする <a href="http://twitter.com/dritoshi/statuses/7894627070">#</a></li>
<li><em>15:23</em> グラフというのは(写真やビデオ)のような自然画像と違い、少ないルールの組み合わせから構成されている。これを指定するのが specification というステップ。 <a href="http://twitter.com/dritoshi/statuses/7894746225">#</a></li>
<li><em>15:26</em> グラフ自体がグラフの組み合わせで構成されている場合もある。グラフのコンポーネント(あるいはレイヤー)を組み立てるステップが assembly <a href="http://twitter.com/dritoshi/statuses/7894803942">#</a></li>
<li><em>15:27</em> グラフを display や画像、ビデオなどに出力するステップが display となる <a href="http://twitter.com/dritoshi/statuses/7894831010">#</a></li>
<li><em>15:29</em> ggplot2 の場合は、assembly のステップが builder pattern のようにオブジェクトを + 演算子で加えていく部分になる。display の部分は単に print() になるんだね。 <a href="http://twitter.com/dritoshi/statuses/7894875007">#</a></li>
</ul>
<h3>グラフを specification するルールとは</h3>
<ul>
<li><em>15:33</em> さて、グラフにおいて、specification すべきルールってなに? って話になる。The grammar of graphics では 6つの statement から構成される、としている。 <a href="http://twitter.com/dritoshi/statuses/7894974459">#</a></li>
<li><em>15:33</em> ここでミーティングの時間になってしまったので続きはあとで&#8230; <a href="http://twitter.com/dritoshi/statuses/7894982063">#</a></li>
<li><em>19:26</em> ミーティングおわた <a href="http://twitter.com/dritoshi/statuses/7899698684">#</a></li>
<li><em>19:36</em> specification の 6つの statement とは data, variable transformation, scale transformation, coordinate system, element, guide のこと <a href="http://twitter.com/dritoshi/statuses/7899884734">#</a></li>
<li><em>19:40</em> data はそのまんま、描画するためのデータのこと。 <a href="http://twitter.com/dritoshi/statuses/7899971590">#</a></li>
<li><em>19:50</em> データをプロットするために bin をとってサマライズしたり、ランクのデータに変換したりする必要があるが、この部分が variable transformation の部分です。 <a href="http://twitter.com/dritoshi/statuses/7900152180">#</a></li>
<li><em>19:50</em> ggplot2 の実装では statistical transformation と呼ばれていて、stat_hoge のアレがそうですね <a href="http://twitter.com/dritoshi/statuses/7900157190">#</a></li>
<li><em>19:52</em> scale transformation は単にログスケールにするとかそういう話 <a href="http://twitter.com/dritoshi/statuses/7900201066">#</a></li>
<li><em>20:06</em> coordinate system もまんまですね、極とかデカルトとかそういうこと <a href="http://twitter.com/dritoshi/statuses/7900470972">#</a></li>
<li><em>20:07</em> element ってのはグラフの種類やみための話で、ggplot2 でいう、geometric object に対応するはず。geom_hoge っていうアレですね <a href="http://twitter.com/dritoshi/statuses/7900503768">#</a></li>
<li><em>20:09</em> guide は軸とかレジェンドとかそういうやつ <a href="http://twitter.com/dritoshi/statuses/7900539184">#</a></li>
<li><em>20:11</em> 以上のように、ggplot2 は The grammar of graphics の実装になっているわけです <a href="http://twitter.com/dritoshi/statuses/7900577149">#</a></li>
<li><em>20:18</em> ggplot2 を使うにあたって、The grammar of graphics 以外に知っておくべきこととして、データの mapping ぐらいかな? aes を使って軸にデータフレームのどのカラムを使うのかを指定する必要があります。 <a href="http://twitter.com/dritoshi/statuses/7900718998">#</a></li>
</ul>
<h3>ggplot2の実装について</h3>
<ul>
<li><em>20:25</em> str(ggplot()) するとオブジェクトの中身がだいたい The grammar of graphics で言っているような感じになっていますね <a href="http://twitter.com/dritoshi/statuses/7900865750">#</a></li>
<li><em>20:29</em> ggplot2 の具体的な話にはいる。qplot がお手軽総称関数で、これでプロットしておくとさっきの6つの statement をいいかんじにしてくれる。よって、The grammar of graphics とか知らなくてもいい。やたー <a href="http://twitter.com/dritoshi/statuses/7900943981">#</a></li>
<li><em>20:31</em> でも、色々細かいことを指定して、かっちょいーグラフを書くには The grammar of graphics を理解して、ggplot() を使わなきゃ。 <a href="http://twitter.com/dritoshi/statuses/7900988520">#</a></li>
<li><em>20:37</em> ggplot() を使うにしても、便利な geom_hoge() と stat_hoge() いうメソッドがあるので完全に一から組み立てる必要はない。GoF の デザパタで言うところの builder pattern っぽい感じですね <a href="http://twitter.com/dritoshi/statuses/7901106324">#</a></li>
<li><em>20:52</em> このあたりの話ってもう誰かブログに書いてたりすんだっけ? &amp;gt; ggplot2 <a href="http://twitter.com/dritoshi/statuses/7901425973">#</a></li>
<li><em>20:59</em> ggplot2 は S3 な Prototype object-based programming で書かれていますね&#8230; <a href="http://twitter.com/dritoshi/statuses/7901578431">#</a></li>
<li><em>21:02</em> Prototype object-based programming はよーわからんのよ、おいら <a href="http://twitter.com/dritoshi/statuses/7901667042">#</a></li>
<li><em>21:15</em> ソースみると、geom_.r というトップレベルのクラスがあってこれには draw method がある。いわゆる template method pattern みたいな感じで、geom-abline.r で draw() が定義されいてるわけか <a href="http://twitter.com/dritoshi/statuses/7901959637">#</a></li>
<li><em>21:20</em> stat_hoge() に関しても stat_.r の calculate() とか calculate_groups() を stat_hoge.r で定義すると。 <a href="http://twitter.com/dritoshi/statuses/7902081202">#</a></li>
<li><em>21:26</em> なんかいろいろなところ作った statement を plot.r のなかで構造体としてまとめる。+ で ggplot オブジェクトを加えていけるのは、plot-construction.r がミソか <a href="http://twitter.com/dritoshi/statuses/7902206110">#</a></li>
<li><em>21:30</em> plot.r で作られた ggplot オブジェクトは最終的に、plot-render.r に渡されて grid.draw {grid} で描画される <a href="http://twitter.com/dritoshi/statuses/7902307727">#</a></li>
<li><em>21:30</em> うん、これで ggplot2 はわかったね、みんな! <a href="http://twitter.com/dritoshi/statuses/7902314138">#</a></li>
<li><em>21:33</em> そして ggplot2 は pure R で実装されているのであった。そりゃ遅いさ。 <a href="http://twitter.com/dritoshi/statuses/7902391324">#</a></li>
<li><em>21:47</em> ggplot2 がゲシュタルト崩壊してきたのでそろそろ自重 <a href="http://twitter.com/dritoshi/statuses/7902718836">#</a></li>
<li><em>21:53</em> @<a href="http://twitter.com/ma_ko">ma_ko</a> いや、プログラムのファイル名が &quot;-&quot; でメソッド名が &quot;_&quot; なのよ <a href="http://twitter.com/dritoshi/statuses/7902867247">#</a></li>
<li><em>21:54</em> 一箇所、たしかにまちがっているところあるな <a href="http://twitter.com/dritoshi/statuses/7902894592">#</a></li>
<li><em>21:56</em> @<a href="http://twitter.com/ma_ko">ma_ko</a>  The grammar of graphics は Google Books でさっき読んだ <a href="http://bit.ly/7RW59X">bit.ly/7RW59X</a> <a href="http://twitter.com/dritoshi/statuses/7902948017">#</a></li>
</ul>
<p>参考書籍:<br />
<a href="http://www.amazon.co.jp/ggplot2-Elegant-Graphics-Data-Analysis/dp/0387981403%3FSubscriptionId%3D198QHWK776EPRCDP9S82%26tag%3Ditoshitv-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0387981403"><img src="http://ecx.images-amazon.com/images/I/311U1iiD9dL._SL160_.jpg" alt="" /></a><br />
<a href="http://www.amazon.co.jp/Grammar-Graphics-Statistics-Computing/dp/0387245448%3FSubscriptionId%3D198QHWK776EPRCDP9S82%26tag%3Ditoshitv-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0387245448"><img src="http://ecx.images-amazon.com/images/I/21FvdiDOULL._SL160_.jpg" alt="" /></a></p>
<p>結局、両方買っちゃいました。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.itoshi.tv/2010/03/theory_of_ggplot2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>R + Rapache + brew + ggplot2 + RSQLite で作る遺伝子発現データベース</title>
		<link>http://blog.itoshi.tv/2010/03/expression_database_on_rapache_brew/</link>
		<comments>http://blog.itoshi.tv/2010/03/expression_database_on_rapache_brew/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 05:16:42 +0000</pubDate>
		<dc:creator>dritoshi</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[Amazon EC2]]></category>
		<category><![CDATA[brew]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[rapache]]></category>
		<category><![CDATA[RSQLite]]></category>

		<guid isPermaLink="false">http://blog.itoshi.tv/?p=74</guid>
		<description><![CDATA[さて、新しいおもちゃを手に入れたら、遺伝子発現データベースを作ってみるというのは定石(主に俺の)なので、作ってみました。
SQLite に発現データをつっこんだデータを RSQLite で取り出して、ggplot2 でプ [...]]]></description>
			<content:encoded><![CDATA[<p>さて、新しいおもちゃを手に入れたら、遺伝子発現データベースを作ってみるというのは定石(主に俺の)なので、作ってみました。</p>
<p>SQLite に発現データをつっこんだデータを RSQLite で取り出して、ggplot2 でプロットし、brew でHTMLを出力しています。</p>
<p>Amazon EC2 上で動作しています。ggplot2 の描写が遅いため、かなりもっさりしていますねー。Amazon EC2 Small Instance は Xeon 1.0-1.2 GHz, 1.7 GB memory 相当らしいです。ggplot2 で画像を on the fly 生成するのは現実的ではなさそうですね。plot() などを使うか、Google Chart API を使うのが現実的かもしれません。また、時間があるときに、plotrix などほかのライブラリと速度の比較をしてみたいです。</p>
<p><a href="http://itoshi.tv/brew/index.r">Simple Gene Expression Database (R + Rapache + Brew + RSQLite + ggplot2 on Amaozn EC2)</a></p>
<p>なんだか、<a href="http://twitter.com/jeffreyhorner/status/8854334810">Brew, Rapache のなかのひとに褒められた</a> &#8230;/// </p>
<p>ちなみに、Amazon EC2 で一日 Ubuntu を動かしていると、<a href="http://skitch.com/dritoshi/nwpn6/amazon-ec2apache2-emacs-r-bioc">このぐらいの料金</a>になりました。<br />
<a href="http://www.amazon.co.jp/ggplot2-Elegant-Graphics-Data-Analysis/dp/0387981403%3FSubscriptionId%3D198QHWK776EPRCDP9S82%26tag%3Ditoshitv-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0387981403"><img src="http://ecx.images-amazon.com/images/I/311U1iiD9dL._SL160_.jpg" alt="" /></a></p>
<p>そのうち github にコードをアップしておきたいと思います。</p>
<p>参考:<br />
<a href="http://itoshi.tv/d/?date=20060506">Ruby on Rails + Gruffを使って、11分で作る遺伝子発現データベース</a><br />
<a href="http://itoshi.tv/d/?date=20081109">Google App EngineとChart APIを使って簡単な遺伝子発現データベースを作ってみた</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.itoshi.tv/2010/03/expression_database_on_rapache_brew/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Amazon EC2へドメインをひっこした</title>
		<link>http://blog.itoshi.tv/2010/03/joyent_to_ec/</link>
		<comments>http://blog.itoshi.tv/2010/03/joyent_to_ec/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 05:04:00 +0000</pubDate>
		<dc:creator>dritoshi</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[private]]></category>
		<category><![CDATA[Amazon EC2]]></category>

		<guid isPermaLink="false">http://blog.itoshi.tv/?p=70</guid>
		<description><![CDATA[Joyent (textdrive) を解約して Amazon EC2 にすべてのコンテンツへ引っ越ししている。
wordpress の設定がだいたいできたので、ドメイン名 itoshi.tv を Amazon EC2  [...]]]></description>
			<content:encoded><![CDATA[<p>Joyent (textdrive) を解約して Amazon EC2 にすべてのコンテンツへ引っ越ししている。</p>
<p>wordpress の設定がだいたいできたので、ドメイン名 itoshi.tv を Amazon EC2 の elastic IP address へ割り当てた。</p>
<p>さらに、wordpress を blog.itoshi.tv でアクセスできるように、A Name の blog も amazon EC2 の elastic IP address に割り当て、apache2 のバーチャルドメイン機能で blog.itoshi.tv からのアクセスを wordpress へ rewrite するように設定した。</p>
<p>Amazon EC2 instance は reboot されると、elastic IP address が動的に変更されるが、ドメインを管理している enom.tv が提供している DNS は Dynamic DNS に対応しているので、DDNSクライアントさえ設定すれば問題ないらしい。</p>
<p>残るタスクは、旧ブログや Hiki などのコンテンツの移動だ。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.itoshi.tv/2010/03/joyent_to_ec/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://blog.itoshi.tv/2010/02/hello-world/</link>
		<comments>http://blog.itoshi.tv/2010/02/hello-world/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 13:14:11 +0000</pubDate>
		<dc:creator>dritoshi</dc:creator>
				<category><![CDATA[private]]></category>
		<category><![CDATA[blog]]></category>

		<guid isPermaLink="false">http://204.236.236.3/blog/?p=1</guid>
		<description><![CDATA[ブログの引越し準備を検討中。試しに Amazon EC2 に Wordpress をインストールしてみている。
テーマいじったり、WP LaTeX plugin とか、SyntaxHighlighter を入れたり。
o [...]]]></description>
			<content:encoded><![CDATA[<p>ブログの引越し準備を検討中。試しに Amazon EC2 に Wordpress をインストールしてみている。<br />
テーマいじったり、WP LaTeX plugin とか、SyntaxHighlighter を入れたり。</p>
<img src='http://s.wordpress.com/latex.php?latex=e%5E%7B%5Ci%20%5Cpi%7D%20%2B%201%20%3D%200&#038;bg=E5E5E5&#038;fg=444444&#038;s=0' alt='e^{\i \pi} + 1 = 0' title='e^{\i \pi} + 1 = 0' class='latex' />
<p>or <img src='http://s.wordpress.com/latex.php?latex=e%5E%7B%5Ci%20%5Cpi%7D%20%2B%201%20%3D%200&#038;bg=E5E5E5&#038;fg=444444&#038;s=0' alt='e^{\i \pi} + 1 = 0' title='e^{\i \pi} + 1 = 0' class='latex' /> syntax.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.itoshi.tv/2010/02/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rapache (mod_r) を使って R で CGI を書くよ。テンプレートエンジンBrew編</title>
		<link>http://blog.itoshi.tv/2010/02/rapache_3_brew/</link>
		<comments>http://blog.itoshi.tv/2010/02/rapache_3_brew/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 08:51:46 +0000</pubDate>
		<dc:creator>dritoshi</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[brew]]></category>
		<category><![CDATA[rapache]]></category>

		<guid isPermaLink="false">http://204.236.236.3/blog/?p=48</guid>
		<description><![CDATA[前回は寧n、いえ、なんでもないです、前回のコードでは、cat() を使ってがんばってHTMLタグを出力していました。これは気持ち悪いです。RのコードとHTMLのコードを分けておかないと、どちらかに変更があった場合に、お互 [...]]]></description>
			<content:encoded><![CDATA[<p>前回は寧n、いえ、なんでもないです、前回のコードでは、cat() を使ってがんばってHTMLタグを出力していました。これは気持ち悪いです。RのコードとHTMLのコードを分けておかないと、どちらかに変更があった場合に、お互いの影響に配慮しながら修正をしなければならなくなります。これは面倒ですね。</p>
<p>こんなときは、テンプレートエンジンを使って、HTMLを作る部分とRのコードをなるべく分けるのが良いですね。R にも Ruby でいうところの erb のようなテンプレートエンジンがあります。今回は、<a href="http://cran.r-project.org/web/packages/brew/index.html">brew</a> というテンプレートフレームワークを使います。</p>
<h3>brewのインストールと設定</h3>
<p>まず brew をインストールします<br />
<code><br />
sudo R<br />
install.pakcages("brew")<br />
q()<br />
</code></p>
<p>次に apache の設定をします。<br />
<code><br />
sudo mkdir  /var/www/brew<br />
sudo jed /etc/apache2/sites-available/default<br />
</code></p>
<p>RHandler に brew を設定します。</p>
<pre class="brush: xml;">
    # brew
        &lt;Directory /var/www/brew&gt;
          #SetHandler r-handler
          SetHandler r-script
          RHandler brew::brew
        &lt;/Directory&gt;
</pre>
<h3>brew で実装し仕直す</h3>
<p>前回のコードを brew を使って書き直してみます。まずメインのコードです。<br />
<code><br />
cd  /var/www/brew<br />
sudo jed index.r<br />
</code></p>
<p>ほぼHTMLですね。プロット部分を別なファイル hist.r に追い出しています。brew() でこれを読み込みます。</p>
<pre class="brush: xml;">
&lt;% setContentType('text/html') %&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;;
&lt;html&gt;
  &lt;head&gt;&lt;title&gt;Test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;% brew('/var/www/brew/hist.r') %&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>プロットを描画するコード hist.r は以下の通り。ほぼ R のコードですね。このコードは画像を作り、最終的にはその画像のパスが入った img タグを作ります。</p>
<pre class="brush: xml;">
&lt;%
randomFileName &lt;- function(prefix, postfix) {
  filename &lt;- paste(
    prefix,
    sprintf(&quot;%08d&quot;, as.integer(runif(1, 0, 10^7))),
    postfix,
    sep=&quot;&quot;
  )
  return(filename)
}

image_dir &lt;- &quot;/var/www/images&quot;
filename  &lt;- randomFileName(&quot;hist_&quot;, &quot;.png&quot;)
filepath  &lt;- paste(image_dir, filename, sep=&quot;/&quot;)

data &lt;- rnorm(1000)
png(filepath)
hist(data)
dev.off()
%&gt;
&lt;img src=&quot;/images/&lt;%=filename%&gt;&quot;&gt;
</pre>
<p>http://localhost/brew/index.r にアクセスすると前回と同じようにヒストグラムが表示されます。リロードするとグラフが変わるのを確認してみてください。</p>
<p>これで「HTMLを組む」コードと「計算しプロットを作図する」コードがだいぶ分かれて見通しが良くなりました。次回こそなにか役に立つものを作ってみたいと思います。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.itoshi.tv/2010/02/rapache_3_brew/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rapacheを使ってRでCGI を書くよ。グラフ画像出力編</title>
		<link>http://blog.itoshi.tv/2010/02/rapache_2/</link>
		<comments>http://blog.itoshi.tv/2010/02/rapache_2/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 06:05:30 +0000</pubDate>
		<dc:creator>dritoshi</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[brew]]></category>
		<category><![CDATA[rapache]]></category>

		<guid isPermaLink="false">http://204.236.236.3/blog/?p=17</guid>
		<description><![CDATA[前回のつづきです。前回は rapache の設定と簡単なCGIを作ってみました。今度は画像を出力して表示する CGI を作ります。内容は、乱数を適当に作ってヒストグラムを作画するCGIです。画像はランダムなファイル名で保 [...]]]></description>
			<content:encoded><![CDATA[<p>前回のつづきです。前回は rapache の設定と簡単なCGIを作ってみました。今度は画像を出力して表示する CGI を作ります。内容は、乱数を適当に作ってヒストグラムを作画するCGIです。画像はランダムなファイル名で保存します。</p>
<pre class="brush: xml;">
# Functions
header &lt;- function(contenttype) {
  setContentType(contenttype)
}
html.head &lt;- function(content) {
  cat(&quot;&lt;head&gt;\n&quot;)
  cat(content)
  cat(&quot;&lt;/head&gt;\n&quot;)
}
html.body &lt;- function(content) {
  cat(&quot;&lt;body&gt;\n&quot;)
  cat(content)
  cat(&quot;&lt;/body&gt;&lt;/html&gt;\n&quot;)
}
randomFileName &lt;- function(prefix, postfix) {
  filename &lt;- paste(
    prefix,
    sprintf(&quot;%08d&quot;, as.integer(runif(1, 0, 10^7))),
    postfix,
    sep=&quot;&quot;
  )
  return(filename)
}                                                                               

# Main
image_dir &lt;- &quot;/var/www/images&quot;
filename  &lt;- randomFileName(&quot;hist_&quot;, &quot;.png&quot;)
filepath  &lt;- paste(image_dir, filename, sep=&quot;/&quot;)                                

data &lt;- rnorm(1000)                                                             

# output html
header(&quot;text/html&quot;)
html.head(&quot;&lt;title&gt;Test&lt;/title&gt;&quot;)                                                

png(filepath)
hist(data)
dev.off()                                                                       

html.body(
  paste(
    &quot;&lt;img src=&quot;/images/&quot;,
    filename,
    '&quot;&gt;',
    sep=&quot;&quot;
  )
)
</pre>
<p>リロードすると画像が変わるのがわかると思います。次回はもっと役に立つものを作ってみます。</p>
<p><object id="scPlayer" width="815" height="624"><param name="movie" value="http://content.screencast.com/users/dritoshi/folders/Default/media/0a798fbd-6165-4640-bbff-cb13d80e91c0/bootstrap.swf"></param><param name="quality" value="high"></param><param name="bgcolor" value="#FFFFFF"></param><param name="flashVars" value="thumb=http://content.screencast.com/users/dritoshi/folders/Default/media/0a798fbd-6165-4640-bbff-cb13d80e91c0/FirstFrame.jpg&#038;containerwidth=815&#038;containerheight=624&#038;content=http://content.screencast.com/users/dritoshi/folders/Default/media/0a798fbd-6165-4640-bbff-cb13d80e91c0/2010-02-08_0022.swf"></param><param name="allowFullScreen" value="true"></param><param name="scale" value="showall"></param><param name="allowScriptAccess" value="always"></param><param name="base" value="http://content.screencast.com/users/dritoshi/folders/Default/media/0a798fbd-6165-4640-bbff-cb13d80e91c0/"></param>  <embed src="http://content.screencast.com/users/dritoshi/folders/Default/media/0a798fbd-6165-4640-bbff-cb13d80e91c0/bootstrap.swf" quality="high" bgcolor="#FFFFFF" width="815" height="624" type="application/x-shockwave-flash" allowScriptAccess="always" flashVars="thumb=http://content.screencast.com/users/dritoshi/folders/Default/media/0a798fbd-6165-4640-bbff-cb13d80e91c0/FirstFrame.jpg&#038;containerwidth=815&#038;containerheight=624&#038;content=http://content.screencast.com/users/dritoshi/folders/Default/media/0a798fbd-6165-4640-bbff-cb13d80e91c0/2010-02-08_0022.swf" allowFullScreen="true" base="http://content.screencast.com/users/dritoshi/folders/Default/media/0a798fbd-6165-4640-bbff-cb13d80e91c0/" scale="showall"></embed></object></p>
<p>寧々さんかわいいです。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.itoshi.tv/2010/02/rapache_2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rapache (mod_r) を使って R で CGI を書くよ。Hello, World編</title>
		<link>http://blog.itoshi.tv/2010/02/rapache_1/</link>
		<comments>http://blog.itoshi.tv/2010/02/rapache_1/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 03:47:08 +0000</pubDate>
		<dc:creator>dritoshi</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[brew]]></category>
		<category><![CDATA[rapache]]></category>

		<guid isPermaLink="false">http://204.236.236.3/blog/?p=6</guid>
		<description><![CDATA[みなさん、こんばとらー。仕事で遺伝子発現データベースを作っているのですが、表示するグラフが Pixiv が保有する全イラスト数である490万枚 (2009/06発表) を越えてしまいました、やりました!(なにが?  所内 [...]]]></description>
			<content:encoded><![CDATA[<p>みなさん、こんばとらー。仕事で遺伝子発現データベースを作っているのですが、表示するグラフが Pixiv が保有する全イラスト数である490万枚 (2009/06発表) を越えてしまいました、やりました!(なにが?  所内の内部向けなので、PVが数百ですがw むこうは2億PV。</p>
<p>画像を吐くために Sun Grid Engine で PC クラスタをぶんまわしたりしています、頭わるいですね。数値データをグラフにするだけなので、WWWサーバで on the fly でグラフを描画すべきなのですが、サーバが Celeron, メモリ 800MB というという有様だったので躊躇していました。今回は新しいサーバに引っ越したので、R で on the fly 描画にトライしたいと思います。</p>
<p>まずは mod_R, いわゆる <a href=http://biostat.mc.vanderbilt.edu/rapache/">rapache</a>というまぎらわしい名前のパッケージをセットアップし動作するところまでやってみましょう。環境は Ubuntu 9.10 です。</p>
<h3>rapache のインストール</h3>
<p>まず、R を apt の source list に入れる。これで apt で最新の R を入れ放題。<br />
<code><br />
deb http://cran.r-project.org/bin/linux/ubuntu intrepid/<br />
</code></p>
<p>次に rapache と関連パッケージを入れます。<br />
<code><br />
 apt-get install r-base-dev apache2-mpm-prefork apache2-prefork-dev<br />
 wget http://biostat.mc.vanderbilt.edu/rapache/files/rapache-latest.tar.gz<br />
 rapachedir=`tar tzf rapache-latest.tar.gz   | head -1`<br />
 tar xzvf rapache-latest.tar.gz<br />
 cd $rapachedir<br />
 ./configure<br />
 make<br />
 sudo make install<br />
</code></p>
<h3>rapacheの設定</h3>
<p>それでは Apache の設定をしましょう。最近は Ruby on Rails を使うので Apache の設定はひさしぶりです。<br />
<code><br />
 sudo emacs -nw /etc/apache2/mods-available/R.load<br />
</code></p>
<pre class="brush: xml;">
 LoadModule R_module /usr/lib/apache2/modules/mod_R.so
</pre>
<p><code><br />
 sudo emacs -nw /etc/apache2/mods-available/R.conf<br />
</code></p>
<pre class="brush: xml;">
&lt;IfModule mod_R.c&gt;
  ROutputErrors
  &lt;Location /RApacheInfo&gt;
    SetHandler r-info
  &lt;/Location&gt;
&lt;/IfModule&gt;
</pre>
<p><code><br />
 cd /etc/apache2/mods-enabled<br />
 sudo sudo ln -s ../mods-available/R.load<br />
 sudo sudo ln -s ../mods-available/R.conf<br />
</code><br />
Apache を再起動する。</p>
<p><code><br />
 sudo /etc/init.d/apache2 restart<br />
 * Restarting web server apache2<br />
 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for  ServerName<br />
 ... waiting apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1  for ServerName<br />
   ...done.<br />
</code><br />
これで apache の設定は完了。</p>
<p>http://localhost/RApacheInfo/</p>
<p>にアクセスするとサーバーの状態が表示されます。</p>
<p><img src="http://img.skitch.com/20100207-d368ma1y38tus8pdneghd9t1c6.jpg"></p>
<h3>RでCGIを書いてみる</h3>
<p>さっそく、Hello, world しましょう。</p>
<p>まずは Apache の設定をします。<br />
<code><br />
 sudo jed /etc/apache2/sites-available/default<br />
</code></p>
<pre class="brush: xml;">
    # rapache
        &lt;Directory /var/www/rapachetest&gt;
          SetHandler r-handler
          RFileHandler /var/www/R/test.R
        &lt;/Directory&gt;
</pre>
<p><code><br />
 sudo mkdir /etc/www/R<br />
 sudo mkdir /etc/www/rapachetest<br />
 cd /etc/www/R<br />
</code></p>
<p>Hello, world のコード書きます。</p>
<p><code><br />
 sudo jed test.R<br />
</code></p>
<pre class="brush: xml;">
 # Functions
 header &lt;- function() {
   setContentType(&quot;text/html&quot;)
 }
 html.head &lt;- function(content) {
   cat(&quot;&lt;head&gt;\n&quot;)
   cat(content)
   cat(&quot;&lt;/head&gt;\n&quot;)
 }
 html.body &lt;- function(content) {
   cat(&quot;&lt;body&gt;\n&quot;)
   cat(content)
   cat(&quot;&lt;/body&gt;&lt;/html&gt;\n&quot;)
 } 

 # Main
 header()
 html.head(&quot;&lt;title&gt;Test&lt;/title&gt;&quot;)
 html.body(&quot;Hello, world!\n&quot;)
</pre>
<p>以下にアクセスします。</p>
<p>http://localhost/rapachetest/</p>
<p><img src="http://img.skitch.com/20100207-842sc1fa5upns1wrgrfcuj6n5b.jpg"></p>
<p>これで終了です。今回は rapache の設定と hello, world をやってみました。次回はグラフを画像を吐いてみます。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.itoshi.tv/2010/02/rapache_1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
