How to Create an R Package

R packages are strong tools in R coding. Whenever we code with R to do some complex data analyzing, R packages will always help. It is cool to make one’s R package to share code with others. I learned how to create an R package in the Data Science class and decided to write the procedure in this blog. I Hope it is helpful for you.
Ready to build your R package? Let’s start!

1. Make a new project in a new directory

In RStudio, go to File » New Project. Select to make a new project in a new directory.

Select the project type as an R Package.

Name the R Package, select the working directory where the R package will be in, and click ‘Create Package’.

Now go to the directory and check out what you have!

You can see the same thing inside of RStudio:

2. Write your functions

After you have created the package, install, and library these two packages:

library(devtools)
library(roxygen2)

Go to File » New File » R Script. In this R Script we will write to create an R file with a function for our package:

# Sample function 

diamond_plot <- function(df, varx, vary){
  ggplot(df, aes(x=get(varx), y=get(vary))) +
  geom_point(shape=23, fill="blue", color="darkred", size=3)
}

Once we are done writing the function, we will save this R Script as diamond_plot.R in the R folder of our package.

Let’s take a look at what this function does.

# Draw sample plot with the function.

df.test <- data.frame(x = rnorm(10),
                      y = rnorm(10))
diamond_plot(df.test, 'x', 'y')

3. Add documentation for your package.

While you have the script diamond_plot.R open, go to Code » Insert Roxygen Skeleton. Make sure your cursor is inside of the function when you do this.

Now you can fill in your documentation.

Go up a level to find the DESCRIPTION file, click on the DESCRIPTION file and edit it.

4. Add Data to your package

Make a data folder.

Save data in this folder using a .RData object.

# Sample data
sample_data <- data.frame(x = rnorm(10),
                          y = rnorm(10),
                          z = rnorm(10))
                          
save(sample_data, file = 'ExamplePackage/data/sample_data.RData')

    

5. Install and Check the Packages

Run the following code in the same working directory as the package.

document()

And then go down one level in the working directory and run the following code.

setwd('..')
install('ExamplePackage')

6. Check what you got!

Type the following commands in the console, and you can see the documentation of your package.

?diamond_plot

data(sample_data)

7. Upload and share your R Package with others.

You can put your package on Github. Make a new repository with the same name as your package, and make it public.

Congratulations! Now you can create your R package, install the package from the Github or give it to a friend to use with the following code.

devtools::install_github('yourusername/ExamplePackage')
Tianran Zhang
Tianran Zhang
A professional data scientist, an unprofessional hiker, cooker, video creator, day dreamer, and life-long learner.

sad().stop(); beAwesome();