R for Data Analytics – Creating packages with devtools
Introduction
The R programming language boasts a vast ecosystem of packages that extend its functionality, making it an invaluable tool for data analysts and statisticians. As an R user, you may eventually want to create your own package to share your work with the community, streamline your workflow, or improve collaboration. This article will guide you through the process of creating packages in R using the devtools package, a powerful suite of tools designed to make package development efficient and straightforward.
Installing and Loading devtools
To create your own package, first, you need to install and load the devtools package. If you haven’t already installed devtools, run the following command in your R console:
install.packages("devtools")
Then, load the package:
library(devtools)
Creating a Package Structure
To start building your package, use the create() function from devtools. This function generates the necessary files and directory structure for your package. Replace “MyPackage” with your desired package name:
create("MyPackage")
The create() function will generate a new directory named “MyPackage” containing essential files and directories, such as R/, man/, DESCRIPTION, and NAMESPACE. The R/ directory is where you’ll store your package’s R code, while the man/ directory holds the documentation files.
Adding Functions to Your Package
Place the R script files containing your functions in the R/ directory. Make sure to include a descriptive comment for each function, as this will be used to generate the documentation files.
Writing Package Documentation
To document your package and its functions, you can use Roxygen2, a popular documentation system integrated with devtools. Install and load Roxygen2 with the following commands:
install.packages("roxygen2") library(roxygen2)
Now, add Roxygen2 comments above each function in your R script files, following the Roxygen2 syntax. For example:
#' Add two numbers together
#'
#' @param x A number.
#' @param y A number.
#' @return The sum of \code{x} and \code{y}.
#' @examples #' add(1, 2)
#' add(3.14, 2.7) add <- function(x, y) { x + y }
Generating Documentation Files
Once you have documented your functions using Roxygen2, use the document() function from devtools to generate the corresponding .Rd files in the man/ directory:
document("MyPackage")
Adding Package Metadata
Edit the DESCRIPTION file to include information about your package, such as the package title, author, and dependencies. A minimal example of a DESCRIPTION file looks like this:
Package: MyPackage
Title: A Package for Adding Numbers
Version: 0.1.0
Authors@R: c(person("John", "Doe", email = "john.doe@example.com", role = c("aut", "cre")))
Description: A simple package for adding numbers together.
License: GPL-3
Encoding: UTF-8
LazyData: true
Imports: dplyr, ggplot2
Checking and Building Your Package
Before sharing your package, it’s important to check and build it to ensure it works correctly and complies with CRAN policies. Use the check() and build() functions from devtools:
check("MyPackage") build("MyPackage")
If the check() function reports any errors or warnings, address them before proceeding.
Sharing Your Package
You can now share your package with others. There are several ways to distribute your package, including:
Submitting to CRAN: If you want your package to be publicly available through the official R package repository, follow the CRAN submission guidelines (https://cran.r-project.org/web/packages/policies.html) and submit your package using the submit_cran() function from devtools:
submit_cran("MyPackage")
Sharing on GitHub: You can also host your package on GitHub, a popular platform for sharing code and collaborating on projects. First, create a new repository on GitHub, then initialize a local Git repository in your package directory, and push your package to the remote repository:
cd MyPackage
git init
git remote
add origin https://github.com/yourusername/MyPackage.git
git add .
git commit -m "Initial package commit"
git push -u origin master
To install your package from GitHub, users can use the install_github() function from devtools:
install_github("yourusername/MyPackage")
Conclusion
Creating packages in R using the devtools package is a powerful way to share your work, enhance collaboration, and streamline your data analytics workflow. By following the steps outlined in this article, you can create your own custom R package and contribute to the thriving R ecosystem.
Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist
R for Data Analytics – Creating packages with devtools
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.