当前位置:天才代写 > tutorial > 其他教程 > 如安在windows中编写R措施包

如安在windows中编写R措施包

2017-12-04 08:00 星期一 所属: 其他教程 浏览:642

在Windows情况下如何编写R措施包,即生成供Linux情况编译运行的tar.gz文件,也生成供windows下利用的.zip文件呢?其实并不巨大,只要下载一些东西软件,凭据相应的步调填写相应的“表格”,继而运行一些简朴的指令,就可以生成R的措施包了。

编写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\bin;c:\Rtools\perl\bin;c:\Rtools\MinGW\bin; C:\CTEX\MiKTeX\miktex\bin;C:\CTEX\CTeX\ctex\bin;C:\CTEX\CTeX\cct\bin;C:\CTEX\CTeX\ty\bin;C:\CTEX\Ghostscript\gs8.64\bin;C:\CTEX\GSview\gsview;C:\CTEX\WinEdt;C:\Program Files\R\R-2.9.0\bin\;
配置启动路径的目标是在cmd呼吁行可以直接挪用相应的exe文件。

假如只是简朴建造一个小我私家利用的包,只需将c:\Rtools\bin;c:\Rtools\perl\bin;c:\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 <[email protected]>
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 <[email protected]>}
\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节制台

 

    关键字:

天才代写-代写联系方式