<?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>R2S &#187; R</title> <atom:link href="http://www.road2stat.com/cn/category/r_language/feed" rel="self" type="application/rss+xml" /><link>http://www.road2stat.com/cn</link> <description>江湖一散人</description> <lastBuildDate>Thu, 26 Jan 2012 08:18:36 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>R连接PostgreSQL</title><link>http://www.road2stat.com/cn/r_language/rpostgresql.html</link> <comments>http://www.road2stat.com/cn/r_language/rpostgresql.html#comments</comments> <pubDate>Mon, 03 Oct 2011 19:30:46 +0000</pubDate> <dc:creator>Xiao Nan</dc:creator> <category><![CDATA[R]]></category> <category><![CDATA[DBI]]></category> <category><![CDATA[PostgreSQL]]></category> <category><![CDATA[数据库]]></category><guid isPermaLink="false">http://www.road2stat.com/cn/?p=949</guid> <description><![CDATA[最近一直在玩DICE三年前的神作《镜之边缘》, 顺便重温了一下一年前的《黑手党II》, 玩得简直是没有什么时间上来灌水了. 游戏之余偶然接触了一个PostgreSQL数据库, 简单记录一下. R连接数据库有几套方案, 其实基本上就是DBI/ODBC/JDBC. 不过话说ODBC和JDBC神马的真是弱爆了. JDBC方案中那个鬼魂一般的依赖rJava, 真的是很难安装. 其实也有一种可能是AUR上的JDK打包得不好, 没能hold住R CMD javareconf的标准. 前些日子安装RWeka时专门研究过rJava的安装脚本, 卡在编译简单JNI程序这句一直不成功, 手动修改各种配置文件无果, 于是果断放弃 ... 吐槽完毕, 顺便拉回正题. 话说这PostgreSQL是伯克利出品, 基于以自己名字命名的协议发布(霸气又外露了), 有着众多优良特性. 其实我总想把这名字读成Post·GRE·SQL, 不难译为"旧GRE的结构化查询语言", 只是, 您这名字让新泽西乡下那儿一心一意革新GRE考试的大爷们情何以堪哪 ... 当然, 最后结果都是万把个英文单词乱入. 同时, 我们选择的RPostgreSQL包是GSoC 08'项目, 有R社区Dirk Eddelbuettel等众牛参与, 正牌DBI系. 另外, Bioconductor项目的用户也贡献了同为DBI系的RdbiPgSQL/pgUtils包. 酌情使用. &#8230; <a href="http://www.road2stat.com/cn/r_language/rpostgresql.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>最近一直在玩DICE三年前的神作《镜之边缘》, 顺便重温了一下一年前的《黑手党II》, 玩得简直是没有什么时间上来灌水了. 游戏之余偶然接触了一个PostgreSQL数据库, 简单记录一下.<br /> <a href="http://www.road2stat.com/cn/wp-content/attachments/2011/10/mirrorsedge.jpg"><img src="http://www.road2stat.com/cn/wp-content/attachments/2011/10/mirrorsedge.jpg" alt="mirrorsedge" title="mirrorsedge" width="500" height="336" class="aligncenter size-full wp-image-950" /></a></p><p>R连接数据库有几套方案, 其实基本上就是DBI/ODBC/JDBC. 不过话说ODBC和JDBC神马的真是弱爆了. JDBC方案中那个鬼魂一般的依赖rJava, 真的是很难安装. 其实也有一种可能是AUR上的JDK打包得不好, 没能hold住R CMD javareconf的标准. 前些日子安装RWeka时专门研究过rJava的安装脚本, 卡在编译简单JNI程序这句一直不成功, 手动修改各种配置文件无果, 于是果断放弃 ...<br /> <span id="more-949"></span><br /> 吐槽完毕, 顺便拉回正题. 话说这PostgreSQL是伯克利出品, 基于以自己名字命名的协议发布(霸气又外露了), 有着<a href="http://obmem.info/?p=493" target="_blank">众多优良特性</a>. 其实我总想把这名字读成Post·GRE·SQL, 不难译为"旧GRE的结构化查询语言", 只是, 您这名字让新泽西乡下那儿一心一意革新GRE考试的大爷们情何以堪哪 ... 当然, 最后结果都是万把个英文单词乱入. 同时, 我们选择的RPostgreSQL包是GSoC 08'项目, 有R社区Dirk Eddelbuettel等众牛参与, 正牌DBI系.</p><p>另外, Bioconductor项目的用户也贡献了同为DBI系的RdbiPgSQL/pgUtils包. 酌情使用.</p><pre># For Arch Linux
# 安装PostgreSQL Server
$ sudo pacman -S postgresql
# 启动daemon
$ sudo /etc/rc.d/postgresql start
# 创建用户
$ sudo createuser -s -U postgres
# psql是个好工具
$ psql -l
# 创建数据库
$ createdb newdatabase
# 导入pg_dump文件
$ psql -d newdatabase -U postgres -f dump.sql</pre><p>导入数据的速度还可以, 700多M的pg_dump文件导进去只消几分钟, 空间占用也涨到了2G+. PostgreSQL的使用问题在<a href="https://wiki.archlinux.org/index.php/PostgreSQL" target="_blank">Arch Wiki</a>上有详细说明. 日常管理方面, 力荐一个自由的, 同样遵守PostgreSQL协议的跨平台GUI工具pgAdmin:</p><pre>sudo pacman -S pgadmin3</pre><p>R部分非常容易:</p><pre>require(RPostgreSQL)
# 读入driver
drv = dbDriver("PostgreSQL")
# 填写连接信息
con = dbConnect(drv, dbname = "数据库名",
user = "用户名", password = "密码", port = 5432)
# 查询语句
rs = dbSendQuery(con, statement = "SQL语句")
# 收割结果
df = fetch(rs, n = -1)
# 其实可以直接执行查询返回结果
dbGetQuery(con, "SQL语句")
# 断开连接
dbDisconnect(con)
# 释放资源
dbUnloadDriver(drv)</pre><p>写数据时, 可能会遇到数据类型字符编码等等RP问题. 更多细节还是关注一下文档吧, 话说牛人们往往都是懒得写vignette的. 只有函数reference manual可读的用户你是真真的伤不起啊.</p> ]]></content:encoded> <wfw:commentRss>http://www.road2stat.com/cn/r_language/rpostgresql.html/feed</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Rapid Prototyping R based Web Applications with Rook: Visualizing CVE-2011-0611 samples with Self-Organizing Maps</title><link>http://www.road2stat.com/cn/r_language/rook.html</link> <comments>http://www.road2stat.com/cn/r_language/rook.html#comments</comments> <pubDate>Sun, 04 Sep 2011 09:47:44 +0000</pubDate> <dc:creator>Xiao Nan</dc:creator> <category><![CDATA[R]]></category> <category><![CDATA[Binary]]></category> <category><![CDATA[Executable]]></category> <category><![CDATA[Rook]]></category> <category><![CDATA[Self-Organizing Maps]]></category> <category><![CDATA[SOM]]></category> <category><![CDATA[Visualization]]></category> <category><![CDATA[web application]]></category><guid isPermaLink="false">http://www.road2stat.com/cn/?p=899</guid> <description><![CDATA[Inspired by Ruby's Rack Project, Jeffery Horner released his R package "Rook" [1] earlier this year. After trying to get several Rook applications running, I realized that Rook had avoided some certain disadvantages of Rapache. Rook is much more flexible &#8230; <a href="http://www.road2stat.com/cn/r_language/rook.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>Inspired by Ruby's <a href="http://rack.rubyforge.org/" target="_blank">Rack</a> Project, Jeffery Horner released his R package "Rook" [1] earlier this year. After trying to get several Rook applications running, I realized that Rook had avoided some certain disadvantages of Rapache. Rook is much more flexible and easier to learn.</p><p>Theoretically speaking, once the proper plugin is done, your app could then be deployed under any web servers such as apache/lighthttpd/nginx, etc. Another significant advantage of Rook is, it's friendly for debugging. As Rook takes Rhttpd as the default server, you could preview your app on-the-fly, without any complicated deploying process.</p><p>Here's a test app, which implements the creative binary file visualization method described in the VizSec and Virol papers [2] and [3]. We choose to visualize the CVE-2011-0611 samples, which were retrieved from [4]. By using the Rook::File application simultaneously, we could serve static (png) files.</p><p>Load required pkgs:</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p899code4'); return false;">View Code</a> RSPLUS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8994"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p899code4"><pre class="rsplus" style="font-family:monospace;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/require.html"><span style="color: #0000FF; font-weight: bold;">require</span></a><span style="color: #080;">&#40;</span>Rook<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/require.html"><span style="color: #0000FF; font-weight: bold;">require</span></a><span style="color: #080;">&#40;</span>digest<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/require.html"><span style="color: #0000FF; font-weight: bold;">require</span></a><span style="color: #080;">&#40;</span>kohonen<span style="color: #080;">&#41;</span></pre></td></tr></table></div><p>Write a Rook app:</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.road2stat.com/cn/wp-content/plugins/wp-codebox/wp-codebox.php?p=899&amp;download=visbin.R">visbin.R</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8995"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code" id="p899code5"><pre class="rsplus" style="font-family:monospace;">newapp <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>env<span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
    req <span style="color: #080;">=</span> Rook<span style="color: #080;">::</span><span style="">Request</span>$new<span style="color: #080;">&#40;</span>env<span style="color: #080;">&#41;</span>
    res <span style="color: #080;">=</span> Rook<span style="color: #080;">::</span><span style="">Response</span>$new<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
    res$write<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Choose a Binary file to Train:<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>
    res$write<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'&lt;form method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>
    res$write<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'&lt;input type=&quot;file&quot; name=&quot;data&quot;&gt;<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>
    res$write<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'xdim:<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>
    res$write<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'&lt;form method=&quot;POST&quot;&gt;<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>
    res$write<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'&lt;input type=&quot;text&quot; name=&quot;xdim&quot; value=&quot;12&quot;&gt;<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>
    res$write<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'ydim:<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>
    res$write<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'&lt;form method=&quot;POST&quot;&gt;<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>
    res$write<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'&lt;input type=&quot;text&quot; name=&quot;ydim&quot; value=&quot;25&quot;&gt;<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>
    res$write<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'ncolors:<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>
    res$write<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'&lt;form method=&quot;POST&quot;&gt;<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>
    res$write<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'&lt;input type=&quot;text&quot; name=&quot;ncolors&quot; value=&quot;8&quot;&gt;<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>
    res$write<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'&lt;input type=&quot;submit&quot; name=&quot;Go!&quot;&gt;<span style="color: #000099; font-weight: bold;">\n</span>&lt;/form&gt;<span style="color: #000099; font-weight: bold;">\n</span>&lt;br&gt;'</span><span style="color: #080;">&#41;</span>
&nbsp;
    myNormalize <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a> <span style="color: #080;">&#40;</span>target<span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span>target <span style="color: #080;">-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a><span style="color: #080;">&#40;</span>target<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/max.html"><span style="color: #0000FF; font-weight: bold;">max</span></a><span style="color: #080;">&#40;</span>target<span style="color: #080;">&#41;</span> <span style="color: #080;">-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a><span style="color: #080;">&#40;</span>target<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
    <span style="color: #080;">&#125;</span>
