As of late July 2013, there were 4,714 packages on CRAN and another 671 on Bioconductor, with more being added daily. In the past, building a package had the potential to be mystifying and complicated but that is no longer the case, especially when using the Hadley Wickham’s devtools package.
As I have worked with lots of projects, I have built up lots
of functions that help me produce result like data tables, graphs that I find useful.
But in many cases, it happens that I reuse the function many times that feel
really awkward. I have been a fan of the idea of personal R package.
In this blog we are going too discussed about how to build
perfect R package so that you don't have to keep thinking to yourself.
Step 1: Package
you will need to create a package are:
install.packages("devtools")
library("devtools")
devtools::install_github("klutometis/roxygen")
library(roxygen2)
|
Step 2:
Create your package directory
# set Path
setwd("C:/Users/admin1/Documents")
# Create Package
create("ownpack")
If you
look in your parent directory (i.e. the path that you will set before creating
package), you will now have a folder called ownPackage, and in it you will have two folders and one file called
DESCRIPTION. The structure should be similar to below image
Step 3: Add
Functions in your Package
When you create packages, you need to add function into it. I
have some line of code where I am create a function.
#'
R_fun Function
#'
#'
This function helps you to check whether condition is TRUE or FALSE
#'
@keywords ownpack
#'
@export
#'
@examples
#'
R_fun()
R_fun<-
function(i)
{
if(i>11)
{
print("Hi
condition is TRUE")
}
else{
print("Hi
condition is FALSE")
}
}
|
After that save this file R_fun-function.R
into your R directory. And also create a new file R_fun-package.R in same folder and paste above code into it. The
structure looks like
Similar
you can add lots of function into your package. I just add only one function so
that you can understand better.
Step 4: Process
your Documentation
Now you need to create documentation from your annotations
earlier. Firstly set the path
setwd ("C:/Users/admin1/Documents/ownPackage")
document
( )
This
automatically adds in the .Rd files to the man directory, and adds a NAMESPACE
file to the main directory.
Step 4: Install
Now we
are ready to install the package. But before install this package we need to
come to our parent directory.
setwd ("C:/Users/admin1/Documents
")
install
("ownPackage")
Now your
package is successfully installed, Now you can also check that your function is
successfully work or not
# Include library
library(ownPackage)
# Try this command
?R_fun
# Assign Value
R_fun(3)
[1]
"Hi condition is FALSE"
If you
are getting this result it means your function is perfectly work. Basically
package building is a great way to make code portable between projects and to
share it with other people. A package purely built with R code only requires
working function that can pass the CRAN check using check and proper help files
that can easily built by including roxygen2 documentation above functions and
calling document. Building the package is as simple as using build.
This way
you can build your own package in R.