Quantcast
Channel: Web Analytics India » Web Analytics
Viewing all articles
Browse latest Browse all 12

Package Creation in ‘R’: Series 1

0
0

An R Package is a collection of R codes or USD (user defined function) available to users anywhere, anytime. R Packages extend the capability of R and helps you achieve much more from this tool beyond the basic functions. This first series tutorial explains how to create your own customized package in R.

Prerequisites:

  • R latest version
  • texlive updated version

Now lets start creating a package in R: 

  • Save your R function with .R extension. Here we are taking a hypothetical name “sample” as a function name, which can be saved as “sample.R”.
  • Copy your R script file (sample.R) in your current working directory.You can see your current working directory with the function “getwd”.

getwd()[1] “/home/amit”    # Current working directory

  • Also if you want to change your current working directory you can use the “setwd” function.setwd(“/home/amit/Article”) # Changing the current working directory from “/home/amit” to “/home/amit/Article”

Running Package.skeleton:

Package.skeleton is a base R function.That means you don’t have to install its package. It has two variables: “name” which defines the name of your package and “code_files” which specifies your source code file(source.R in this case).

library(“utils”)

 package.skeleton(name=”sample”, code_files=”sample.R”)

Package.skeleton creates a folder name “sample” for you which contains these files and folders:

R1

 

 

  • man – It is a folder which contains 2 files :  sample-package.Rd and sample.Rd
  • R  – A folder which contains your R code i.e. sample
  • Description – A text file containing description of the package
  • Namespace – A text file having namespace declarations
  • Read and delete me – A text file containing commands and instructions to build a package

To build a package you need to edit these 4 files:-

  • sample-package.Rd
  • sample.Rd
  • Description
  • Namespace

sample-package.Rd and sample.Rd are documentation files. It provides users a reference on these aspects :

  • Description of the package
  • How to use the functions in the package
  • What kind of arguments a functions can take
  • Some examples for the reference

Both sample-package.Rd and sample.Rd are self explanatory, you can easily fill in the necessary information.

The Description file contains following information by default:

  • Package: sample
  • Type: Package
  • Title: What the package does (short line)
  • Version: 1.0
  • Date: 2013-08-07
  • Author: Who wrote it
  • Maintainer: Who to complain to <yourfault@somewhere.net>
  • Description: More about what it does (maybe more than one line)
  • License: What license is it under?

You need to edit title,author,maintainer,description for the file

If you have any dependency on any R package you can include it in description file by adding a line “Depends”. For example our package “sample” depends on two packages “plyr” and “reshape2”. You can also specify which R version your package works on.

After adding the above information desciption file will now be:

Depends: R (>= 2.9.0), plyr, reshape2

Editing the Namespace File:

Name space file specifies which functions to export from the packages after loading and which packages or dependencies to export. You can exclude import function if you have used Depends in Description file.You can edit the namespace file to specify which function you want to export from your package or made available to user.

For example our package “sample” contains a function “addition”. Then we can us export(addition) function.

So this was the first series about Creating a package in R. Next time, we will learn about checking the package you created for errors and then finally launching it. You can also share your feedback or any tips on R here.

Stay Tuned!


Viewing all articles
Browse latest Browse all 12

Latest Images

Trending Articles





Latest Images