Create R package – Rstudio, github, devtools

July 22, 2013
By

(This article was first published on R (en) - Analytik dat, and kindly contributed to R-bloggers)

If you are going to create your first package in R, there is common set of tools you will probably use - Rstudio, devtools package and github. You don't have to, but it will save you a lot of time and your code wil be versioned and better understandable by others. This short tutorial will show you how to start new R package on github.

Assumptions

I assume you have some basic knowledge about R and version control. This is why you decided to create your own package, right? Before we start, there are few assumptions I have:

  • you have installed Rstudio
  • you have Installed devtools() package
  • you have github account

Creating package skeleton

(Note: Press ENTER after each command. Replace red text with your own values)

  1. Create empty repository on github (I will use name rpackage in this example)
  2. Start Rstudio and load devtools package: library(devtools
  3. Create package locally using devtools: create("rpackage ") (this will create rpackage folder in your working directory)
  4. Create new project in RStudio (Create project from: Existing directory) and choose rpackage directory
  5. In RStudio go to Git/More/Shell and type: git init
  6. Reopen the project (this will refresh the Git tab in Rstudio)
  7. Start Git/More/Shell and type:
git add *
git commit -m "first commit"
git remote add origin [email protected]:username/rpackage.git
git push -u origin master

Then you can refresh repository on github and you should see files created by devtools. You have your package on github now! If you wish, you can delete your local data. Next time, you can checkout your package from Rstudio (Project/Create Project - Checkout project from version control repository).

Add code

You should save your R code in R folder. This is a minimalistic function example:

#' Print hello
#'
#' This function prints hello
#'
#' @param fname First name
#' @param lname Last name
#' @export
#' @examples
#' hello(fname="Tomas",lname="Greif")
#' hello(fname="Your",lname="Name")

hello <- function(fname, lname) {
cat(paste("Hello",fname,lname,"!"))
}

Change DESCRIPTION file

devtools() will create DESCRIPTION file in root folder of your package. In order to design valid package, this is a minimalistic DESCRIPTION file - you will have to add Author, Maintainer and License manually:

Package: scoring
Title:
Description:
Version: 0.1
Author: Tomas Greif
Maintainer: Tomas Greif <[email protected]>
Depends: R (>= 3.0.1)
License: >GPL-2
LazyData: true

Now you can run check() command to test whether your package is valid (meaning you can send this package to CRAN if you wish to).

This tutorial described only the basics. If you need more details, you should visit:

 

To leave a comment for the author, please follow the link and comment on his blog: R (en) - Analytik dat.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.