Tutorials

How to create your own R package with RStudio [Tutorial]

3 min read

In this tutorial, we will look at the process of creating your own R package. If you are going to create code and put it into production, it’s always a good idea to create a package with version control, examples, and other features. Plus, with RStudio, it is easy to do. So, we will use a simple example with one small function to show how easy it is.

This tutorial is an excerpt taken from the book Mastering Machine Learning with R – Third Edition written by Cory Lesmeister. The book explores expert techniques for solving data analytics and covers machine learning challenges that can help you gain insights from complex projects and power up your applications.

Before getting started, you will need to load two packages:

> install.packages("roxygen2")
> install.packages("devtools")

You now want to open File in RStudio and select New Project, which will put you at this point:

Select a new directory as desired, and specify R Package, as shown in the following screenshot:

You will now name your package – I’ve innovatively called this one package – and select Create Project:

Go to your Files tab in RStudio and you should see several files populated like this:

Notice the folder called R. That is where we will put the R functions for our package. But first, click on Description and fill it out accordingly, and save it. Here is my version, which will be a function to code all missing values in a dataframe to zero:

I’ve left imports and suggests blank. This is where you would load other packages, such as tidyverse or caret. Now, open up the hello.R function in the R folder, and delete all of it. The following format will work nicely:

  • Title: Your package title of course
  • Description: A brief description
  • Param: The parameters for that function; the arguments
  • Return: The values returned
  • Examples: Provide any examples of how to use the function
  • Export: Here, write the function you desire

Here is the function for our purposes, which just turns all NAs to zero:

You will now go to BuildConfigure Build Tools and you should end up here:

Click the checkmark for Generate documentation with Roxygen. Doing so will create this popup, which you can close and hit OK. You probably want to rename your function now from hello.R to something relevant. Now comes the moment of truth to build your package. Do this by clicking BuildClean and Rebuild.

Now you can search for your package, and it should appear:

Click on it and go through the documentation:

There you have it, a useless package, but think of what you can do by packaging your own or your favorite functions, and anyone who inherits your code will thank you.

In this tutorial, we went through the process of creating an R package, which can help you and your team put your code into production. We created one user-defined function for our package, but your only limit is your imagination. I hope you’ve enjoyed it and can implement the methods in here, as well as other methods you learn over time. If you want to learn other concepts of Machine Learning with R, be sure to check out the book Mastering Machine Learning with R – Third Edition.

Read Next

How to make machine learning based recommendations using Julia [Tutorial]

The rise of machine learning in the investment industry

GitHub Octoverse: top machine learning packages, languages, and projects of 2018

Natasha Mathur

Tech writer at the Packt Hub. Dreamer, book nerd, lover of scented candles, karaoke, and Gilmore Girls.

Share
Published by
Natasha Mathur

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago