环境

  • 使用RStudio
  • 安装了devtools
  • github账号

创建包的骨架

  1. 在RStudio中选择创建项目(create project …)
  2. 输入名字, 选择git管理项目
  3. 结束后你会看到一个最基本的package
  4. 在R目录下创建一个文件, 写入代码. ```{r} require(stats) require(metRology)

#’ @title dtruncdist: truncated and inflated density function of specific distribution #’ @description when spec=’norm’, this is the corresponding function of ‘dnorm’. when lbound<x<ubound, dtruncdist(x,spec=’norm’,lbound,ubound)=dnorm(x) #’ when x=lbound, the return value is the mass probability at lbound, which is pnorm(lbound), in this case. #’ @param x vector of x values #’ @param spec distribution’s rout name defined in https://cran.r-project.org/doc/manuals/R-intro.html#Probability-distributions #’ @param … additional parameters needed by the specified distribution #’ @seealso ptruncdist,qtruncdist,rtruncdist #’ @examples #’ dtruncdist(1.2,’t’,df=2,ncp=0) #’ dtruncdist<-function(x,spec=’norm’,lbound=-1,ubound=1,…) { res<-rep(0,length(x)) g<-get(paste(‘d’,spec,sep=’’),mode=’function’) res[x>lbound&x<ubound]=g(x[x>lbound&x<ubound],…) gin<-get(paste(‘p’,spec,sep=’’),mode=’function’) pl = gin(lbound,…) pu = gin(ubound,…) res = res + pl(x==lbound)+pu(x==ubound) return(res) }

5. 运行devtools::document()将按照代码中的注释生成相应的帮助文件.
6. 修改DESCRIPTION文件,加入对于其他包的依赖.
```{r}
Package: truncinflatedist
Type: Package
Title: Truncated and Inflated Distribution
Version: 0.1.0
Author: Hai Lan
Maintainer: Hai Lan <lh_loki@163.com>
Description: General function to translate any distribution to its truncated and inflated one. related d,p,q,r functions are all provided. For the purpose of fitting distribution, some wrap distribution for norm,t.scaled,logis,beta are also provided.
License: MIT
Encoding: UTF-8
LazyData: true
imports: stats
RoxygenNote: 6.0.1.9000
  1. 编译并安装 rstudiobuild
  2. 上传到github rstudiogit
  3. 你可以使用devtools::install_github(“lhloki/truncinflatedist”)在你的机器上安装.