An R package containing miscellaneous functions that I use all the time.
This is my first attempt to create an R package after many shameful years spent cutting and pasting. This is still a work in progress since I'm still a beginner at creating R packages, but it's not as difficult as I thought it would be.
I was inspired to try this out after reading a post on Hilary Parker's blog. Two other valuable resources for R package creation are Hadley Wickham's Advanced R Programming and the documentation for roxygen2. One of these days I'm going to get around to reading the definitive documentation contained in Writing R Extensions.
The basic workflow for creating and updating a package is as follows. If they're not installed already, install the packages devtools
and roxygen2
and then load them
install.packages("devtools")
library("devtools")
devtools::install_github("klutometis/roxygen")
library(roxygen2)
To create a new package in a new directory do the following:
setwd("parent_directory")
create("your_package_here")
This creates a number of files and directories. One of them is the R directory. Put your R code here, and see the roxygen2
help files for how to add documentation comments. To process the documentation, go to the package directory and run the command document()
setwd("./your_package_here")
document()
To install the package, go the the parent directory and use the install
command
setwd("parent_directory")
install("your_package_here")
To install directly from a Github repo, do the following:
install_github("your_package_here", "github_username")
For example, to install frankmisc
make sure that devtools
is loaded and then run
install_github("frankmisc", "fditraglia")
Finally, use library
as per usual to load the package.
library(frankmisc)
I initially failed to include an @export
statement in the roxygen2
documentation comments. Without this, everything still worked when installed locally, but when I tried to install from Github I could only view the documentation: the functions themselves couldn't be found.
Later on, I encountered this error when trying to view documentation after installing from Github
Error in fetch(key) : lazy-load database '�' is corrupt
It seems that the solution is simply to restart R. According to Hadley "you'll need to restart R after invocations of install
."
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.