How to Create an R Package

5 Easy Steps

1. Create R functions, classes, and data objects interactively:

Copy-and-paste the code below from here: ecg.R.
R version 2.13.0 alpha (2011-03-28 r55140)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-unknown-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> #sinc function of frequency f
> sinc <- function( x, f )
+     ifelse(x==0, 2*pi*f, sin(2*pi*f*x)/x)
> 
> #Blackman window from 0..m
> Blackman <- function( m )
+     0.42-0.5*cos(2*pi*(0:m)/m)+0.08*cos(4*pi*(0:m)/m)
> 
> #windowed sinc low pass filter
> #y - vector to filter
> #t - time interval between measurements (s)
> #f - low pass frequency (Hz)
> wlpf <- function( y, t, f ) {
+     m  <- min(floor(length(y)/2), 500)
+     rk <- sinc(-m:m, f*t)  
+     bk <- Blackman(2*m) * rk
+     k  <- c(bk, rep(0,length(y)-length(bk)))
+     fy  <- fft(fft(k)*fft(y), inverse=TRUE)
+     return(Re(fy))
+ }
> 
> ecg  <- scan("http://biostatmatt.com/csv/ecg.csv")
Read 22650 items
> ecg  <- ( ecg - mean(ecg) ) / sd(ecg) 
> ls()
[1] "Blackman" "ecg"      "sinc"     "wlpf"

2. Use package.skeleton

> package.skeleton("ecg", ls())
Creating directories ...
Creating DESCRIPTION ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './ecg/Read-and-delete-me'.

3. Document the package functions Blackman, sinc, wlpf, data ecg, and the package itself

Documenting your work is hard, and takes time. When R evaluates the package.skeleton function, a directory named ecg is created (as a subdirectory of the current working directory, see ?getwd). This directory contains all of the necessary R package structure, including a subdirectories named data, R, and man. The data subdirectory contains the ecg data in the RDA format. When the package is loaded, these data are accessed by data(ecg). The R subdirectory contains R code files, one for each code object (/i.e./ sinc.R, wlpf.R, and Blackman.R). Finally, the man subdirectory contains several Rd files, which are used to document the various aspects of your code, data, and the package itself. These files must be edited and formatted in the Rd format. Fortunately, the package.skeleton function generates some of this structure for you.

In addition to files in the man directory, you must also edit the DESCRIPTION file in the root directory. This file contains general information about the package. Finally, the package.skeleton creates a file in the root directory called Read-and-delete-me, which you should ... read and delete.

4. Check that the package is structured and documented correctly

From the Linux shell, the R CMD build ecg command builds and stores the package as a compressed archive (i.e. ecg_1.0.tar.gz):
matt@pal:~$ R CMD build ecg
* checking for file ‘ecg/DESCRIPTION’ ... OK
* preparing ‘ecg’:
* checking DESCRIPTION meta-information ... OK
* installing the package to re-build vignettes
* creating vignettes ... OK
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
* looking to see if a 'data/datalist' file should be added
* building ‘ecg_1.0.tar.gz’
Again, from the Linux shell, the R CMD check ecg_1.0.tar.gz checks the built package to ensure that all data and code items are documented correctly, that the examples work, and that the package can be installed. In other words, R CMD check checks that the newly created R package is structured correctly.
R CMD check ecg_1.0.tar.gz

5. Upload the package to CRAN


Add an Sweave Vignette


Comments

* R CMD check doesn't ensure that your package documentation is 'good', only that it is structured correctly * R CMD check doesn't ensure that your package code does anything useful
Topic attachments
I Attachment Action Size Date Who Comment
ecg.RR ecg.R manage 0.6 K 06 Jun 2011 - 22:57 MattShotwell Step 1. R Code
Edit | Attach | Print version | History: r5 | r4 < r3 < r2 < r1 | Backlinks | View wiki text | Edit WikiText | More topic actions...
Topic revision: r2 - 07 Jun 2011, MattShotwell
 

This site is powered by FoswikiCopyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback