Module 10: Building your own R package
This week we begin to dive into the procedures needed to create an official R package. Our assignment this week was to create a package in R and create a DESCRIPTION File.
Overall I feel that with Devtools and RStudios, creating a package is very streamlined and user friendly. I'm looking forward to creating our package for our class!
Let us begin!:
First thing first, we will be heavily reliant on two main packages:
1. devtools
2. roxygen2
devtools will provide us with most of the functions needed in order to create a package and roxygen2 is a great package that streamlines many of the documentations needed in packages for R.
We simply have to install the packages:
>install.packages("devtools")
>library("devtools")
>install.packages("roxygen2")
>library*"roxygen2")
Unfortunately for me there was an error in my "backports", that prevented me from installing and using devtools correctly. Upon searching online I found a way to reinstall the backports package and install devtools again. This fixed my issue:
>install.packages("backports"); install.packages("devtools")
Once we have these installed there are a few ways we can create a new package.
The easiest way was by using Rstudios clicking on the project button in the top right hand corner and creating a new project with the project type being a package.
What is great is this automatically creates a directory for you with the files and folders that are standard in most packages
The project button
Automatically generated files and folders for the project
While everything is automatically generated, some things to keep in mind is that many unnecessary and placeholder items are generated as well. For instance, a built in place holder function is generated called "hello.R" that just demonstrates the universally recognized "hello world" example. This is obviously unnecessary and if left in the package may look funny to other that use the package. This can easily be fixed by simply removing the files from the package (note that a hello.rd will also be in the man file as well).
The auto generated DESCRIPTION file will also have to be edit as well. By default the description file looks like this:
As you can see there are many default values that simply explain what should be entered and not actually useful. Also certain descriptions are missing, for instance there is no Depends section which is fairly standard in most packages today. This is not too difficult, all one has to do is add it to their file, which can easily be done in Rstudios.
I simply followed the instructions from Friedrich Leisch and Hadley Wickham to fill out a mock DESCRIPTION file. I also found it useful to open up a package I have installed and look at their DESCRIPTION file as refence. One that I found was the file for pkgbuild.
Here I get a concrete example of how the authors section should be formatted (persons format). Which helped with some of the ambiguities when simply reading instructions.
Alternative ways to start a package:
While the method above is a quick and simple way to create a package, it relies on being in Rstudios. We may not always have that luxury and so I decided to explore other methods.
One method that comes to mind is using devtools' create() function.
This is best highlighted in Hilary Parker's blog: https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/
With the create function we simply need to just load up devtools and assign the name of our package within the create() argument.
For instance if we want to name our package "ThePackage" we would simply use the following lines:
>library(devtools)
>library(roxygen2)
>create("ThePackage")
devtools will automatically create a DESCRIPTION file and an R folder for your R code. We will need to create a man file manually.
The result of using create() to make a package.
Overall I feel that with Devtools and RStudios, creating a package is very streamlined and user friendly. I'm looking forward to creating our package for our class!
-Anthony
Comments
Post a Comment