How to make a package in R

Hi everyone!!! Last week I learned a new thing, how to make packages in R. It is very easy, and all it needs is few hours of decent effort, and voila, you get your own package. So, let’s begin.

Why make a package?

First of all, let me convince you the utility of making a R package. Ever written a code long time back, and now that you suddenly need it, you cannot find it anywhere? Ever been frustrated to copy paste the same R code again and again? Well, there you go. Take some time to learn how to make a package, and all you need to do later is install your package, and you are saved of all the hassles. Well, also there is the fact that if you wish to publish a new methodology in a well respected journal, the chances of publication goes up if you have a R package of your own methods. So, let’s indulge ourselves in the process.


Basics: In this article, I will show to how to make a R package using RStudio. So, I first need you to install the RStudio, if you already haven’t done so. You can download it for free from this link.

Now that you have RSudio installed in your computer, let us create an empty package at first.

For this, we will go to the top right corner of the RStudio and click on Project, and then New Project. 


Then we will click: New Package -> R Package.



Now, we will create the name and folder for our R package.

We will supply the following things:

  • Name of the R Package, which in our case will be myPack
  • Location in your computer where you want the package to be saved.
  • An option to create a git repository. If you don’t know about github, don’t worry!!! It’s not essential for creating a R package. Let’s keep it unchecked for the moment.

Now click Create Project.


Inside the package: Now that we have created a R package, we have opened the following window of RSTudio.

Let’s see each aspects one by one:

  • The circled area is the RScript, where we write the R codes. RStudio creates a sample Hello.R R code when creating the package. I will delete this code and write a new code to show how it works.
  • The top right corner gives the Environment, History and Build sections. Of these the Build section is really important, as it helps to build the R package once we have written all the codes.
  • The lower right corner has the files and folders that were created automatically inside the R folder package. Let’s see what they denote:
    Description: This is a text file which contains the description of your folder. I will shortly explain what one should write in the description file. Without a description file, a package cannot be installed.
    Man: This folder contains some .rd files which are basically documentation of the R codes you have written. I will show you how to create the documentations.
    R: This folder contains all the .R codes that you will include in your R Package.

So, let’s start writing a R codes in the package.

Suppose, we want to check whether a matrix is symmetric or not. Let’s write the following code in RStudio for that.

As you can see, I have create a new R script, and have written a code there. However, now we have to document the code, mentioning how to use this code, what should be the input, an example, etc. This is what will show when we give, say, ?is_symmetric, once we install the package.

Let me write the whole code outside once more:

#’ Checking Symmetry of a matrix.
#’ @param A: a matrix input
#’ @return A binary value (0 or 1), with 1 specifying that the matrix is symmetric
#’ @examples
#’ a=matrix(rnorm(1:10^2),10,10)
#’ is_symmetric(a)
#’ b=a+t(a)
#’ is_symmetric(a)
#’ @export


Now, it’s time to edit the Description file. Here while creating the package, RStudio already gives a template. We just need to modify the names, and voila, we are done.

The only thing worth mentioning in the Description file is the Depends Parameter. It will help you to install any package you will need in any R codes inside your package, and will make it available so your code don’t run into an error. As an example, I have given MASS and Matrix package, you can add whatever you want.


Building the package:

Now comes the final step, building the package. For this, there is a Build tool which I showed earlier, clicking which the package will be built. But sometimes, the .rd files are not generated since the roxygen2 will not be called. To do it correctly, I would recommend the following steps:

  • First write in the console: library(devtools). If devtools is not installed, install it first.
  • Go to Build tab at the top of the Rstudio.
  • Then got Configure Build Tools.
  • Check the option Generate Documentation with roxygen.
  • Go to configure at the side of it and check all the options. Usually all the options will be checked except Build and Reload, check it also.
  • Now click OK.
  • Now click build and reload.


Now, we have got all we need. You can go to the Man folder and see that the corresponding .rd files have been created for all the codes.

You can go to the package section and you will see that your package now appears there, with all the functions, and their corresponding documentations.










Congratulations!!! You have created your first R Package. Now you can share it with anyone and others can use your code as well. One popular method of sharing a package is to use Github. It’s pretty simple to learn, and I would recommend you to learn how to use Github. Next, I will talk about Unit Testing, a method used by coders to check the correctness of their codes efficiently. Most packages are expected to include a test file indicating the correctness of the codes in the package.

Leave a comment

Your email address will not be published. Required fields are marked *