1. Windows 情况下
1.1 预装的软件 (所有软件都可以在 http://www.biosino.org/R/R-doc/Rm/ 和 http://www.biosino.org/R/requiredSoftWares下载)
1.1.1 R 软件(R的官方下载:http://www.r-project.org/index.html;可能在我建设的一个当地非官方下载:http://www.biosino.org/R/R-doc/Rm/;)
1.1.2 Rtools(在Windows情况下编译R包的一些必备软件,感激开源社区如此为我们思量周全;http://www.murdoch-sutherland.com/Rtools/;可能我建设的链接:http://www.biosino.org/R/requiredSoftWares/Rtools26.exe )
1.1.3 微软的HTML Help Workshop (微软的官方下载:http://msdn2.microsoft.com/en-us/library/ms669985.aspx;可能我建设的链接:http://www.biosino.org/R/requiredSoftWares/htmlhelp.exe)(这个大概之前系统就安装过了,假如这样可以不消安装)
1.1.4 MikTex(Tex在Windows下面的实现;官方下载:http://www.miktex.org/Default.aspx;可能我建设的链接:http://www.biosino.org/R/requiredSoftWares/basic-miktex-2.7.2904.exe;假如你要写中文document,请安装CTEX尺度设置:http://www.ctex.org/HomePage)
1.2 安装完后,设置系统的path
1.2.1 假定 R的安装路径在: D:\Program Files\R\R-
1.2.2 在系统的PATH内里加上
c:\Rtools\bin;c:\Rtools\perl\bin;c:\Rtools\MinGW\bin;C:\Program Files\HTML Help Workshop;C:\Program Files\MiKTeX 2.5\miktex\bin;C:\WINDOWS\system32; C:\WINDOWS;C:\WINDOWS\System32\Wbem;
在下图中,箭头所指的区域就是配置PATH的处所,在path中增加路径时,用分号分隔。

1.2.3 确认path配置是否有效。假如不知道,可以问旁边的对计较机较量熟悉的人。键入下面的呼吁,假如正常说明PATH配置有效,
gcc –help
perl –help
R CMD –help
1.3 此刻开始编写R的包,我们用 package.skeleton() 这个函数。
1.3.1 打开R节制台
键入如下代码,
`hl` <-
function() “Hello World!”;
`rLove` <-
function(name) paste(“Welcom to R world, “, name, “!”, sep = “”);
`hl_data` <-
matrix(rnorm(100), nc = 10);
package.skeleton(name=”helloWorld”, list=c(“hl”, “rLove”, “hl_data”);
1.3.2 查察当前事情路径下面,会呈现一个“helloWorld”的目次,个中
R 子目次:包括*.R 文件,就是R代码,我们前面写的hl,rLove函数都是在这内里(:_),hl_data呢?);
data 子目次:呵呵,这个就是放*.rda文件的处所,包罗我们的hl_data.rda;
man子目次:放在 *.Rd文件,就是包,函数,数据的辅佐文档,tex名目标,不要怕Tex,依葫芦画瓢就行了,每个文件一看名字就知道是描写什么内容了;
DESCRIPTION文件:内里的内容我也不表明白,本身改一下;
Read-and-delete-me文件:这个文件没有什么用,看完后删掉。
1.3.3 打包或编译上面的文件
首先,配置一下TMPDIR
D:\Work>set TMPDIR= D:\Work\tmp
然后,有两种处理惩罚方法,
第一种是包的源码名目(*.tar.gz),Linux下面凡是用这种名目安装
D:\Work>R CMD build helloWorld
* checking for file ‘helloWorld/DESCRIPTION’ … OK
* preparing ‘helloWorld’:
* checking DESCRIPTION meta-information … OK
* removing junk files
* checking for LF line-endings in source files
* checking for empty or unneeded directories
* building ‘helloWorld_1.0.tar.gz’
第二种是包的二进制名目(*.zip),Windows下面是用这种安装
D:\Work>R CMD build –binary helloWorld
* checking for file ‘helloWorld/DESCRIPTION’ … OK
* preparing ‘helloWorld’:
* checking DESCRIPTION meta-information … OK
* removing junk files
* checking for LF line-endings in source files
* checking for empty or unneeded directories
* building binary distribution
WARNING
* some HTML links may not be found
installing R.css in D:/Work/tmp/Rinst220096374
Using auto-selected zip options ”
———- Making package helloWorld ————
adding build stamp to DESCRIPTION
installing R files
installing data files
installing man source files
installing indices
not zipping data
installing help
>>> Building/Updating help pages for package ‘helloWorld’
Formats: text html latex example chm
helloWorld-package text html latex example chm
hl text html latex example chm
hl_data text html latex example chm
rLove text html latex example chm
Microsoft HTML Help Compiler 4.74.8702
Compiling d:\Work\tmp\Rbuild220075595\helloWorld\chm\helloWorld.chm
Compile time: 0 minutes, 2 seconds
5 Topics
11 Local links
0 Internet links
1 Graphic
Created d:\Work\tmp\Rbuild220075595\helloWorld\chm\helloWorld.chm, 17,193 bytes
Compression increased file by 5,171 bytes.
adding MD5 sums
packaged installation of package ‘helloWorld’ as helloWorld_1.0.zip
* DONE (helloWorld)
1.3.4 :_),你的第一个包helloWorld_1.0.zip大功告成。如安在Windows下面安装?见下图
然后在节制台键入呼吁
library(helloWorld)
hl();
rLove(“Test”);
data(hl_data);
?hl
1.4 我们是不是发明问题了?辅佐文档内里都是一些文差池题的言语。是的,在
1.5 假如你不想用package.skeleton()这个函数。可以手工设置helloWorld目次,所有的文件都是本身写,写完后再编译。这里较量烦的是写man目次下面的Rd文件,一个简朴要领如下,
1) 把R情况的事情路径设在你要写的包的man目次下;
2) 键入如下代码,
`hl` <-
function() “Hello World!”;
`rLove` <-
function(name) paste(“Welcom to R world, “, name, “!”, sep = “”);
`hl_data` <-
matrix(rnorm(100), nc = 10);
prompt(rLove) # 生成rLove.Rd
prompt(hl) # 生成hl.Rd
prompt(hl_data) # 生成hl_data.Rd
2. Linux 情况下
同Windows下面的做法,除了不消疾苦地设置那些软件(省略1.1和1.2),因为预先已经设置完了。从1.3开始就行了。安装包的方法回收呼吁: R CMD INSTALL helloWorld_1.0.tar.gz
好运。
3. 好手进阶
看《R扩展》(http://www.biosino.org/R/R-doc/Rm/R-exts.html)
