To create a package in R, follow these steps:
Create a new directory for your package. You can name it whatever you like, but for this example, we'll call it "mypackage".
Inside this directory, create a set of sub-directories according to the following structure:
main.r27 chars4 lines
The R/
directory will contain your package's R code, and the man/
directory will contain the documentation for your package.
Write your functions in R code and store them in the R/
directory. Name your functions with a .R
extension.
Create documentation for your functions using the Roxygen2 package. Roxygen2 is a package that makes it easy to write documentation directly in-line with your code. To use it, you need to start your function definitions with a special comment block, like this:
main.r371 chars14 lines
Run the devtools::document()
function to generate documentation files for your package in the man/
directory.
Create an DESCRIPTION
file in the main package directory. This file contains metadata about your package, such as its name, version, and author information. Here's an example DESCRIPTION
file:
main.r260 chars9 lines
devtools::build()
function to build your package into a compressed file (mypackage_0.1.0.tar.gz
). This file can be shared and installed on other computers.Congratulations, you've now created your own R package!
gistlibby LogSnag