&nbsp;
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a> <span style="color: #080;">&#40;</span><span style="color: #080;">!</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/is.null.html"><span style="color: #0000FF; font-weight: bold;">is.<span style="">null</span></span></a><span style="color: #080;">&#40;</span>req$POST<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> req$POST<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">&quot;data&quot;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>
    hash <span style="color: #080;">=</span> digest<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$tempfile, algo <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;md5&quot;</span>, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/file.html"><span style="color: #0000FF; font-weight: bold;">file</span></a> <span style="color: #080;">=</span> TRUE<span style="color: #080;">&#41;</span>
    destFile <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/file.html"><span style="color: #0000FF; font-weight: bold;">file</span></a><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$tempfile, <span style="color: #ff0000;">&quot;rb&quot;</span><span style="color: #080;">&#41;</span>
    k <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/floor.html"><span style="color: #0000FF; font-weight: bold;">floor</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/file.info.html"><span style="color: #0000FF; font-weight: bold;">file.<span style="">info</span></span></a><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span>$tempfile<span style="color: #080;">&#41;</span>$size<span style="color: #080;">/</span><span style="color: #ff0000;">16</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">-</span> <span style="color: #ff0000;">2</span>
    doneFile <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/readBin.html"><span style="color: #0000FF; font-weight: bold;">readBin</span></a><span style="color: #080;">&#40;</span>con <span style="color: #080;">=</span> destFile, what <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;raw&quot;</span>, n <span style="color: #080;">=</span> <span style="color: #ff0000;">2</span> <span style="color: #080;">*</span> <span style="color: #ff0000;">8</span> <span style="color: #080;">*</span> k<span style="color: #080;">&#41;</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/close.html"><span style="color: #0000FF; font-weight: bold;">close</span></a><span style="color: #080;">&#40;</span>destFile<span style="color: #080;">&#41;</span>
    tmpFile0 <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rbind.html"><span style="color: #0000FF; font-weight: bold;">rbind</span></a><span style="color: #080;">&#40;</span>doneFile<span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/seq.html"><span style="color: #0000FF; font-weight: bold;">seq</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, <span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span> <span style="color: #080;">*</span> <span style="color: #ff0000;">8</span> <span style="color: #080;">*</span> k<span style="color: #080;">&#41;</span> <span style="color: #080;">-</span> <span style="color: #ff0000;">1</span>, <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>, doneFile<span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/seq.html"><span style="color: #0000FF; font-weight: bold;">seq</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span>, <span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span> <span style="color: #080;">*</span> <span style="color: #ff0000;">8</span> <span style="color: #080;">*</span> k<span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
    tmpFile1 <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>tmpFile0<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span>, <span style="color: #080;">&#93;</span>, tmpFile0<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span>, <span style="color: #080;">&#93;</span>, sep <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span>
    initMat <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="color: #0000FF; font-weight: bold;">matrix</span></a><span style="color: #080;">&#40;</span>strtoi<span style="color: #080;">&#40;</span>tmpFile1, 16L<span style="color: #080;">&#41;</span>, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="color: #0000FF; font-weight: bold;">ncol</span></a> <span style="color: #080;">=</span> <span style="color: #ff0000;">8</span>, byrow <span style="color: #080;">=</span> TRUE<span style="color: #080;">&#41;</span>
    normMat <span style="color: #080;">=</span> myNormalize<span style="color: #080;">&#40;</span>initMat<span style="color: #080;">&#41;</span>
    trainedSOM <span style="color: #080;">=</span> kohonen<span style="color: #080;">::</span><span style="">som</span><span style="color: #080;">&#40;</span>normMat, <a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/grid.html"><span style="color: #0000FF; font-weight: bold;">grid</span></a> <span style="color: #080;">=</span> somgrid<span style="color: #080;">&#40;</span>xdim <span style="color: #080;">=</span> req$POST<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">&quot;xdim&quot;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>, ydim <span style="color: #080;">=</span> req$POST<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">&quot;ydim&quot;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>, <span style="color: #ff0000;">&quot;hexagonal&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/summary.lm.html"><span style="color: #0000FF; font-weight: bold;">png</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;/tmp/&quot;</span>, hash, <span style="color: #ff0000;">&quot;.png&quot;</span>, sep <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/plot.html"><span style="color: #0000FF; font-weight: bold;">plot</span></a><span style="color: #080;">&#40;</span>trainedSOM, type <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;dist.neighbours&quot;</span>, palette.<span style="">name</span> <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/summary.lm.html"><span style="color: #0000FF; font-weight: bold;">rainbow</span></a>, ncolors <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>req$POST<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">&quot;ncolors&quot;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>, main <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/summary.lm.html"><span style="color: #0000FF; font-weight: bold;">dev.<span style="">off</span></span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
    res$write<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;&lt;img src='&quot;</span>, s$full_url<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;pic&quot;</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot;/&quot;</span>, hash, <span style="color: #ff0000;">&quot;.png'&quot;</span>, <span style="color: #ff0000;">&quot; /&gt;&quot;</span>, sep <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
    <span style="color: #080;">&#125;</span>
    res$finish<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span></pre></td></tr></table></div><p>Initialize/Run the app:</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p899code6'); return false;">View Code</a> RSPLUS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8996"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p899code6"><pre class="rsplus" style="font-family:monospace;">s <span style="color: #080;">=</span> Rhttpd$new<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
s$add<span style="color: #080;">&#40;</span>app <span style="color: #080;">=</span> newapp, name <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;visbin&quot;</span><span style="color: #080;">&#41;</span>
s$add<span style="color: #080;">&#40;</span>app <span style="color: #080;">=</span> File$new<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;/tmp&quot;</span><span style="color: #080;">&#41;</span>, name <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;pic&quot;</span><span style="color: #080;">&#41;</span>
s$start<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
s$browse<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;visbin&quot;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div><p>Firstly the app hashes the uploaded files then trains SOM models. As the training result differs each time, we may train more times to get the better one.</p><p>We use the U-Matrix to visualize the Self-Organizing Maps, The U-Matrix value of a particular unit is the average distance between the unit and its closest neighbors, then color was used to represent the value. Actually, the number of the color palette is critical, too much or too little may interfere the detection of potential cluster patterns.</p><p>There exists much more methods for dimensional reduction and visualization with R packages, you may refer to the R News (R Journal) paper [5].</p><p><a href="http://www.road2stat.com/cn/wp-content/attachments/2011/09/visbin.jpg"><img src="http://www.road2stat.com/cn/wp-content/attachments/2011/09/visbin.jpg" alt="visbin" title="visbin" width="445" height="623" class="aligncenter size-full wp-image-901" /></a></p><p>It clearly shows that a cluster pattern appears in the lower right corner. It's reasonable to suspect the file was injected with some data that shouldn't be there.</p><p>The paper says it got bad results when visualizing macro viruses (embedded in Microsoft Office files). Actually, the CVE-2011-0611 sample are doc/xls files, but they are not macro viruses. They're hosts injected with harmful Adobe swf files. From this point of view, they're just like the infected executable files. So theory still applies.</p><p>A detail is, after uploading, the <code>data$tempfile</code> has a different MD5 with the original file, it gains extra hex 0D 0A (seems a new line) in the end. I don't quite understand how this happens. As we had deleted the last two lines of the file to form a proper matrix, the training data is not identical with the binary sample. Nothing influences for this case.</p><p>In summary, Rook connects the 3000+ available R package and web application development, just 40 lines of code were used to achieve a not-so-simple goal, it's really amazing.</p><h1>References</h1><p>[1] <a href="http://cran.r-project.org/web/packages/Rook/index.html" target="_blank">Rook - a web server interface for R</a>.<br /> [2] Visualizing Windows Executable Viruses Using Self-Organizing Maps, VizSec, 2004.<br /> [3] Non-signature Based Virus Detection, Journal in Computer Virology, 2:163–186, 2006.<br /> [4] Contagio Malware Dump. <a href="http://contagiodump.blogspot.com/2011/04/apr-8-cve-2011-0611-flash-player-zero.html" target="_blank">Apr. 8 CVE-2011-0611 Flash Player Zero day - SWF in DOC/ XLS - Disentangling Industrial Policy</a>.<br /> [5] Dimensional Reduction for Data Mapping, R News, Vol. 3/3, 2003.</p> ]]></content:encoded> <wfw:commentRss>http://www.road2stat.com/cn/r_language/rook.html/feed</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>结合豆瓣基础API学习XML包</title><link>http://www.road2stat.com/cn/r_language/rxml.html</link> <comments>http://www.road2stat.com/cn/r_language/rxml.html#comments</comments> <pubDate>Wed, 24 Aug 2011 06:37:00 +0000</pubDate> <dc:creator>Xiao Nan</dc:creator> <category><![CDATA[R]]></category> <category><![CDATA[API]]></category> <category><![CDATA[OAuth]]></category> <category><![CDATA[R语言]]></category> <category><![CDATA[XML]]></category> <category><![CDATA[笔记]]></category> <category><![CDATA[豆瓣]]></category><guid isPermaLink="false">http://www.road2stat.com/cn/?p=878</guid> <description><![CDATA[很久以前在R-Forge上注册过一个RDouban项目, 想用豆瓣提供的API做点好玩的事情. 可惜后来只写了个开头, 感兴趣的童鞋可以无条件认领. 在这里结合豆瓣的基础API, 非常简略地写一下用XML包读数据的基本问题. 1 XPath 花十分钟学习XPath语法. 熟练后可使用Firebug等调试工具直接提取. 此外, 要特别注意XML命名空间问题. (感谢yixuan提醒) 2 Douban API 花n分钟阅读"豆瓣API参考手册". 用户的评论、收藏、广播、豆邮等交互功能往往需要先进行OAuth认证, 建议阅读RFC5849以充分理解OAuth协议. 这块目前也有ROAuth包可以实现, 不过与读数据没什么关系, 此略. 3 Code 读取豆瓣电影/音乐/读书条目的API基本通用, 仅有部分字段细节的区别. 读数据时要注意缺失值和异常值的处理. 由于编码问题, 中文需要使用iconv()函数进行转码. 这里以读取一部电影的三个典型字段为例. 《僵尸肖恩》, IMDB编号tt0365748. require(XML) url = "http://api.douban.com/movie/subject/imdb/tt0365748" movieItemInfo = xmlTreeParse(url, useInternal &#8230; <a href="http://www.road2stat.com/cn/r_language/rxml.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>很久以前在R-Forge上注册过一个<a href="http://rdouban.r-forge.r-project.org/" target="_blank">RDouban项目</a>, 想用豆瓣提供的API做点好玩的事情. 可惜后来只写了个开头, 感兴趣的童鞋可以无条件认领. 在这里结合豆瓣的基础API, 非常简略地写一下用XML包读数据的基本问题.</p><h1>1 XPath</h1><p>花十分钟学习<a href="http://www.w3school.com.cn/xpath/index.asp" target="_blank">XPath</a>语法.<br /> 熟练后可使用Firebug等调试工具直接提取. 此外, 要特别注意<a href="http://www.w3school.com.cn/xml/xml_namespaces.asp" target="_blank">XML命名空间</a>问题. (感谢yixuan提醒)</p><h1>2 Douban API</h1><p>花n分钟阅读"<a href="http://www.douban.com/service/apidoc/reference/" target="_blank">豆瓣API参考手册</a>".<br /> 用户的评论、收藏、广播、豆邮等交互功能往往需要先进行OAuth认证, 建议阅读<a href="http://tools.ietf.org/html/rfc5849" target="_blank">RFC5849</a>以充分理解OAuth协议. 这块目前也有ROAuth包可以实现, 不过与读数据没什么关系, 此略.</p><p><span id="more-878"></span></p><h1>3 Code</h1><p>读取豆瓣电影/音乐/读书条目的API基本通用, 仅有部分字段细节的区别. 读数据时要注意缺失值和异常值的处理. 由于编码问题, 中文需要使用<code>iconv()</code>函数进行转码. 这里以读取一部电影的三个典型字段为例.</p><p>《僵尸肖恩》, IMDB编号tt0365748.</p><p><code>require(XML)<br /> url = "http://api.douban.com/movie/subject/imdb/tt0365748"<br /> movieItemInfo = xmlTreeParse(url, useInternal = TRUE)</code></p><p>名称:<br /> <code>iconv(xmlValue(getNodeSet(doc = movieItemInfo,<br /> path = "//db:attribute[@name='title']",<br /> namespaces = c("db" = "http://www.douban.com/xmlns/"))[[1]]),<br /> from = 'UTF-8', to = '')<br /> # [1] "Shaun of the Dead"</code></p><p>主演:<br /> <code>iconv(sapply(getNodeSet(doc = movieItemInfo,<br /> path = "//db:attribute[@name='cast']",<br /> namespaces = c("db" = "http://www.douban.com/xmlns/")), xmlValue),<br /> from = 'UTF-8', to = '')<br /> #  [1] "西蒙·佩吉"         "尼克·弗罗斯特"     "比尔·奈伊"<br /> #  [4] "凯特·阿什菲尔德"   "彼得·塞拉菲诺威茨" "迪兰·莫兰"<br /> #  [7] "露茜·戴维斯"       "尼古拉·坎宁安"     "基尔·米尔斯"<br /> # [10] "马特·杰恩斯"       "加文·弗格森"       "霍顿·朱庇特"<br /> # [13] "蒂姆·巴格雷"       "阿尔温德·多希"     "拉菲·斯波"<br /> # [16] "索奈尔·达卓" </code></p><p>标签名:<br /> <code>iconv(xpathApply(movieItemInfo,<br /> path = "//db:tag", xmlGetAttr, "name",<br /> namespaces = c("db" = "http://www.douban.com/xmlns/")),<br /> from = 'UTF-8', to = '')<br /> # [1] "黑色幽默"  "英国"      "喜剧"      "僵尸"      "英国电影"  "cult"<br /> # [7] "SimonPegg" "恐怖" </code></p><p>以上, 我们针对XML文档中不同形式的结构, 组合使用了<code>xmlValue()</code>, <code>getNodeSet()</code>, <code>xpathApply()</code>, <code>xmlGetAttr()</code>, <code>sapply()</code>几个函数. 这些, 基本就是XML包和豆瓣API的初阶. 要实现POST数据等等进阶功能, 不妨深入研读XML包的冗长文档.</p> ]]></content:encoded> <wfw:commentRss>http://www.road2stat.com/cn/r_language/rxml.html/feed</wfw:commentRss> <slash:comments>10</slash:comments> </item> <item><title>使用Google Perftools和kcachegrind深入剖析R程序性能瓶颈</title><link>http://www.road2stat.com/cn/r_language/rprofiling.html</link> <comments>http://www.road2stat.com/cn/r_language/rprofiling.html#comments</comments> <pubDate>Fri, 22 Jul 2011 22:20:38 +0000</pubDate> <dc:creator>Xiao Nan</dc:creator> <category><![CDATA[R]]></category> <category><![CDATA[Google]]></category> <category><![CDATA[kcachegrind]]></category> <category><![CDATA[Perftools]]></category> <category><![CDATA[profiling]]></category> <category><![CDATA[R语言]]></category> <category><![CDATA[valgrind]]></category> <category><![CDATA[剖析]]></category> <category><![CDATA[性能]]></category><guid isPermaLink="false">http://www.road2stat.com/cn/?p=816</guid> <description><![CDATA[“颐和园在我家北面，假如没有北这个方向的话，我就只好向南走，越过南极和北极，行程四万余公里到达那里。” ——《革命时期的爱情》 1 剖析与Profiling 这里的"剖析"对应的单词是"profiling", 中文似乎没有词语能精准地表达出原词的内涵, 与"shell"的情况有点类似, 所以就不去刻意翻译了. 由于没有学过软件工程, 不吝先贴一段wiki上profiling的定义[1]: In software engineering, program profiling, software profiling or simply profiling, a form of dynamic program analysis (as opposed to static code analysis), is the investigation of a program's behavior using &#8230; <a href="http://www.road2stat.com/cn/r_language/rprofiling.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<blockquote><p> “颐和园在我家北面，假如没有北这个方向的话，我就只好向南走，越过南极和北极，行程四万余公里到达那里。”</p><p style="text-align: right;" >——《革命时期的爱情》</p></blockquote><h1>1 剖析与Profiling</h1><p>这里的"剖析"对应的单词是"profiling", 中文似乎没有词语能精准地表达出原词的内涵, 与"shell"的情况有点类似, 所以就不去刻意翻译了. 由于没有学过软件工程, 不吝先贴一段wiki上profiling的定义[1]:</p><blockquote><p>In software engineering, program profiling, software profiling or simply profiling, a form of dynamic program analysis (as opposed to static code analysis), is the investigation of a program's behavior using information gathered as the program executes. The usual purpose of this analysis is to determine which sections of a program to optimize - to increase its overall speed, decrease its memory requirement or sometimes both.</p></blockquote><p>要提高R程序的运行速度, 不仅仅需要剽悍的机器(单核高频, 多核并行, GPU)和高效的代码(向量化, 混合编程), 寻找程序中的性能瓶颈并进行有针对的优化也是很重要的, "找到北这个方向", 就是profiling的意义所在.</p><p>R中其实自带了几个最简单的profiling工具, 我们或多或少都接触过:</p><ul><li><code>base::system.time()</code> —— 简陋的计时秒表</li><li><code>utils::Rprof()</code> —— 对CPU的简易profile工具</li><li><code>utils::Rprofmem()</code> —— 对内存的简易profile工具</li></ul><p>关于这些函数的使用, 可以参看Ross Ihaka的说明[2]. 与此同时, CRAN上<a href="http://cran.r-project.org/web/packages/profr/" target="_blank">profr</a>(Hadley Wickham)和<a href="http://cran.r-project.org/web/packages/proftools/" target="_blank">proftools</a>(Luke Tierney)两个微型包均提供了可视化<code>Rprof()</code>函数输出结果的能力. 但是, 这类简单的profiling只将程序拆解了到了单个R运算的层次, 没有提供更深一层, 即profiling compiled code的功能. R原生支持这个特性, 但需要在编译时对默认选项进行简单的修改[4].</p><h1>2 安装Google Perftools</h1><p>翻看Dirk Eddelbuettel牛在useR2010上的RhpcTutorial[3], 其中提到了Google员工开发的Google Perftools可以profiling compiled code. 试了一下, 感觉是个不错的工具, 配合kcachegrind, 还可以将结果可视化.</p><p>测试环境: Arch Linux x86_64</p><p>AUR上已有一位大人在维护google-perftools(源代码安装):</p><pre>sudo yaourt -S google-perftools</pre><p>Fedora:</p><pre>sudo yum install google-perftools</pre><p>Ubuntu:</p><pre>sudo apt-get install google-perftools</pre><p>不过Ubuntu/Fedora仓库中的二进制包可能比较陈旧了, 无妨直接自行编译最新版本:</p><pre>svn checkout http://google-perftools.googlecode.com/svn/trunk/</pre><p>在Arch x86_64下, google-perftools的默认安装路径为</p><pre>/usr/bin</pre><p>库文件libprofiler.so位于</p><pre>/usr/lib</pre><p>Google Perftools工具集中除了名为pprof的CPU profiler以外, 还提供了堆内存泄漏检测/使用情况统计等工具. 这里我们只关注pprof就可以了: 它通过CPU中断采样的方式统计每个函数被采样的次数, 占总采样次数的百分比, 调用的子函数的被采样次数等等(可以说"剖析"在此处还是比较恰当的). 最后通过这些信息寻找程序的(CPU)性能瓶颈.</p><h1>3 使用Google Perftools</h1><p>要和R一起使用, pprof有两种可行的运行方式, 第一种比较硬朗: 在编译选项中直接加入对libprofiler.so的引用, R在运行时就会自动加载libprofiler库:</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p816code17'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81617"><td class="code" id="p816code17"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>cran.r-project.org<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>base<span style="color: #000000; font-weight: bold;">/</span>R-<span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">/</span>R-2.13.1.tar.gz
<span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xf</span> R-2.13.1.tar.gz
<span style="color: #7a0874; font-weight: bold;">cd</span> R-2.13.1
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MAIN_CFLAGS</span>=<span style="color: #ff0000;">&quot;-pg&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MAIN_FFLAGS</span>=<span style="color: #ff0000;">&quot;-pg&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MAIN_LDFLAGS</span>=<span style="color: #ff0000;">&quot;-pg&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LDFLAGS</span>=<span style="color: #ff0000;">&quot;-lprofiler&quot;</span>
<span style="color: #666666; font-style: italic;"># 也可显式指定路径:</span>
<span style="color: #666666; font-style: italic;"># export LDFLAGS=&quot;-L/usr/lib -lprofiler&quot;</span>
.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--enable-R-shlib</span></pre></td></tr></table></div><p>参数 -pg 打开R的profiling支持, 设定LDFLAGS用以连接libprofiler库.</p><p>configure完成:</p><pre>R is now configured for x86_64-unknown-linux-gnu

  Source directory:          .
  Installation directory:    /usr/local

  C compiler:                gcc -std=gnu99  -g -O2
  Fortran 77 compiler:       gfortran  -g -O2

  C++ compiler:              g++  -g -O2
  Fortran 90/95 compiler:    gfortran -g -O2
  Obj-C compiler:	      

  Interfaces supported:      X11
  External libraries:        readline, ICU, lzma
  Additional capabilities:   PNG, JPEG, TIFF, NLS, cairo
  Options enabled:           shared R library, shared BLAS, R profiling, Java

  Recommended packages:      yes</pre><p>结果无误, 开始编译安装:</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p816code18'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81618"><td class="code" id="p816code18"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></td></tr></table></div><p>这样, 连接了libprofiler.so的R编译成功.</p><p>第二种使用pprof的方式则相对委婉: 使用时动态预加载库文件就可以了, Dirk大人的RhpcTutorial中已经给出说明, 此略.</p><p>选取《Wringting R Extension》 3.2节中给出的一个例子进行测试:</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.road2stat.com/cn/wp-content/plugins/wp-codebox/wp-codebox.php?p=816&amp;download=profiling.R">profiling.R</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81619"><td class="code" id="p816code19"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">#!/usr/local/lib64/R/bin/Rscript</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/suppressMessages.html"><span style="color: #0000FF; font-weight: bold;">suppressMessages</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>MASS<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/suppressMessages.html"><span style="color: #0000FF; font-weight: bold;">suppressMessages</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>boot<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
storm.<span style="">fm</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">nls</span><span style="color: #080;">&#40;</span>Time ~ b<span style="color: #080;">*</span>Viscosity<span style="color: #080;">/</span><span style="color: #080;">&#40;</span>Wt <span style="color: #080;">-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#41;</span>, stormer, 
                <span style="color: #0000FF; font-weight: bold;">start</span> <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>b<span style="color: #080;">=</span><span style="color: #ff0000;">29.401</span>, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">2.2183</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
