Using Rstudio

First create your own .R files , you can create one file with all of your functions or create separate files for each function , e.g. llibrary function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
llibrary <- function(pkg, Bioc_Mirror = "http://mirrors.ustc.edu.cn/bioc/",...) {
if (suppressWarnings(require(pkg, character.only = T))) {
cat("loading", pkg, "done!")
}
else {
cat("installing", pkg)
install.packages(pkg)
if (suppressWarnings(require(pkg, character.only = T))) {
sprintf("%s %s %s", "loading", pkg, "done!!!")
} else {
cat("Try to install bioconductor packages", pkg)
options(BioC_mirror = Bioc_Mirror)
source("https://bioconductor.org/biocLite.R")
biocLite(pkg)
if (require(pkg, character.only = T)) {
sprintf("%s %s %s", "loading", pkg, "done!!!")
} else{
stop(pkg, " installed error!")
}
}
}
}

Clean your workingspace, make sure devtools is installed

1
rm(list= ls())

Open a new project in RStudio and create a new R package, put all your .R files in.

Then, the ‘man’ folder will contain the help files for each function in your package on your lower right hand side .( i.e. .Rd or R documentation files, which contains the help files for your
functions in LATEX code )

If your ‘man’ file is empty, go to File > New File > R Documentation to create your own .Rd files.

Note: In .Rd, the ‘title’ field is required

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
\name{llibrary}
\alias{llibrary}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{
%% ~~function to do ... ~~
Installing/Loading a R/Bioc package
}
\description{
%% ~~ A concise (1-5 lines) description of what the function does. ~~
A brief function to perform `install.packages`,`biocLite`,`library`.
}
\usage{
llibrary("packagename")
}
%- maybe also 'usage' for other objects documented here.
\arguments{
\item{x}{
%% ~~Describe \code{x} here~~
packagename
}
}
\details{
%% ~~ If necessary, more details than the description above ~~
}
\value{
%% ~Describe the value returned
%% If it is a LIST, use
%% \item{comp1 }{Description of 'comp1'}
%% \item{comp2 }{Description of 'comp2'}
%% ...
}
\references{
%% ~put references to the literature/web site here ~
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole
}
\author{
%% ~~who you are~~
Who am I
}
\note{
%% ~~further notes~~
}

%% ~Make other sections like Warning with \section{Warning }{....} ~

\seealso{
%% ~~objects to See Also as \code{\link{help}}, ~~~
\code{\link{library}}
}
\examples{
llibrary("ggplot2")
llibrary("‘BiocParallel")
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{ ~kwd1 }% use one of RShowDoc("KEYWORDS")
\keyword{ ~kwd2 }% __ONLY ONE__ keyword per line

Compile your package :Ctrl+Shift+B, test the functions you written to make sure they work

properly.

Command Line Tools

1
2
3
4
5
6
7
8
9
10
rm(list = ls())
package.skeleton(name="DaintyR",code_files=dir(pattern=".R$"))
# Creating directories ...
# Creating DESCRIPTION ...
# Creating NAMESPACE ...
# Creating Read-and-delete-me ...
# Copying code files ...
# Making help files ...
# Done.
# Further steps are described in './DaintyR/Read-and-delete-me'.
  • Edit the help file skeletons in 'man’, add a title under the ‘title’ heading and save.
  • Edit the exports in ‘NAMESPACE’, and add necessary imports
  • Put any C/C++/Fortran code in ‘src’.
  • If you have compiled code, add a useDynLib() directive to
    ‘NAMESPACE’.
  • Run R CMD build/R CMD build – binaryto build the package tarball.
  • Run *R CMD check .tar.gz to check the package tarball.
  • Run **R CMD install ** to check the package tarball.
1
2
3
4
5
6
7
8
9
library(devtools)
my.Rpackage <- as.package("DaintyR")
load_all(my.Rpackage)
document(my.Rpackage)
# Updating DaintyR documentation
# Loading DaintyR
# First time using roxygen2. Upgrading automatically...
# Updating roxygen version in G:\R\R_POST\Vignettes\DaintyR/DESCRIPTION
# The existing 'NAMESPACE' file was not generated by roxygen2, and will not be overwritten.

devtools

1
2
3
4
5
6
7
8
library(devtools)
dev_mode()
install()
library(DaintyR)
# cd R-dev
# ls -lah
dev_mode()
detach("package:DaintyR", unload=T)

REFERENCES

Writing R Extensions

Parsing Rd files

http://cran.fhcrc.org/web/packages/RUnit/vignettes/RUnit.pdf

http://web.mit.edu/insong/www/pdf/rpackage_instructions.pdf

https://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf