• 如何在windows中编写R程序包(转载)


    网上有不少R包的编译过程介绍,挑选了一篇比较详细的,做了稍许修改后转载至此,与大家分享

    如何在windows中编写R程序包

    created by helixcn

    modified by binaryfan

    在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:Rtoolsin;c:Rtoolsperlin;c:RtoolsMinGWin; C:CTEXMiKTeXmiktexin;C:CTEXCTeXctexin;C:CTEXCTeXcctin;C:CTEXCTeX yin;C:CTEXGhostscriptgs8.64in;C:CTEXGSviewgsview;C:CTEXWinEdt;C:Program FilesRR-2.9.0in;
    设置启动路径的目的是在cmd命令行可以直接调用相应的exe文件。

    如果只是简单制作一个个人使用的包,只需将c:Rtoolsin;c:Rtoolsperlin;c:RtoolsMinGWin; 添加到系统路径即可

    二 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
    ame{linmod}
    Rdversion{1.1}
    alias{linmod}
    %- Also NEED an 'alias' for EACH other topic documented here.
    itle{
    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'}
    %% ...
    }
    eferences{
    Friedrich Leisch,2008 Creating R Packages: A Tutorial
    }
    author{
    helixcn
    }
    ote{
    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
    ame{linmod-package}
    Rdversion{1.1}
    alias{linmod-package}
    alias{linmod}
    docType{package}
    itle{Linear Regression Modification}
    description{to Give the more exactly output of linear regression rather than R default}
    details{
    abular{ll}{
    Package: ab linmodcr
    Type: ab Packagecr
    Version: ab 1.0cr
    Date: ab 2009-07-20cr
    License: ab GNU 2.0 or latercr
    LazyLoad: ab yescr
    }
    ~~The aim of the package was to give the more exactly output of linear regression~~ linmod~~
    }
    author{helixcn
    Maintainer: helixcn <helixcn@163.com>}
    eferences{
    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

  • 相关阅读:
    SpringMVC 中整合之JSON、XML
    解决Django扩展用户表后新表内增加用户密码存储为明文
    python之路:始解(四)
    python作业:select版本FTP
    python作业:高级FTP程序
    python归档:笔记转化
    python之路:始解(三)
    python之路:始解(二)
    python作业:FTP程序
    python文件操作:pickle模块多次dump后出现的读取问题
  • 原文地址:https://www.cnblogs.com/hdu-2010/p/3557610.html
Copyright © 2020-2023  润新知