st <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/cbind.html"><span style="color: #0000FF; font-weight: bold;">cbind</span></a><span style="color: #080;">&#40;</span>stormer, fit<span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">fitted</span><span style="color: #080;">&#40;</span>storm.<span style="">fm</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
storm.<span style="">bf</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>rs, i<span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
    st$Time <span style="color: #080;">&lt;-</span>  st$fit <span style="color: #080;">+</span> rs<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span>
    tmp <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">nls</span><span style="color: #080;">&#40;</span>Time ~ <span style="color: #080;">&#40;</span>b <span style="color: #080;">*</span> Viscosity<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #080;">&#40;</span>Wt <span style="color: #080;">-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#41;</span>, st, <span style="color: #0000FF; font-weight: bold;">start</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">coef</span><span style="color: #080;">&#40;</span>storm.<span style="">fm</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
    tmp$m$getAllPars<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
rs <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/scale.html"><span style="color: #0000FF; font-weight: bold;">scale</span></a><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">resid</span><span style="color: #080;">&#40;</span>storm.<span style="">fm</span><span style="color: #080;">&#41;</span>, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/scale.html"><span style="color: #0000FF; font-weight: bold;">scale</span></a> <span style="color: #080;">=</span> FALSE<span style="color: #080;">&#41;</span>
storm.<span style="">boot</span> <span style="color: #080;">&lt;-</span> boot<span style="color: #080;">&#40;</span>rs, storm.<span style="">bf</span>, R <span style="color: #080;">=</span> <span style="color: #ff0000;">500</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div><p>将profiling结果记录到文件:</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p816code20'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81620"><td class="code" id="p816code20"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">755</span> profiling.R
<span style="color: #007800;">CPUPROFILE</span>=rprof.out .<span style="color: #000000; font-weight: bold;">/</span>profiling.R</pre></td></tr></table></div><p>这里在写R文件时用了一点点技巧, 使得它能够支持类Unix系统的Shebang特性而直接执行, 参考[7], [8].</p><p>执行完毕后, 我们即可使用pprof来分析输出的结果文件(一个二进制文件！)了. pprof可以将此文件解析成你想要的各种可读的形式. 其参数如下:</p><pre>pprof --option [ --focus=< regexp > ] [ --ignore=< regexp > ]
                      [--line or addresses or functions] 可执行文件路径 结果文件路径
</pre><p>方括号为可选项目, <code>< regexp ></code>为正则表达式.</p><p>具体的选项分为几组. 其中输出格式的基本可选项为:</p><pre>text, callgrind, gv, evince, web, symbols, dot, ps, pdf, svg, gif, raw, list=< regexp >, disasm=< regexp >.</pre><p><code>text</code> 表示字符统计输出形式, 其它均对应各自的图形格式;<br /> <code>list=< regexp ></code> 表示输出匹配正则表达式的函数的源代码;<br /> <code>diasm=< regexp ></code> 表示输出匹配正则表达式的函数的反汇编代码.</p><p>其他比较重要的参数:</p><p><code>--focus=< regexp ></code> 表示只统计函数名匹配正则表达式的函数的采样;<br /> <code>--ignore=< regexp ></code> 表示不统计函数名匹配正则表达式的函数的采样;<br /> <code>[--line or addresses or functions]</code> 表示生成的统计是基于代码行, 指令地址还是函数的, 默认是函数.</p><p>这里仅输出文字型结果:</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p816code21'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81621"><td class="code" id="p816code21"><pre class="bash" style="font-family:monospace;">pprof <span style="color: #660033;">--cum</span> <span style="color: #660033;">--text</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>R<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>Rscript rprof.out <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">less</span></pre></td></tr></table></div><p>结果中的前15位:</p><pre>
Total: 254 samples
       2   0.8%   0.8%      213  83.9% Rf_applyClosure
      24   9.4%  10.2%      213  83.9% Rf_eval
       0   0.0%  10.2%      213  83.9% do_begin
       0   0.0%  10.2%      212  83.5% do_set
       0   0.0%  10.2%      206  81.1% do_internal
       0   0.0%  10.2%      148  58.3% do_lapply
       7   2.8%  13.0%      123  48.4% Rf_evalList
       0   0.0%  13.0%      107  42.1% Rf_ReplIteration
       0   0.0%  13.0%      104  40.9% R_ReplConsole
       0   0.0%  13.0%      102  40.2% Rf_usemethod
       0   0.0%  13.0%      100  39.4% run_Rmainloop
       0   0.0%  13.0%       96  37.8% main
       0   0.0%  13.0%       92  36.2% __libc_start_main
       0   0.0%  13.0%       85  33.5% do_usemethod
       1   0.4%  13.4%       85  33.5% forcePromise
</pre><p>输出结果中, 每行对应着一个函数的统计:</p><ul><li>第1, 2列是该函数的本地采样(不包括被该函数调用的函数中的采样次数)次数和比例;</li><li>第3列是该函数本地采样次数占当前所有已统计函数的采样次数之和的比例;</li><li>第4, 5列是该函数的累计采样次数(包括其调用的函数中的采样次数)和比例.</li></ul><p>如果你的系统中安装了gnu-gv或evince, 即可直接即刻显示一幅无码清晰大图(ps/pdf):</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p816code22'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81622"><td class="code" id="p816code22"><pre class="bash" style="font-family:monospace;">pprof <span style="color: #660033;">--gv</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>R<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>Rscript rprof.out
pprof <span style="color: #660033;">--evince</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>R<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>Rscript rprof.out</pre></td></tr></table></div><p><a href="http://www.road2stat.com/cn/wp-content/attachments/2011/07/rprof.gif"><img src="http://www.road2stat.com/cn/wp-content/attachments/2011/07/rprof-300x182.gif" alt="rprof" title="rprof" width="300" height="182" class="aligncenter size-medium wp-image-817" /></a></p><p>其他几个比较常用的选项可能是</p><p>生成PDF:</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p816code23'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81623"><td class="code" id="p816code23"><pre class="bash" style="font-family:monospace;">pprof <span style="color: #660033;">--pdf</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>R<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>Rscript rprof.out <span style="color: #000000; font-weight: bold;">&gt;</span> rprof.pdf</pre></td></tr></table></div><p>生成SVG:</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p816code24'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81624"><td class="code" id="p816code24"><pre class="bash" style="font-family:monospace;">pprof <span style="color: #660033;">--svg</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>R<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>Rscript rprof.out <span style="color: #000000; font-weight: bold;">&gt;</span> rprof.svg</pre></td></tr></table></div><p>生成GraphViz所支持的dot格式:</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p816code25'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81625"><td class="code" id="p816code25"><pre class="bash" style="font-family:monospace;">pprof <span style="color: #660033;">--dot</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>R<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>Rscript rprof.out <span style="color: #000000; font-weight: bold;">&gt;</span> rprof.dot</pre></td></tr></table></div><p>当然, 要想读懂图中的内容, 从而针对某些部分进行优化, 还需要对R的底层比较熟悉才行: 最起码要了解涉及到的C函数的具体功能.</p><h1>4 配合kcachegrind可视化profile结果</h1><p>pprof可将输出转化为强大的Valgrind工具集中的组件Callgrind可采用的格式, 配合KCachegrind这个图形前端, 即可对结果进行简单的可视化, 能够交互哦亲:</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p816code26'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81626"><td class="code" id="p816code26"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># For Arch Linux</span>
<span style="color: #666666; font-style: italic;"># sudo pacman -S kdesdk-kcachegrind</span>
pprof <span style="color: #660033;">--callgrind</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>lib64<span style="color: #000000; font-weight: bold;">/</span>R<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>Rscript rprof.out <span style="color: #000000; font-weight: bold;">&gt;</span> rprof.callgrind
kcachegrind rprof.callgrind</pre></td></tr></table></div><p><a href="http://www.road2stat.com/cn/wp-content/attachments/2011/07/kcachegrind.jpg"><img src="http://www.road2stat.com/cn/wp-content/attachments/2011/07/kcachegrind-300x198.jpg" alt="kcachegrind" title="kcachegrind" width="300" height="198" class="aligncenter size-medium wp-image-818" /></a></p><p>其实看上去KCachegrid就是做了一个最普通的树可视化, 所以理论上我们其实可以用角度各异的无数种手段展示profiling结果: 就是画一棵树嘛. 不过KCachegrind中可以与图形交互, 进一步的分析很方便, 大家可以自己进一步体验.</p><h1>5 其他工具(sprof和oprofile)</h1><p>Writing R Extensions[6]提到另外两个可供Linuxer选择的工具: sprof和oprofile, 我没有实验, 感兴趣的同学不妨实践一下.</p><h1>6 参考</h1><p>[1] <a href="http://en.wikipedia.org/wiki/Profiling_(computer_programming)" target="_blank">Wikipedia - Profiling (computer programming)</a>.<br /> [2] Ross Ihaka. <a href="http://www.stat.auckland.ac.nz/~ihaka/downloads/Taupo.pdf" target="_blank">Writing Efficient Programs in R (and Beyond)</a>.<br /> [3] Dirk Eddelbuettel. <a href="http://dirk.eddelbuettel.com/papers/useR2010hpcTutorial.pdf" target="_blank">Introduction to High-Performance Computing with R</a>. pp. 29-35<br /> [4] R Installation and Administration. Version 2.13.0 (2011-04-13) Appendix B.1, B.7.<br /> [5] 冯文龙. <a href="http://www.ibm.com/developerworks/cn/linux/l-cn-googleperf/index.html" target="_blank">使用google-perftools剖析程序性能瓶颈</a>.<br /> [6] Writing R Extensions. Version 2.13.0 (2011-04-13) pp. 69-71.<br /> [7] <a href="http://zh.wikipedia.org/wiki/Shebang" target="_blank">Wikipedia - Shebang</a>.<br /> [8] <a href="http://stackoverflow.com/questions/3128122/shebang-line-not-working-in-r-script" target="_blank">shebang line not working in R script</a>.</p> ]]></content:encoded> <wfw:commentRss>http://www.road2stat.com/cn/r_language/rprofiling.html/feed</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>给力的Rcpp和不给力的RcppArmadillo</title><link>http://www.road2stat.com/cn/r_language/rcpp.html</link> <comments>http://www.road2stat.com/cn/r_language/rcpp.html#comments</comments> <pubDate>Sat, 21 May 2011 18:02:48 +0000</pubDate> <dc:creator>Xiao Nan</dc:creator> <category><![CDATA[R]]></category> <category><![CDATA[icpc]]></category> <category><![CDATA[Rcpp]]></category> <category><![CDATA[rcppArmadillo]]></category><guid isPermaLink="false">http://www.road2stat.com/cn/?p=728</guid> <description><![CDATA[在炎热与抑郁的夏天, 继续博客上的灌水大业才是正经事. xfocus水区有名曰: 灌水王潮 我一直幻想着有朝一日R能够同时拥有简单的语法和C的效率, 直到今天认真看了一下Rcpp, 不懂PROTECT UNPROTECT的孩子终于伤得起了 ... 不得不承认这发了JSS的软件包还是有一定独到之处的: 谁叫人家是SCI呢, 哈哈哈哈哈哈哈. 好吧自行吐槽其实我out了 ... 关于Rcpp, 不周山上早有此文. Rcpp的作者在文档里说在Revolution R下没测试过可能需要修改. 我用icpc替代g++倒是能编译成功, 除了中间闪过几个warning. 安装之前先建立符号链接 sudo ln -s /opt/intel/composerxe-2011.3.174/bin/ia32/icpc /bin/icpc 否则会找不到icpc. 1 Rcpp + inline 如果不是写正式的包, Rcpp + inline让一切浮云都浮云了. 最简单的例子, 来自 [1]: ?View Code &#8230; <a href="http://www.road2stat.com/cn/r_language/rcpp.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>在炎热与抑郁的夏天, 继续博客上的灌水大业才是正经事. xfocus水区有名曰:</p><blockquote><p><em>灌水王潮</em></p></blockquote><p>我一直幻想着有朝一日R能够同时拥有简单的语法和C的效率, 直到今天认真看了一下Rcpp, 不懂PROTECT UNPROTECT的孩子终于伤得起了 ... 不得不承认这发了JSS的软件包还是有一定独到之处的: 谁叫人家是SCI呢, 哈哈哈哈哈哈哈. 好吧自行吐槽其实我out了 ... 关于Rcpp, 不周山上早有<a href="http://www.wentrue.net/blog/?p=1157">此文</a>.</p><p>Rcpp的作者在文档里说在Revolution R下没测试过可能需要修改. 我用icpc替代g++倒是能编译成功, 除了中间闪过几个warning.</p><p>安装之前先建立符号链接</p><blockquote><p>sudo ln -s /opt/intel/composerxe-2011.3.174/bin/ia32/icpc /bin/icpc</p></blockquote><p>否则会找不到icpc.</p><h2>1 Rcpp + inline</h2><p>如果不是写正式的包, Rcpp + inline让一切浮云都浮云了. 最简单的例子, 来自 [1]:</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p728code29'); return false;">View Code</a> RSPLUS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p72829"><td class="code" id="p728code29"><pre class="rsplus" style="font-family:monospace;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/require.html"><span style="color: #0000FF; font-weight: bold;">require</span></a><span style="color: #080;">&#40;</span>Rcpp<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/require.html"><span style="color: #0000FF; font-weight: bold;">require</span></a><span style="color: #080;">&#40;</span>inline<span style="color: #080;">&#41;</span>
&nbsp;
src <span style="color: #080;">=</span> <span style="color: #ff0000;">'
Rcpp::NumericVector xa(a);
Rcpp::NumericVector xb(b);
int n_xa = xa.size();
int n_xb = xb.size();
&nbsp;
Rcpp::NumericVector xab(n_xa + n_xb - 1);
&nbsp;
for (int i = 0; i &lt; n_xa; i++)
  for (int j = 0; j &lt; n_xb; j++)
    xab[i + j] += xa[i] * xb[j];
