R for Data Analytics – Creating packages with devtools

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

Loader Loading...
EAD Logo Taking too long?

Reload Reload document
| Open Open in new tab

Download PDF [238.24 KB]

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!