Today
•How can we make an R package?
•How can we share my R package through GitHub?
•What else do I need to make an R package that contains
Rcppcode?
•How can I document my R package conveniently?
Hyun Min Kang Statistical Computing (BIOSTAT615) 2
Why R package?
•R packaging system has been one of the key factors in
the overal sucessof the R project.
•Packages alow for easy, transparent, and cros-platform.
extensionsof the R base system.
•R packages are a comfortablemeans to maintain
colection of R functions and datasets
Hyun Min Kang Statistical Computing (BIOSTAT615) 3
Pure Rimplementation of Viterbi algorithm
viterbiHMM
Description: This package contains a basic algorithm for
generic HMM
License: GPL-3
Hyun Min Kang Statistical Computing (BIOSTAT615) 12
Running R CMD check myHMM
$ R CMD check myHMM
* using log directory ‘/blah/615_1_10/myHMM.Rcheck’
* using R version 3.4.2 (2017-09-28)
* using platform. x86_64-apple-darwin13.4.0 (64-bit)
* using session charset: UTF-8
* checking for file ‘myHMM/DESCRIPTION’ ... OK
* checking extension type ... Package
* this is package ‘myHMM’ version ‘1.0’
* checking package namespace information ... OK
... (omitted)
* checking examples ... OK
* checking PDF version of manual ... OK
* DONE
Status: 1 NOTE
See
‘/blah/615_1_10/myHMM.Rcheck/00check.log’
for details.
Hyun Min Kang Statistical Computing (BIOSTAT615) 13
Running R CMD build myHMM
$ R CMD build myHMM
* checking for file ‘myHMM/DESCRIPTION’ ... OK
* preparing ‘myHMM’:
* checking DESCRIPTION meta-information ... OK
* installing the package to process help pages
* saving partial Rd database
* checking for LF line-endings in source and make files
and shell scripts
* checking for empty or unneeded directories
* building ‘myHMM_1.0.tar.gz’
Hyun Min Kang Statistical Computing (BIOSTAT615) 14
Installingthe package
Hyun Min Kang Statistical Computing (BIOSTAT615) 15
Example useof the package
Hyun Min Kang Statistical Computing (BIOSTAT615) 16
Example helppages
Hyun Min Kang Statistical Computing (BIOSTAT615) 17
Example manual pdf (in .Rcheckfolder)
Hyun Min Kang Statistical Computing (BIOSTAT615) 18
Using devtools/roxygen2to create an R/Rcpppackage
Install devtoolsand roxygen2
1.Run devtools::create(“pkgName”,rstudio=FALSE)
2.Add license devtools::use_{gpl3|mit}_license(“pkgName”)
3.Enable roxygen2 using devtools::use_package_doc(“pkgName”)
4.Enable Rcppusing devtools::use_rcpp(“pkgName”)
5.Add your R and C+ files in pkgName/Rand pkgName/src
6.Run devtools::document(“pkgName”)
7.Edit pkgName/R/pkgName-package.r
8.Run devtools::check(“pkgName”) to test and check
9.Run devtools::build(“pkgName”) to build package
Hyun Min Kang Statistical Computing (BIOSTAT615) 19
Using roxygen2for better documentation
#' Viterbi algorithm implemented in R
#'
#' This function implements a Viterbi algorithm, given observed outcomes,
transition and emission matrices, and initial probabilities.
#'
#' @paramobssize T integer vector of observed outcomes, values from 1 to m
#' @paramtransMtxn x n transition matrix from row to column
#' @paramemisMtxn x m emission matrix of observed data (column) given states
(row)
#' @parampisize n vector of initial state probabilities
#' @returna list containing maximum likelihood (ml) and the Viterbi path (path)
#' @examples
#' obs devtools::install_github(”username/reponame”)
•For example, try to install my package at
> devtools::install_github(“hyunminkang/hmm615”)
And remove it using remove.packages(“hmm615”)
Hyun Min Kang Statistical Computing (BIOSTAT615) 26
Summary
•R package is an efective way to shareyour methods and tols
with others.
•Writing R package is straightforwardbut requires additional
work beside coding.
•Using devtoolsand roxygen2can make writing R/Rcp
packages in a more convenientand organized way.
•Putting your package on GitHubis one of the most efective
ways to share your R/Rcpcode with others.
Hyun Min Kang Statistical Computing (BIOSTAT615) 27