&nbsp;
return xab;
'</span>
&nbsp;
fun <span style="color: #080;">=</span> cxxfunction<span style="color: #080;">&#40;</span>
       <a href="http://astrostatistics.psu.edu/su07/R/html/base/html/Log.html"><span style="color: #0000FF; font-weight: bold;">signature</span></a><span style="color: #080;">&#40;</span>a <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;numeric&quot;</span>, b <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;numeric&quot;</span><span style="color: #080;">&#41;</span>, 
       src, plugin <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;Rcpp&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
fun<span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">3</span>, <span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;"># [1] 1 4 10 16 17 12</span></pre></td></tr></table></div><p>以上在Intel系编译器出来的R/Rcpp下通过.</p><h2>2 Rcpp + RcppArmadillo + inline</h2><p>这Armadillo是又一个线性代数库.</p><p>话说之前我一直以为这词是穿山甲的意思, 因为有个经典壳就叫Armadillo, 然后我就被Inception了. 今天一查这词的真正意思是"犰狳" ...</p><p>Armadillo在网站上说自己支持MKL和ACML. 但是用icpc编译的时候蹦了n+10086个warning, 编到最后竟然就</p><blockquote><p><em>Error in dyn.load(file, DLLpath = DLLpath, ...) :<br /> 无法载入共享目标对象‘/home/jimmy/R/i686-pc-linux-gnu-library/2.13/RcppArmadillo/libs/RcppArmadillo.so’：:<br /> /home/jimmy/R/i686-pc-linux-gnu-library/2.13/RcppArmadillo/libs/RcppArmadillo.so: undefined symbol: _ZN4arma12arma_version5patchE</em></p></blockquote><p>了! 编译都通不过的孩子, 你就伤得起么 ... 《编程序的小女孩》有云:</p><blockquote><p><em>她刚把头伸出去，想看的仔细一些，程序crash了，大显示器不见了。她坐在那儿，眼前的破显示器上一行刺眼的segment fault。</em></p></blockquote><p>此处太过曲折, 篇幅所限略去不表. 依然是最简单的例子, 来自 [2]:</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p728code30'); return false;">View Code</a> RSPLUS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p72830"><td class="code" id="p728code30"><pre class="rsplus" style="font-family:monospace;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/require.html"><span style="color: #0000FF; font-weight: bold;">require</span></a><span style="color: #080;">&#40;</span>Rcpp<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/require.html"><span style="color: #0000FF; font-weight: bold;">require</span></a><span style="color: #080;">&#40;</span>RcppArmadillo<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/require.html"><span style="color: #0000FF; font-weight: bold;">require</span></a><span style="color: #080;">&#40;</span>inline<span style="color: #080;">&#41;</span>
&nbsp;
src <span style="color: #080;">=</span> <span style="color: #ff0000;">'
arma::colvec x = Rcpp::as&lt;arma::colvec&gt; (x_);
arma::mat Y = Rcpp::as&lt;arma::mat&gt; (Y_ );
arma::colvec z = Rcpp::as&lt;arma::colvec&gt; (z_);
double result = arma::as_scalar(arma::trans(x) * arma::inv(Y) * z);
return Rcpp::wrap(result);
'</span>
&nbsp;
fx <span style="color: #080;">=</span> cxxfunction<span style="color: #080;">&#40;</span>
      <a href="http://astrostatistics.psu.edu/su07/R/html/base/html/Log.html"><span style="color: #0000FF; font-weight: bold;">signature</span></a><span style="color: #080;">&#40;</span>x_ <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;numeric&quot;</span>, Y_ <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;matrix&quot;</span>, z_ <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;numeric&quot;</span><span style="color: #080;">&#41;</span>,
      src, plugin <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;RcppArmadillo&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
fx<span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">4</span>, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/diag.html"><span style="color: #0000FF; font-weight: bold;">diag</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;"># [1] 30</span></pre></td></tr></table></div><h2>3 无题</h2><p>给自己翻了个Rcpp-quickref [3], 没加颜色. 原文档里的高亮风格就不吐槽了, 同一段code snippet中serif mono各种混用难道很酷么 ...</p><p><a href='http://www.road2stat.com/cn/wp-content/attachments/2011/05/Rcpp-quickref-cn.pdf'>Rcpp-quickref-cn.pdf</a></p><p>感觉作者挺不小心的, 括号前后的空格写得相当随意, 也许是多人共同蹂躏文档的结果 ... 有时间要研究一下RcppGSL.</p><h2>4 Ref</h2><p>[1] Rcpp: Seanless R and C++ Integration<br /> [2] Frequently Asked Questions about Rcpp<br /> [3] Rcpp Quick Reference Guide</p><h2>5 无题又见无题</h2><p>某天我突然冒出来一个十分惊悚的想法, 那就是去读物理, 而且方向必须是理论物理 ... 然后就被这白日梦吓醒了 ... 要知道我在大学里唯一学过一个学期的物理, 内容是刚体机械波热力学什么的, 去上过的课绝对没超过一半, 而且最后成绩是C ... 这 ... 难道是Crysis2 OST听多了么 ... 还是万有引力之虹看多了 ...</p><p>给我一万支秃笔, 让我仿黄鲁直, 削长枪大戟。<br /> 给我十千斗浊酒, 让我学李太白, 灭沧海横流。</p> ]]></content:encoded> <wfw:commentRss>http://www.road2stat.com/cn/r_language/rcpp.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>霜刃未曾试</title><link>http://www.road2stat.com/cn/r_language/assign.html</link> <comments>http://www.road2stat.com/cn/r_language/assign.html#comments</comments> <pubDate>Mon, 16 May 2011 19:58:11 +0000</pubDate> <dc:creator>Xiao Nan</dc:creator> <category><![CDATA[R]]></category> <category><![CDATA[全局变量]]></category> <category><![CDATA[局部变量]]></category><guid isPermaLink="false">http://www.road2stat.com/cn/?p=696</guid> <description><![CDATA[不试哪成? ?View Code RSPLUS0 1 2 3 4 5 6 7 8 9 10 x = rep&#40;NA, 10&#41; test1 &#60;- function&#40;m, n&#41; &#123; for &#40;i in m:n&#41; &#123; x&#91;i&#93; &#60;- i &#125; &#125; &#62; print&#40;x&#41; &#91;1&#93; NA NA NA NA &#8230; <a href="http://www.road2stat.com/cn/r_language/assign.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>不试哪成?</p><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p696code33'); return false;">View Code</a> RSPLUS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p69633"><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p696code33"><pre class="rsplus" style="font-family:monospace;">x <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="color: #0000FF; font-weight: bold;">rep</span></a><span style="color: #080;">&#40;</span>NA, <span style="color: #ff0000;">10</span><span style="color: #080;">&#41;</span>
test1 <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>m, n<span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
  <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="color: #0000FF; font-weight: bold;">for</span></a> <span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> m<span style="color: #080;">:</span>n<span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
    x<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> i
    <span style="color: #080;">&#125;</span>
<span style="color: #080;">&#125;</span>
<span style="color: #080;">&gt;</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="color: #0000FF; font-weight: bold;">print</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>
 <span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span> NA NA NA NA NA NA NA NA NA NA
<span style="color: #080;">&gt;</span> test1<span style="color: #080;">&#40;</span><span style="color: #ff0000;">3</span>, <span style="color: #ff0000;">5</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&gt;</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="color: #0000FF; font-weight: bold;">print</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>
 <span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span> NA NA NA NA NA NA NA NA NA NA</pre></td></tr></table></div><div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p696code34'); return false;">View Code</a> RSPLUS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p69634"><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
</pre></td><td class="code" id="p696code34"><pre class="rsplus" style="font-family:monospace;">test2 <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>m, n<span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
  <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="color: #0000FF; font-weight: bold;">for</span></a> <span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> m<span style="color: #080;">:</span>n<span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
    x<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;&lt;-</span> i
    <span style="color: #080;">&#125;</span>
