相关日志
-
-
分享
如何开发R包
-
一路嘿嘿 2014-8-2 18:21
-
注: 本文档是Rmd文档,复制下面文本至Rstudio,通过knitr html即可生成html格式文本 --- output: html_document: default pdf_document: includes: in_header: header.tex keep_tex: yes latex_engine: xelatex --- ```{r setup, include=FALSE} library(knitr) library(hello) opts_chunk$set(fig.width=8, fig.height=5) knit_hooks$set(htmlcap = function(before, options, envir) { if(!before) { paste('p class="caption" style="text-align: center; font-size: 20px; color: blue"',options$htmlcap,"/p",sep="") } }) set.seed(60823316) ``` STYLE type="text/css" h1,h2,h3,h4{ color: royalblue; } h1,h2,h5,h6{ text-align: center; } h2{ font-family: palatino, georgia, serif; } body{ font-size: 0.9em; line-height: 23px; } h3{ font-weight: normal; } h6{ font-size: 0.9em; font-weight: normal; line-height: 5px; } hr{ border-top-style: solid; border-top-width: medium; } code { font-size: 80%; line-height: 140%; border: 1px solid #ccc; } @media print{ hr { visibility: inherit; page-break-before: auto; } p { align. } /STYLE ## R package development ### 一. 系统环境要求 **我用的是windows, linux下没测试过** 1. 需要安装的R包 * devtools:开发R包更容易,提供了`load_all`,`document`,`build`,`check`,`insall`等开发包的函数 * roxygen2:R文档中的注释语句可以自动生成函数的帮助文档Rd文件,不用手动编写 ```{r, eval=FALSE} install.packages("devtools", dependencies = TRUE) install.packages("roxygen2", dependencies = TRUE) ``` 2. 生成手册所需软件 * Rtools: http://cran.r-project.org/bin/windows/Rtools/ * Miktex: http://miktex.org/download、 更多可参考: (https://support.rstudio.com/hc/en-us/articles/200486498-Package-Development-Prerequisites) ### 二. 以Hello包为例说明写包的步骤 1. 加载`devtools`,可以更容易的创建包 ```{r,eval=FALSE} library(devtools) ``` 2. 调用`devtools::create`函数,创建包hello,这样会在工作目录下建立hello文件夹,里面包含了必要的文件: ```{r,eval=FALSE} create("hello") ``` * DESCREPTION:包的描述,包括作者,依赖和导入的其他的包,包的功能等,可以参考 * http://cran.r-project.org/doc/manuals/R-exts.html#The-DESCRIPTION-file * http://adv-r.had.co.nz/Package-basics.html * 包的说明(hello.R):包含了命名空间文件的生成NAMESPACE,命名空间可以查看: * http://adv-r.had.co.nz/Namespaces.html * http://cran.r-project.org/doc/manuals/R-exts.html#Package-namespaces 形如: ```r #' The ReverseEcologyR package #' #' This package implementation the applications of reverse ecology. Reverse #' ecology refers to the use of genomics to study ecology with no a priori #' assumptions about the organism(s) under consideration, linking the organism #' and their environment. Prediction the cooperation among species and hosts. #' #' @name ReverseEcologyR #' @docType package #' @importFrom igraph graph.dfs get.adjacency delete.vertices graph.adjacency #' V V- neighborhood.size plot.igraph print.igraph subcomponent is.igraph #' @importFrom Matrix t #' @importFrom Biobase listLen #' @importFrom KEGGREST keggGet keggList #' @importFrom stringr str_replace_all str_count #' @importFrom XML getNodeSet xmlToList xmlParse #' @importFrom magrittr extract is_greater_than %% #' @importFrom gtools permutations #' @importFrom rlist list.append #' @importFrom mmnet updateKEGGPathway NULL ``` 3. 之后就可以在编写函数,如`hello.R`保存在*R*文件夹下 ```{r,eval=FALSE} #’ a simple example function hello #' a simple example function hello #' @author LiLei #' @details more details #' @export hello - function(){ print("Hello, welcome") } ``` 为了方便在写函数的时候把注释写在`.R`文件中,会调用`roxygen2`自动生成函数的*Rd*文件, 即帮助文档,保存在man文件夹下,详细说明可以看roxygen2的说明文档和http://adv-r.had.co.nz/Documenting-functions.html 4. `load_all`加载所有关于hello包中的信息 ```{r,eval=FALSE} ## change the workspace setwd("./hello") load_all() ``` 5. `document`调用`roxygen2`包生成*Rd*文件和NAMESPACE文件 ```{r,eval=FALSE} document() ``` 6. 接下来就可以利用`build` `install` `check`来创建 安装 检查包 ```{r,eval=FALSE} check() build() install() ``` 安装之后, 可以查看包的帮助文档 ```{r, eval=TRUE} help(package="hello") ``` **上面只是建立一个最简单的包,用到的`devtools`中的函数详细用法可以查看帮助** **关于包中数据的存储,类的操作等可以看相关文档,上面方法是hardley wickham书中adv-r 介绍的过程, 我觉得最简单的一种方法了,碰到什么问题我们也可以共同讨论** ### 三. 资料 ### * http://www.bioconductor.org/developers/package-guidelines/ * http://adv-r.had.co.nz * http://cran.r-project.org/doc/manuals/R-exts.html
-
38 次阅读|0 个评论
GMT+8, 2025-12-9 14:39