15 Creating Packages
You can create your own packages, which are basically a collection of functions. You wil need to have installed the devtools package.
15.1 Setup Folders
Create an R Package Project file (File > New Project…> New Directory > R Package). Give a name to your package and the location where all the relevant files will be saved.
Your package folder will contain subfolders ‘R’ and ‘man’ which will contain details of functions. By default these should contain ‘hello.R’ and ‘hello.Rd’ files.
15.2 R folder
Delete the contents of ‘hello.R’ and replace with your own package function(s). For example:
NB. DO NOT use library() or require() in any function files. See the Dependencies section below. Rename the .R file so that it matches the function name.
15.3 man folder
Delete the contents of ‘hello.Rd’ and replace with documentation of your own functions.
A .Rd file is needed for each function that you create. It should have the following format:
\name{ packagefunction }
\alias{ packagefunction }
\title{ A basic function }
\usage{ packagefunction() }
\description{ Multiplies 2 numbers. }
\examples{ packagefunction() }Rename the .Rd file so that it matches the function name.
15.4 Package details
By default a ‘DESCRIPTION’ file should have been created such as the example below. You can update these as neccessary.
Package: kdpackage0
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <yourself@somewhere.net>
Description: More about what it does (maybe more than one line)
Use four spaces when indenting paragraphs within the Description.
License: What license is it under?
Encoding: UTF-8
LazyData: true15.5 Package Dependencies
If your package uses functions from other packages then you must add these to the ‘DESCRIPTION’ file so that they are imported. For example this package will use functions form dplyr and tm (sepearted by commas):
15.6 Build the package
In the menu, click Build > Install & Restart (or Build & Reload). The pacakge should now be installed and available in your list of packages.
15.7 Making changes
If you make chnages to your package functions or documentation, you will need to re-build it again.
http://web.mit.edu/insong/www/pdf/rpackage_instructions.pdf http://tinyheero.github.io/jekyll/update/2015/07/26/making-your-first-R-package.html