<span style="color: #080;">&#125;</span>
<span style="color: #080;">&gt;</span> test2<span style="color: #080;">&#40;</span><span style="color: #ff0000;">3</span>, <span style="color: #ff0000;">5</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&gt;</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/print.html"><span style="color: #0000FF; font-weight: bold;">print</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>
 <span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span> NA NA  <span style="color: #ff0000;">3</span>  <span style="color: #ff0000;">4</span>  <span style="color: #ff0000;">5</span> NA NA NA NA NA</pre></td></tr></table></div>]]></content:encoded> <wfw:commentRss>http://www.road2stat.com/cn/r_language/assign.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Ubuntu下使用icc/ifort/MKL编译R及性能实测</title><link>http://www.road2stat.com/cn/r_language/optimize.html</link> <comments>http://www.road2stat.com/cn/r_language/optimize.html#comments</comments> <pubDate>Thu, 05 May 2011 23:55:34 +0000</pubDate> <dc:creator>Xiao Nan</dc:creator> <category><![CDATA[R]]></category> <category><![CDATA[BLAS]]></category> <category><![CDATA[icc]]></category> <category><![CDATA[ifort]]></category> <category><![CDATA[LAPACK]]></category> <category><![CDATA[MKL]]></category> <category><![CDATA[Ubuntu]]></category> <category><![CDATA[优化]]></category> <category><![CDATA[编译]]></category><guid isPermaLink="false">http://www.road2stat.com/cn/?p=682</guid> <description><![CDATA[1 基本环境 Ubuntu 11.04 32-bit R 32-bit 2.13.0 Intel Composer XE 2011.3.174(含icc/ifort/MKL) MKL(Intel Math Kernel Library), 号称"provides extremely well-tuned BLAS and LAPACK implementations that deliver significant performance leadership over alternative math libraries". 在特定的计算情境下会带来一定的性能提升. 记得哪里看到过Revolution R和MATLAB就有用到MKL. 这里尝试用Intel提供的C和Fortran编译器结合其MKL库编译R, 以期尽量发挥现有硬件的性能. Intel的Composer XE中自带了MKL, 而且icc/ifort基本是傻瓜化安装, &#8230; <a href="http://www.road2stat.com/cn/r_language/optimize.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<h1>1 基本环境</h1><ul><li>Ubuntu 11.04 32-bit</li><li>R 32-bit 2.13.0</li><li>Intel Composer XE 2011.3.174(含icc/ifort/MKL)</li></ul><p>MKL(Intel Math Kernel Library), 号称"provides extremely well-tuned BLAS and LAPACK implementations that deliver significant performance leadership over alternative math libraries". 在特定的计算情境下会带来一定的性能提升. 记得哪里看到过Revolution R和MATLAB就有用到MKL. 这里尝试用Intel提供的C和Fortran编译器结合其MKL库编译R, 以期尽量发挥现有硬件的性能. Intel的Composer XE中自带了MKL, 而且icc/ifort基本是傻瓜化安装, 这里不再赘述.</p><h1>2 编译过程</h1><p>假设icc/ifort/MKL安装在默认的 /opt/intel/composerxe-2011.3.174/ 目录下.</p><p>通常我们会这样简单编译安装:</p><blockquote><p>wget http://ftp.ctex.org/mirrors/CRAN/src/base/R-2/R-2.13.0.tar.gz<br /> tar -xf R-2.13.0.tar.gz<br /> cd R-2.13.0<br /> ./configure<br /> make<br /> sudo make install</p></blockquote><p>这次具体指定一下参数就好了.</p><p><span id="more-682"></span></p><p>卸载原有的R:</p><blockquote><p>sudo apt-get remove r-base<br /> sudo apt-get autoremove</p></blockquote><p>设以下为foo.sh</p><blockquote><p>source /opt/intel/composerxe-2011.3.174/bin/iccvars.sh ia32<br /> source /opt/intel/composerxe-2011.3.174/bin/ifortvars.sh ia32<br /> source /opt/intel/composerxe-2011.3.174/mkl/bin/mklvars.sh ia32</p><p>export CC=icc<br /> export CFLAGS="-g -O2 -wd188 -ip -std=c99"<br /> export F77=ifort<br /> export FFLAGS="-g -O3"<br /> export CXX=icpc<br /> export CXXFLAGS="-g -O3"<br /> export FC=ifort<br /> export FCFLAGS="-g -O3"<br /> export ICC_LIBS=/opt/intel/composerxe-2011.3.174/compiler/lib/ia32<br /> export IFC_LIBS=/opt/intel/composerxe-2011.3.174/compiler/lib/ia32<br /> export SHLIB_CXXLD=icpc<br /> export SHLIB_CXXLDFLAGS=-shared</p><p>MKL_LIB_PATH=/opt/intel/composerxe-2011.3.174/mkl/lib/ia32<br /> export LD_LIBRARY_PATH=$MKL_LIB_PATH</p><p>OMP_NUM_THREADS=2</p><p>export LDFLAGS="-L${MKL_LIB_PATH},-Bdirect,--hash-style=both,-Wl,-O1 -L$ICC_LIBS -L$IFC_LIBS -L/usr/local/lib"</p><p>export SHLIB_LDFLAGS="-lpthread"<br /> export MAIN_LDFLAGS="-lpthread"</p><p>MKL="-L${MKL_LIB_PATH} -lmkl_blas95 -lmkl_lapack95  -Wl,--start-group -lmkl_intel -lmkl_intel_thread -lmkl_core -Wl,--end-group -openmp -lpthread"</p></blockquote><p>source一下foo.sh:</p><blockquote><p>source foo.sh</p></blockquote><p>下载解压和configure:</p><blockquote><p>wget http://ftp.ctex.org/mirrors/CRAN/src/base/R-2/R-2.13.0.tar.gz<br /> tar -xf R-2.13.0.tar.gz<br /> cd R-2.13.0<br /> ./configure --enable-R-shlib --with-blas="$MKL"  --with-lapack</p></blockquote><p>最后的configure结果大概会是这样的:</p><blockquote><p>R is now configured for i686-pc-linux-gnu</p><p>Source directory:          .<br /> Installation directory:    /usr/local</p><p>C compiler:                icc  -g -O2 -wd188 -ip -std=c99<br /> Fortran 77 compiler:       ifort  -g -O3</p><p>C++ compiler:              icpc  -g -O3<br /> Fortran 90/95 compiler:    ifort -g -O3<br /> Obj-C compiler:</p><p>Interfaces supported:      X11<br /> External libraries:        readline, BLAS(generic), LAPACK(in blas)<br /> Additional capabilities:   PNG, JPEG, NLS, cairo<br /> Options enabled:           shared R library, R profiling, Java</p><p>Recommended packages:      yes</p></blockquote><p>然后</p><blockquote><p>make<br /> make check<br /> sudo make install</p></blockquote><p>Bingo.</p><p>三点说明:</p><ol><li>make到中间出现找不到/usr/include/asm/errno.h的情况. 加一个符号链接, 解决:<br /> sudo ln -s /usr/include/asm-generic /usr/include/asm</li><li>参考网站[1]给出的中CFLAGS等4处带有参数 -mieee-fp , make时过不去. 在此去掉此参数以后, make通过, 后果未知.</li><li>make install时提示<br /><blockquote><p>icc -I. -I../../src/include -I../../src/include  -I/usr/local/include -DHAVE_CONFIG_H   -openmp -fpic  -g -O2 -wd188 -ip -std=c99 -DR_HOME='"/usr/local/lib/R"' -o Rscript \<br /> ./Rscript.c<br /> /bin/bash: icc: 未找到命令<br /> make[2]: *** [install-Rscript] 错误 127</p></blockquote><p>建立符号链接, 解决:<br /> sudo ln -s /opt/intel/composerxe-2011.3.174/bin/ia32/icc /bin/icc</li></ol><h1>3 性能测试</h1><p>根据 <a href="http://r.research.att.com/benchmarks/" target="_blank">AT&amp;T Research R Benchmark</a> 提供的 R-benchmark-25.R 和 bench.R 做了简单的性能测试, 测试主要涵盖了常用的矩阵计算.</p><h2>3.1 默认安装的测试结果</h2><p>= R-benchmark-25.R =</p><blockquote><p>R Benchmark 2.5<br /> ===============<br /> Number of times each test is run__________________________:  3</p><p>I. Matrix calculation<br /> ---------------------<br /> Creation, transp., deformation of a 2500x2500 matrix (sec):  1.11666666666666<br /> 2400x2400 normal distributed random matrix ^1000____ (sec):  1.86866666666666<br /> Sorting of 7,000,000 random values__________________ (sec):  1.323<br /> 2800x2800 cross-product matrix (b = a' * a)_________ (sec):  23.1193333333333<br /> Linear regr. over a 3000x3000 matrix (c = a \ b')___ (sec):  18.635<br /> --------------------------------------------<br /> Trimmed geom. mean (2 extremes eliminated):  3.58487232845403</p><p>II. Matrix functions<br /> --------------------<br /> FFT over 2,400,000 random values____________________ (sec):  1.50533333333333<br /> Eigenvalues of a 640x640 random matrix______________ (sec):  3.05366666666669<br /> Determinant of a 2500x2500 random matrix____________ (sec):  12.1626666666667<br /> Cholesky decomposition of a 3000x3000 matrix________ (sec):  11.2026666666667<br /> Inverse of a 1600x1600 random matrix________________ (sec):  9.61066666666667<br /> --------------------------------------------<br /> Trimmed geom. mean (2 extremes eliminated):  6.90185004025158</p><p>III. Programmation<br /> ------------------<br /> 3,500,000 Fibonacci numbers calculation (vector calc)(sec):  1.67933333333336<br /> Creation of a 3000x3000 Hilbert matrix (matrix calc) (sec):  1.09566666666666<br /> Grand common divisors of 400,000 pairs (recursion)__ (sec):  1.77066666666667<br /> Creation of a 500x500 Toeplitz matrix (loops)_______ (sec):  2.82166666666666<br /> Escoufier's method on a 45x45 matrix (mixed)________ (sec):  1.90700000000004<br /> --------------------------------------------<br /> Trimmed geom. mean (2 extremes eliminated):  1.78323318573098</p><p>Total time for all 15 tests_________________________ (sec):  92.8720000000001<br /> Overall mean (sum of I, II and III trimmed means/3)_ (sec):  3.53358695795615<br /> --- End of test ---</p></blockquote><p>= bench.R =</p><blockquote><p>[1] "hilbert n=500"<br /> 用户  系统  流逝<br /> 0.604 0.016 0.657<br /> 用户  系统  流逝<br /> 0.492 0.040 0.531<br /> 用户  系统  流逝<br /> 0.508 0.024 0.533<br /> [1] "hilbert n=1000"<br /> 用户  系统  流逝<br /> 3.480 0.140 3.624<br /> 用户  系统  流逝<br /> 2.768 0.108 2.879<br /> 用户  系统  流逝<br /> 2.904 0.128 3.037<br /> [1] "sort n=6"<br /> 用户  系统  流逝<br /> 0.536 0.036 0.568<br /> 用户  系统  流逝<br /> 0.548 0.020 0.569<br /> 用户  系统  流逝<br /> 0.544 0.024 0.568<br /> [1] "sort n=7"<br /> 用户  系统  流逝<br /> 6.921 0.232 7.161<br /> 用户  系统  流逝<br /> 6.840 0.184 7.034<br /> 用户  系统  流逝<br /> 6.989 0.184 7.212<br /> [1] "loess n=3"<br /> 用户  系统  流逝<br /> 0.084 0.000 0.188<br /> 用户  系统  流逝<br /> 0.080 0.000 0.081<br /> 用户 系统 流逝<br /> 0.08 0.00 0.08<br /> 用户  系统  流逝<br /> 0.080 0.000 0.079<br /> 用户  系统  流逝<br /> 0.080 0.000 0.081<br /> [1] "loess n=4"<br /> 用户  系统  流逝<br /> 7.120 0.004 7.125<br /> 用户  系统  流逝<br /> 7.113 0.000 7.119<br /> 用户  系统  流逝<br /> 7.100 0.000 7.109<br /> 用户  系统  流逝<br /> 7.109 0.000 7.116<br /> 用户  系统  流逝<br /> 7.112 0.000 7.119</p></blockquote><h2>3.2 icc+ifort+MKL编译的测试结果</h2><p>= R-benchmark-25.R =</p><blockquote><p>R Benchmark 2.5<br /> ===============<br /> Number of times each test is run__________________________:  3</p><p>I. Matrix calculation<br /> ---------------------<br /> Creation, transp., deformation of a 2500x2500 matrix (sec):  1.00633333333333<br /> 2400x2400 normal distributed random matrix ^1000____ (sec):  1.07333333333333<br /> Sorting of 7,000,000 random values__________________ (sec):  1.22766666666667<br /> 2800x2800 cross-product matrix (b = a' * a)_________ (sec):  2.14133333333334<br /> Linear regr. over a 3000x3000 matrix (c = a \ b')___ (sec):  1.248<br /> --------------------------------------------<br /> Trimmed geom. mean (2 extremes eliminated):  1.180347511679</p><p>II. Matrix functions<br /> --------------------<br /> FFT over 2,400,000 random values____________________ (sec):  1.34566666666667<br /> Eigenvalues of a 640x640 random matrix______________ (sec):  1.496<br /> Determinant of a 2500x2500 random matrix____________ (sec):  1.53566666666667<br /> Cholesky decomposition of a 3000x3000 matrix________ (sec):  1.47066666666667<br /> Inverse of a 1600x1600 random matrix________________ (sec):  1.59333333333333<br /> --------------------------------------------<br /> Trimmed geom. mean (2 extremes eliminated):  1.50054007982371</p><p>III. Programmation<br /> ------------------<br /> 3,500,000 Fibonacci numbers calculation (vector calc)(sec):  1.42833333333333<br /> Creation of a 3000x3000 Hilbert matrix (matrix calc) (sec):  1.348<br /> Grand common divisors of 400,000 pairs (recursion)__ (sec):  1.77033333333334<br /> Creation of a 500x500 Toeplitz matrix (loops)_______ (sec):  2.34466666666667<br /> Escoufier's method on a 45x45 matrix (mixed)________ (sec):  1.211<br /> --------------------------------------------<br /> Trimmed geom. mean (2 extremes eliminated):  1.5049595832868</p><p>Total time for all 15 tests_________________________ (sec):  22.2403333333333<br /> Overall mean (sum of I, II and III trimmed means/3)_ (sec):  1.38652416123196<br /> --- End of test ---</p></blockquote><p>= bench.R =</p><blockquote><p>[1] "hilbert n=500"<br /> 用户  系统  流逝<br /> 0.280 0.032 0.289<br /> 用户  系统  流逝<br /> 0.292 0.032 0.169<br /> 用户  系统  流逝<br /> 0.296 0.036 0.169<br /> [1] "hilbert n=1000"<br /> 用户  系统  流逝<br /> 1.440 0.140 0.971<br /> 用户  系统  流逝<br /> 1.356 0.124 0.842<br /> 用户  系统  流逝<br /> 1.276 0.140 0.834<br /> [1] "sort n=6"<br /> 用户  系统  流逝<br /> 0.408 0.024 0.461<br /> 用户  系统  流逝<br /> 0.416 0.016 0.431<br /> 用户  系统  流逝<br /> 0.408 0.024 0.430<br /> [1] "sort n=7"<br /> 用户  系统  流逝<br /> 5.512 0.216 5.740<br /> 用户  系统  流逝<br /> 5.437 0.260 5.704<br /> 用户  系统  流逝<br /> 5.504 0.192 5.703<br /> [1] "loess n=3"<br /> 用户  系统  流逝<br /> 0.060 0.000 0.128<br /> 用户  系统  流逝<br /> 0.052 0.000 0.051<br /> 用户  系统  流逝<br /> 0.052 0.000 0.051<br /> 用户  系统  流逝<br /> 0.048 0.000 0.051<br /> 用户  系统  流逝<br /> 0.052 0.000 0.050<br /> [1] "loess n=4"<br /> 用户  系统  流逝<br /> 8.897 0.016 4.560<br /> 用户  系统  流逝<br /> 9.184 0.004 4.607<br /> 用户  系统  流逝<br /> 9.141 0.008 4.585<br /> 用户  系统  流逝<br /> 9.204 0.008 4.621<br /> 用户  系统  流逝<br /> 9.209 0.004 4.619</p></blockquote><p>针对 R-benchmark-25.R 的15项测试结果绘制barchart一张. 在部分情况下, Intel编译器和数学库的组合反较默认编译慢, 绝大部分项目icc/ifort/MKL都较默认编译有一定的性能提升, 在5个项目上远远超出默认库的性能(系MKL所致).</p><p><a href="http://www.road2stat.com/cn/wp-content/attachments/2011/05/benchmark.png"><img class="aligncenter size-full wp-image-683" title="benchmark" src="http://www.road2stat.com/cn/wp-content/attachments/2011/05/benchmark.png" alt="benchmark" width="490" height="294" /></a></p><h1>4 主要参考</h1><p>1. <a href="http://www.rd.dnc.ac.jp/~otsu/lecture/RwithMKL.html" target="_blank">Building R-2.8.0 with Intel Compiler Suite 11.0 (icc 11, ifort 11, MKL 10)</a></p><p>2. <a href="http://psyccomputing.blogspot.com/2010/04/compiling-64-bit-r-2101-with-mkl-in.html" target="_blank">Compiling 64-bit R 2.10.1 with MKL in Linux: The rationale for compiling R using the Intel Math Kernel Library</a></p><p>3. 《R Installation and Administration Guide》 Appendix A.3.1.4</p><p>4. <a href="http://software.intel.com/en-us/articles/intel-mkl-link-line-advisor/" target="_blank">Intel MKL参数选择工具</a></p><h1>5 结语</h1><p>自行编译需谨慎, 本人又属linux菜鸟, 不能保证以上设定都是正确和最优的, 且GNU和Intel两家的货混用, 很有可能出问题. 如果因为追求一点速度导致计算结果有误, 都懂的.</p><h1>6 已知问题</h1><p>q("no")以后无响应, 需要强制退出.</p> ]]></content:encoded> <wfw:commentRss>http://www.road2stat.com/cn/r_language/optimize.html/feed</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>R之不务正业 - Chromium Updater</title><link>http://www.road2stat.com/cn/r_language/chromium_updater.html</link> <comments>http://www.road2stat.com/cn/r_language/chromium_updater.html#comments</comments> <pubDate>Sun, 31 Oct 2010 21:36:09 +0000</pubDate> <dc:creator>Xiao Nan</dc:creator> <category><![CDATA[R]]></category> <category><![CDATA[chromium]]></category> <category><![CDATA[updater]]></category> <category><![CDATA[不务正业]]></category> <category><![CDATA[更新]]></category><guid isPermaLink="false">http://www.road2stat.com/cn/?p=624</guid> <description><![CDATA[Chromium是Google Chrome开发版的名称, 这东西每天发布20来个更新版本是正常现象. 故有"好事者"写了名为Nightly Chrome Updater的Chrome扩展, 更有甚者, 制作了专用GUI工具来检查和下载更新 ... 不过 .. 用R中的基础函数写一段脚本来做这个简单工作是再好不过了. 想想每次开R时加载一段脚本自动更新Chromium, 有益身心健康 ... ?Download chromium_update.RtrunkURL = &#34;http://build.chromium.org/f/chromium/snapshots/chromium-rel-xp/&#34; tmpDir = getwd&#40;&#41; appDir = gsub&#40;'Application Data', 'Local Settings/Application Data/Chromium/Application/', Sys.getenv&#40;&#34;APPDATA&#34;&#41;&#41; rmDir = list.files&#40;path = appDir, pattern = &#34;\b\d&#34;&#41; download.file&#40;paste&#40;trunkURL, &#34;LATEST&#34;, &#8230; <a href="http://www.road2stat.com/cn/r_language/chromium_updater.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p><a href="http://www.road2stat.com/cn/attachments/2010/11/r_chromium_logo.png"><img class="aligncenter size-full wp-image-625" title="r_chromium_logo" src="http://www.road2stat.com/cn/attachments/2010/11/r_chromium_logo.png" alt="r_chromium_logo" width="350" height="335" /></a></p><p>Chromium是Google Chrome开发版的名称, 这东西每天发布20来个更新版本是正常现象. 故有"好事者"写了名为<a href="https://chrome.google.com/extensions/detail/akabngbddgpigiemjhkblleffkbpmpfc" target="_blank">Nightly Chrome Updater</a>的Chrome扩展, 更有甚者, 制作了<a href="http://dirhael.dcmembers.com/cnu/" target="_blank">专用GUI工具</a>来检查和下载更新 ... 不过 .. 用R中的基础函数写一段脚本来做这个简单工作是再好不过了. 想想每次开R时加载一段脚本自动更新Chromium, 有益身心健康 ...</p><div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.road2stat.com/cn/wp-content/plugins/wp-codebox/wp-codebox.php?p=624&amp;download=chromium_update.R">chromium_update.R</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p62436"><td class="code" id="p624code36"><pre class="rsplus" style="font-family:monospace;">trunkURL <span style="color: #080;">=</span> 
