大数据人|大数据第一社区

 找回密码
 注册会员

扫一扫,访问微社区

查看: 1159|回复: 0
打印 上一主题 下一主题

如何在windows中编写R程序包

[复制链接]
  • TA的每日心情
    奋斗
    2015-7-30 23:05
  • 签到天数: 12 天

    [LV.3]偶尔看看II

    852

    主题

    972

    帖子

    4804

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    4804
    QQ
    跳转到指定楼层
    楼主
    发表于 2015-8-16 15:13:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
      工具软件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:\uRtools\bin;c:\uRtools\uperl\bin;c:\uRtools\uMinGW\bin; C:\uCTEX\uMiKTeX\umiktex\bin;C:\uCTEX\uCTeX\uctex\bin;C:\uCTEX\uCTeX\cct\bin;C:\uCTEX\uCTeX\ty\bin;C:\uCTEX\uGhostscript\gs8.64\bin;C:\uCTEX\uGSview\ugsview;C:\uCTEX\uWinEdt;C:\uProgram Files\R\R-2.9.0\bin\;
      设置启动路径的目的是在cmd命令行可以直接调用相应的exe文件。
      如果只是简单制作一个个人使用的包,只需将c:\uRtools\bin;c:\uRtools\uperl\bin;c:\uRtools\uMinGW\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 :开始所有程序RR.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}
      \uRdversion{1.1}
      \ualias{linmod}
      %- Also NEED an '\ualias' for EACH other topic documented here.
      \utitle{
      linear regression
      }
      \udescription{
      to give the more exactly results of linear regression
      }
      %usage{
      linmod(x, y)
      }
      %- maybe also 'usage' for other objects documented here.
      \uarguments{
      \uitem{x}{
      a numeric design matrix for the model
      }
      \uitem{y}{
      a numeric vector of responses
      }
      }
      \udetails{
      %% ~~ If necessary, more details than the description above ~~
      }
      \uvalue{
      %% ~Describe the value returned
      %% If it is a LIST, use
      %% \uitem{comp1 }{Description of 'comp1'}
      %% \uitem{comp2 }{Description of 'comp2'}
      %% ...
      }
      \ureferences{
      Friedrich Leisch,2008 Creating R Packages: A Tutorial
      }
      \uauthor{
      helixcn
      }
      ote{
      Please read Friedrich Leisch,2008
      }
      %% ~Make other sections like Warning with \usection{Warning }{....} ~
      \useealso{
      %% ~~objects to See Also as \ucode{\ulink{help}}, ~~~
      }
      \uexamples{
      ##---- 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.
      \ukeyword{ ~kwd1 }
      \ukeyword{ ~kwd2 }% __ONLY ONE__ keyword per line
      2)linmod-package.Rd
      ame{linmod-package}
      \uRdversion{1.1}
      \ualias{linmod-package}
      \ualias{linmod}
      \udocType{package}
      \utitle{Linear Regression Modification}
      \udescription{to Give the more exactly output of linear regression rather than R default}
      \udetails{
      \utabular{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~~
      }
      \uauthor{helixcn
      Maintainer: helixcn helixcn@163.com}
      \ureferences{
      Friedrich Leisch,2008,Creating R Packages: A Tutorial
      }
      \useealso{lm}
      \uexamples{
      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
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册会员

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条


    id="mn_portal" >首页Portalid="mn_P18" onmouseover="navShow('P18')">应用id="mn_P15" onmouseover="navShow('P15')">技术id="mn_P37" onmouseover="showMenu({'ctrlid':this.id,'ctrlclass':'hover','duration':2})">前沿id="mn_P36" onmouseover="navShow('P36')">宝箱id="mn_P61" onmouseover="showMenu({'ctrlid':this.id,'ctrlclass':'hover','duration':2})">专栏id="mn_P65" >企业id="mn_Nd633" >导航 折叠导航 关注微信 关注微博 关注我们

    QQ|广告服务|关于我们|Archiver|手机版|小黑屋|大数据人 ( 鄂ICP备14012176号-2  

    GMT+8, 2024-5-17 11:31 , Processed in 0.254738 second(s), 30 queries .

    Powered by 小雄! X3.2

    © 2014-2020 bigdataer Inc.

    快速回复 返回顶部 返回列表