编写R程序包通常包括以下几步:
(1) 工具软件Rtools的安装和备选软件的安装。
(2) r脚本的准备,也就是用来生成程序包的函数脚本。
(3) 利用R中自带的package.skeleton()函数,生成制作包所需要的Description 文件和帮助文件帮助文件.rd。
(4) 编辑该函数生成的Description 文件和帮助文件.rd
(5) 在windows cmd的命令行中输入相应的命令,生成zip文件或者.tar.gz
下面我们来一起建立只有一个函数的R程余哪序包,来详细说明:
一 工具软件安装和配置
制作r包的工具软件包括Rtools,HTML编译器,MikTeX 或Cte等(备选软件不一定要安装):
1 工具软配磨件安装
(1)Rtools(制作R包的主要工具)
Rtools是在windows下制作R包的一系列工具,其中包括
1) CYGWIN 在Windows下模拟UNIX环境
2) MinGW编译器,可用来编译C和Fortran语言。
3) Perl
下载地培毁斗址: http://www.murdoch-sutherland.com/Rtools/
(2) 微软HTML编译器(备选):
用来从源文件生成HTML格式的帮助文件
下载地址:http://go.microsoft.com/fwlink/?LinkId=14188
(3) MikTeX 或CteX(备选)
用来生成PDF格式的帮助文件
下载地址:http://www.miktex.org/ www.ctex.org/
分别按照要求安装好。
2 设置文件启动路径:
我的电脑>属性>高级>环境变量>系统变量 PATH一项,点击“编辑”,检查是否具有以下路径,如果没有,需要手工添加:
c:\Rtools\binc:\Rtools\perl\binc:\Rtools\MinGW\binC:\CTEX\MiKTeX\miktex\binC:\CTEX\CTeX\ctex\binC:\CTEX\CTeX\cct\binC:\CTEX\CTeX\ty\binC:\CTEX\Ghostscript\gs8.64\binC:\CTEX\GSview\gsviewC:\CTEX\WinEdtC:\Program Files\R\R-2.9.0\bin\
设置启动路径的目的是在cmd命令行可以直接调用相应的exe文件。
如果只是简单制作一个个人使用的包,只需将c:\Rtools\binc:\Rtools\perl\binc:\Rtools\MinGW\bin添加到系统路径即可
二 R脚本的准备
假如现在我们已经有了一个编好的R函数,用来给出回归的精确结果,存成了r脚本的格式,文件名为linmod.r
其内容如下所示,那么该如何制作R程序包呢?
linmod<- function(x, y)
{
## compute QR-decomposition of x
qx <- qr(x)
## compute (x'x)^(-1) x'y
coef <- solve.qr(qx, y)
## degrees of freedom and standard deviation of residuals
df <- nrow(x)-ncol(x)
sigma2 <- sum((y - x%*%coef)^2)/df
## compute sigma^2 * (x'x)^-1
vcov <- sigma2 * chol2inv(qx$qr)
colnames(vcov) <- rownames(vcov) <- colnames(x)
list(coefficients = coef,
vcov = vcov,
sigma = sqrt(sigma2),
df = df)
}
三 R包框架的准备
1 生成准备文件
登陆R :开始>所有程序>R>R.2.9.0
(1)清除内存中的对象:
rm(list=ls())
(2)设定工作目录,这里设定为 c:/pa
setwd("c:/pa")
(3)将制作包的源文件 linmod.r拷贝到c:/pa/文件夹下,
之后输入:
package.skeleton(name="linmod",code_files="c:/pa/linmod.r")
此时,R控制台中显示
Creating directories ...
Creating DESCRIPTION ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './linmod/Read-and-delete-me'.
>
可以看到c:/pa文件夹下新出现了一个linmod文件夹
该文件夹下的内容就是R包的框架,包括data文件夹,man文件夹,只要按要求将其填写完整,再进行相应的编译即可。
首先查看Read-and-delete-me文件
文件内容如下:
* Edit the help file skeletons in 'man', possibly combining help
files for multiple functions.
* Put any C/C++/Fortran code in 'src'.
* If you have compiled code, add a .First.lib() function in 'R' to
load the shared library.
* Run R CMD build to build the package tarball.
* Run R CMD check to check the package tarball.
Read "Writing R Extensions" for more information.
大致意思如下:
可以man文件夹下编辑帮助文件
C/C++/Fortran 的源代码应该放入src文件夹下
需要在登录时载入包
可以运行R CMD建立和检查相应的包
查看更多信息,应该阅读Writing R Extensions
2 编辑Description文件和rd文件
(1) Description文件的编辑
按照提示,填好各项
Package: linmod
Type: Package
Title: test for linear regression
Version: 1.0
Date: 2009-07-20
Author: helixcn
Maintainer: helixcn <zhangjl@ibcas.ac.cn>
Description: To give the exactly results of linear regression.
License: GNU 2 or later
LazyLoad: yes
(2)man文件夹中.rd文件编辑
man文件夹中包含两个文件 linmod.Rd和linmod-package.Rd,分别是对linmod()函数和linmod包的介绍,下面逐项填写:
1) linmod.Rd
\name{linmod}
\Rdversion{1.1}
\alias{linmod}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{
linear regression
}
\description{
to give the more exactly results of linear regression
}
\usage{
linmod(x, y)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
\item{x}{
a numeric design matrix for the model
}
\item{y}{
a numeric vector of responses
}
}
\details{
%% ~~ If necessary, more details than the description above ~~
}
\value{
%% ~Describe the value returned
%% If it is a LIST, use
%% \item{comp1 }{Description of 'comp1'}
%% \item{comp2 }{Description of 'comp2'}
%% ...
}
\references{
Friedrich Leisch,2008 Creating R Packages: A Tutorial
}
\author{
helixcn
}
\note{
Please read Friedrich Leisch,2008
}
%% ~Make other sections like Warning with \section{Warning }{....} ~
\seealso{
%% ~~objects to See Also as \code{\link{help}}, ~~~
}
\examples{
##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (x, y)
{
qx <- qr(x)
coef <- solve.qr(qx, y)
df <- nrow(x) - ncol(x)
sigma2 <- sum((y - x \%*\% coef)^2)/df
vcov <- sigma2 * chol2inv(qx$qr)
colnames(vcov) <- rownames(vcov) <- colnames(x)
list(coefficients = coef, vcov = vcov, sigma = sqrt(sigma2),
df = df)
}
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{ ~kwd1 }
\keyword{ ~kwd2 }% __ONLY ONE__ keyword per line
2)linmod-package.Rd
\name{linmod-package}
\Rdversion{1.1}
\alias{linmod-package}
\alias{linmod}
\docType{package}
\title{Linear Regression Modification}
\description{to Give the more exactly output of linear regression rather than R default}
\details{
\tabular{ll}{
Package: \tab linmod\cr
Type: \tab Package\cr
Version: \tab 1.0\cr
Date: \tab 2009-07-20\cr
License: \tab GNU 2.0 or later\cr
LazyLoad: \tab yes\cr
}
~~The aim of the package was to give the more exactly output of linear regression~~ linmod~~
}
\author{helixcn
Maintainer: helixcn <helixcn@163.com>}
\references{
Friedrich Leisch,2008,Creating R Packages: A Tutorial
}
\seealso{lm}
\examples{
data(cats, package="MASS")
mod1 <- linmod(Hwt~Bwt*Sex, data=cats)
mod1
summary(mod1)
}
四 通过cmd创建R包
开始>运行>cmd
键入 cd c:\pa\ 将工作目录转移到c:/pa下
键入 Rcmd build --binary linmod 制作window zip包
键入 Rcmd build linmod 制作linux平台下可运行的tar.gz包
命令运行完之后可以发现,在c:/pa/文件夹下分别生成了linmod.zip和linmod.tar.gz压缩包。
注意R CMD 系列命令是在windows控制台下运行,而非R控制台
参考网址
[1]http://www.robjhyndman.com/researchtips/building-r-packages-for-windows/
[2]http://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf
[3]http://faculty.chicagobooth.edu/peter.rossi/research/bayes%20book/bayesm/Making%20R%20Packages%20Under%20Windows.pdf
[4]http://www.biostat.uni-hannover.de/teaching/fallstudien/schaarschmidt2.pdf
你是不是用R Gui安岩顷装的,提示已经说了缺少quadprog依赖包,用R Gui安装的话需要先把依赖包一个个安装上,比较麻烦,
可以在Rstudio上安装,
install.packages('tseries')
also installing the dependencies ‘quadprog’, ‘zoo’
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.3/quadprog_1.5-5.zip'
Content type 'application/zip' length 52439 bytes (51 KB)
downloaded 51 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.3/zoo_1.7-13.zip'
Content type 'application/zip' length 899932 bytes (878 KB)
downloaded 878 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.3/tseries_0.10-35.zip'
Content type 'application/zip' length 327381 bytes (319 KB)
downloaded 319 KB
package ‘quadprog’ successfully unpacked and MD5 sums checked
package ‘zoo’ successfully unpacked and MD5 sums checked
package ‘tseries’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\yuexf\AppData\Local\Temp\RtmpqqGpKz\downloaded_packages
>library(tseries)
‘tseries’ version: 0.10-35
‘tseries’兄配 is a package for time series analysis and computational finance.
See ‘library(help="tseries")’ for details.
它会自动把依赖包粗尘陆给你安装上
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)