<span style="color: #ff0000;">&quot;http://build.chromium.org/f/chromium/snapshots/chromium-rel-xp/&quot;</span>
tmpDir <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/getwd.html"><span style="color: #0000FF; font-weight: bold;">getwd</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
appDir <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/gsub.html"><span style="color: #0000FF; font-weight: bold;">gsub</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Application Data'</span>, 
	<span style="color: #ff0000;">'Local Settings/Application Data/Chromium/Application/'</span>, 
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/Sys.getenv.html"><span style="color: #0000FF; font-weight: bold;">Sys.<span style="">getenv</span></span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;APPDATA&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
rmDir <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.files.html"><span style="color: #0000FF; font-weight: bold;">list.<span style="">files</span></span></a><span style="color: #080;">&#40;</span>path <span style="color: #080;">=</span> appDir, pattern <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\b</span><span style="color: #000099; font-weight: bold;">\d</span>&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">download.<span style="">file</span></span><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>trunkURL, <span style="color: #ff0000;">&quot;LATEST&quot;</span>, sep <span style="color: #080;">=</span> <span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>, 
		<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>tmpDir, <span style="color: #ff0000;">&quot;/LATEST&quot;</span>, sep <span style="color: #080;">=</span> <span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>, 
		quiet <span style="color: #080;">=</span> TRUE<span style="color: #080;">&#41;</span>
versionNum <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/readLines.html"><span style="color: #0000FF; font-weight: bold;">readLines</span></a><span style="color: #080;">&#40;</span>
		<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>tmpDir, <span style="color: #ff0000;">&quot;/LATEST&quot;</span>, sep <span style="color: #080;">=</span> <span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>, 
		warn <span style="color: #080;">=</span> FALSE<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/unlink.html"><span style="color: #0000FF; font-weight: bold;">unlink</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>tmpDir, <span style="color: #ff0000;">&quot;/LATEST&quot;</span>, sep <span style="color: #080;">=</span> <span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">download.<span style="">file</span></span><span style="color: #080;">&#40;</span>
		<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>trunkURL, versionNum, 
		<span style="color: #ff0000;">&quot;/mini_installer.exe&quot;</span>, sep <span style="color: #080;">=</span> <span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>, 
		<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>tmpDir, <span style="color: #ff0000;">&quot;/mini_installer.exe&quot;</span>, sep <span style="color: #080;">=</span> <span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>, 
		<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/mode.html"><span style="color: #0000FF; font-weight: bold;">mode</span></a> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;wb&quot;</span><span style="color: #080;">&#41;</span>
shell.<span style="">exec</span><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>tmpDir, 
		<span style="color: #ff0000;">&quot;/mini_installer.exe&quot;</span>, sep <span style="color: #080;">=</span> <span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/Sys.sleep.html"><span style="color: #0000FF; font-weight: bold;">Sys.<span style="">sleep</span></span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">30</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/try.html"><span style="color: #0000FF; font-weight: bold;">try</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/unlink.html"><span style="color: #0000FF; font-weight: bold;">unlink</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>appDir, rmDir, 
		sep <span style="color: #080;">=</span> <span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>, recursive <span style="color: #080;">=</span> TRUE<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/try.html"><span style="color: #0000FF; font-weight: bold;">try</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/unlink.html"><span style="color: #0000FF; font-weight: bold;">unlink</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>tmpDir, 
		<span style="color: #ff0000;">&quot;/mini_installer.exe&quot;</span>, sep <span style="color: #080;">=</span> <span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div><p>这个mini_installer.exe事实上是一个自解压静默安装程序, 用7-Zip可以打开瞧瞧. 但它不会清理原有程序所在目录, 所以清理临时文件和原有程序目录的任务正好就交由脚本完成了. R中的DLL.version()函数要是支持提取DLL中指定字段的信息就好了, 因为Chromium的版本信息是写在chrome.dll中的, 那样就可以先比较当前版本号再判断是否有必要更新了, 残念 ......</p><p>写完了发现毫无技术含量, 而且是简单到令人发指的程度了 ... 当然, 我们可以经过深思熟虑往里加if并包装成函数 ... 改改还可以跨平台, 不过我只会在Win下用, 因为Debian什么的用apt三两句就搞定, 而其他*nix-like系统<a href="http://www.guao.hk/posts/how-to-upgrade-chromium-in-linux.html" target="_blank">写shell脚本</a>最简单.</p><p>再做个GUI程序就理想了.</p> ]]></content:encoded> <wfw:commentRss>http://www.road2stat.com/cn/r_language/chromium_updater.html/feed</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>The Story of R and OAuth</title><link>http://www.road2stat.com/cn/r_language/r_and_oauth.html</link> <comments>http://www.road2stat.com/cn/r_language/r_and_oauth.html#comments</comments> <pubDate>Sun, 24 Oct 2010 08:41:18 +0000</pubDate> <dc:creator>Xiao Nan</dc:creator> <category><![CDATA[R]]></category> <category><![CDATA[OAuth]]></category><guid isPermaLink="false">http://www.road2stat.com/cn/?p=616</guid> <description><![CDATA[OAuth以降, 第三方client需要在授权时接触明文密码的问题解决了. Twitter在今年8月开始强制推行OAuth授权方式. 对此, R中的twitteR包作者Jeff Gentry却在最新的文档中叫苦不迭: The old Twitter authentication mechanism is being removed in August 2010 in favor of OAuth. As of this writing there is not an OAuth solution for R, so unless someone (or myself) writes such &#8230; <a href="http://www.road2stat.com/cn/r_language/r_and_oauth.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p><a href="http://www.road2stat.com/cn/attachments/2010/10/r_oauth_logo.png"><img class="aligncenter size-full wp-image-617" title="r_oauth_logo" src="http://www.road2stat.com/cn/attachments/2010/10/r_oauth_logo.png" alt="r_oauth_logo" width="450" height="453" /></a></p><p>OAuth以降, 第三方client需要在授权时接触明文密码的问题解决了. Twitter在今年8月开始强制推行OAuth授权方式. 对此, R中的twitteR包作者<a href="http://biowww.dfci.harvard.edu/~jgentry/" target="_blank">Jeff Gentry</a>却在最新的文档中叫苦不迭:</p><blockquote><p>The old Twitter authentication mechanism is being removed in August 2010 in favor of OAuth. As of this writing there is not an OAuth solution for R, so unless someone (or myself) writes <strong>such a beast</strong> you will not be able to access any authenticated aspects of Twitter. Many functions that you might be familiar with using will no longer work properly, if at all. These have been set to be defunct.<br /> Also not that it is not simply a matter of having an OAuth interface, but also that Twitter's usage of OAuth is very tied to having stable applications as opposed to scripts being written. There are currently various potential workarounds for this that I'm looking into.</p></blockquote><p>看来自从授权方式改为OAuth之后, 大部分写好的功能都极其华丽丽的失效了. 我没有读过OAuth规范, 不过个人认为, 实现OAuth的一个难点在于需要用户在浏览器中进行一步通过授权的操作. 而其他的token生成, 取回token之类的, 对于开发者都是些简单问题而已. Stack Overflow上就曾经有人问<a href="http://stackoverflow.com/questions/3391460/oauth-access-for-r" target="blank">在R中怎么进行OAuth授权</a>的问题, 有人说不妨尝试用Quick and Dirty的方式, 其实, 这个答案也很Quick and Dirty啊.</p><p>从这个角度出发, 就我的感觉, 从技术上讲,  client开发大概可以分为以下3种情况:</p><ol><li>Web开发, 如php. 客户端本身就是在浏览器里干活, 很容易实现, 且可以借鉴现成代码;</li><li>编译型语言的桌面开发, 如C. 可以通过在客户端内调用浏览器组件实现认证, 对用户很友好, 且有现成的lib, 实现较容易;</li><li>脚本语言的桌面开发, 如Python和R. 除非写很suck的GUI client, 否则基本不可能在客户端内调用浏览器.  这时就可以分出两种处理方式:<ul><li>打开(或让用户手动打开)浏览器, 然后授权获取token, 让用户手动粘贴过来, 整个过称很怪异,  很不友好;</li><li>祭出神器cURL(libcurl)处理.</li></ul></li></ol><p>这里忽略情况1和情况2, 专注于情况3. 情况3的第一种处理方式非常不友好, 但是这也是一些现有示例的处理方式, 如Python. -_-!! 而第二种处理方式的技术难度非常高, 因为天知道那个授权页面是不是用了一些cURL容易处理的方式写成的, 比如授权页面里包含了一些ajax或是其他的什么东西, 完全依赖于开放API的网站有没有考虑到这一点了. 当然, 这是我的个人看法, 这些问题对于大牛也许都不是问题吧. 当然, 如果能使用第二种处理方式, 是再好不过了, 简直完全不亚于情况2. 不过我想没有真正的最终用户去用这种纯命令行的client, 基本都是开发人员用. 以上那些所谓的友好不友好的标准, 也都是针对会用哪一种client做事的用户群而言的, 莫要细究.</p><p>回到开始的TwitteR话题. 一位日本人在最近写了一些关于在R中怎么进行OAuth授权的东西, 当然, 仅仅是针对Twitter的. 他的Slides<a href="http://www.slideshare.net/tor_ozaki/r-oauth-for-twitter-5232160" target="blank">在这里</a>可以看到. 相关的R代码在<a href="http://gist.github.com/586468" target="blank">GitHub</a>上. 由于我没有研究过这个网站的API, 所以不知道他最后实现的是不是我所谓的第2种方式, 单纯从代码上看起来还是比较像的. 希望Jeff看到这些能够有动力把包里的大部分函数更新一下 ...</p><p>同时也期待<a href="http://www.stat.ucdavis.edu/~duncan/" target="blank">Duncan Temple Lang</a>这些牛人们早日给R做一个标准的OAuth包吧.</p> ]]></content:encoded> <wfw:commentRss>http://www.road2stat.com/cn/r_language/r_and_oauth.html/feed</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>R-Forge用户手册 中文版</title><link>http://www.road2stat.com/cn/r_language/r_forge_manual.html</link> <comments>http://www.road2stat.com/cn/r_language/r_forge_manual.html#comments</comments> <pubDate>Sat, 16 Oct 2010 04:51:02 +0000</pubDate> <dc:creator>Xiao Nan</dc:creator> <category><![CDATA[R]]></category> <category><![CDATA[Manual]]></category> <category><![CDATA[R-Forge]]></category> <category><![CDATA[中文版]]></category><guid isPermaLink="false">http://www.road2stat.com/cn/?p=600</guid> <description><![CDATA[R-Forge是提供给R社区的一项开源社区服务, 它拥有SVN和一系列基于Web页面的管理功能. 使用R-Forge, 可以很方便地维护R包. 并且, 通过R-Forge质量管理系统测试的R包, 能够以一种标准化的方式直接发布到CRAN. 这里是我制作的《R-Forge User's Manual》中文版本, 只有短短几页, 简单看过之后即可上手. 文档来源: http://r-forge.r-project.org/scm/viewvc.php/doc/Manual/?root=site&#38;pathrev=147 编译说明: 为了使用效果更加稳健的中文字体, 我改造了源文件, 使用XeLaTeX编译. 当然, 你还得有相应的字体. 关于译文: 我不熟悉SVN, 所以可能有些名词译得不对, 希望看出的同学不吝指出, 我会立即修正, 先行谢过. Download: [Manual, PDF, 279KB] [Source, Gzip, 13KB]]]></description> <content:encoded><![CDATA[<p><a href="http://www.road2stat.com/cn/attachments/2010/10/r-forge-manual.png"><img class="aligncenter size-full wp-image-603" title="r-forge-manual" src="http://www.road2stat.com/cn/attachments/2010/10/r-forge-manual.png" alt="r-forge-manual" width="390" height="119" /></a><br /> <a href="http://r-forge.r-project.org/" target="_blank">R-Forge</a>是提供给R社区的一项开源社区服务, 它拥有SVN和一系列基于Web页面的管理功能. 使用R-Forge, 可以很方便地维护R包. 并且, 通过R-Forge质量管理系统测试的R包, 能够以一种标准化的方式直接发布到<a href="http://cran.r-project.org/" target="_blank">CRAN</a>.</p><p>这里是我制作的《R-Forge User's Manual》中文版本, 只有短短几页, 简单看过之后即可上手.</p><p>文档来源:<br /> <a href="http://r-forge.r-project.org/scm/viewvc.php/doc/Manual/?root=site&amp;pathrev=147" target="_blank">http://r-forge.r-project.org/scm/viewvc.php/doc/Manual/?root=site&amp;pathrev=147</a></p><p>编译说明:<br /> 为了使用效果更加稳健的中文字体, 我改造了源文件, 使用XeLaTeX编译. 当然, 你还得有相应的字体.</p><p>关于译文:<br /> 我不熟悉SVN, 所以可能有些名词译得不对, 希望看出的同学不吝指出, 我会立即修正, 先行谢过.</p><p>Download:<br /> [<a href="http://www.road2stat.com/cn/attachments/2010/10/R-Forge_Manual.pdf">Manual</a>, PDF, 279KB]<br /> [<a href="http://www.road2stat.com/cn/attachments/2010/10/R-Forge_Manual.tar.gz">Source</a>, Gzip, 13KB]</p> ]]></content:encoded> <wfw:commentRss>http://www.road2stat.com/cn/r_language/r_forge_manual.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
