Creating a Package with RStudio

Introduction

In this post I will be walking through the steps I used to create a package with R Studio. All my resources are linked below.

Background

The package I am planning to make is a distance package that will have variations of two functions. One function will calculate the Euclidean distance (Read more here), and the other will calculate the Taxicab distance (Read more here). Below Figure 1 (from the Taxicab distance link) shows both the Euclidean and Taxicab calculations for \(\overline{AB}\) of \(\triangle ABC\).

Distance from A to B on a traingle in Euclidean Distance and Taxicab Distance

Figure 1: Distance from A to B on a traingle in Euclidean Distance and Taxicab Distance

Using this example, my distance package should be able to preform the following code.

### Euclidean Distance 
distance_E <- function(x1,x2,y1,y2){
  sqrt((x2-x1)^2+(y2-y1)^2)
}
### Taxicab Distance
distance_T <- function(x1,x2,y1,y2){
  abs(x2-x1)+abs(y2-y1)
}
### Example 
x1 <- 2
x2 <- 8
y1 <- 5
y2 <- 1
distance_E(x1,x2,y1,y2)
## [1] 7.211103
distance_T(x1,x2,y1,y2)
## [1] 10

Dang, that is pretty tedious entering in each point like that. Not sure how much time that will save us in the long run so lets create a couple more functions. That way we can put in A and B as points instead.

### Euclidean xy Distance 
distance_Eab <- function(A,B){
  sqrt((B[1]-A[1])^2+(B[2]-A[2])^2)
}
### Taxicab xy Distance
distance_Tab <- function(A,B){
  abs(B[1]-A[1])+abs(B[2]-A[2])
}
### Example 
A <- c(2,5)
B <- c(8,1)
distance_Eab(A,B)
## [1] 7.211103
distance_Tab(A,B)
## [1] 10

Lastly, just because, lets create two more functions where we can put in a single line.

### Euclidean xy Distance 
distance_Eline <- function(line){
  sqrt((line[3]-line[1])^2+(line[4]-line[2])^2)
}
### Taxicab xy Distance
distance_Tline <- function(line){
  abs(line[3]-line[1])+abs(line[4]-line[2])
}
### Example
line <- c(A,B)
distance_Eline(line)
## [1] 7.211103
distance_Tline(line)
## [1] 10

Alrighty, now let’s jump into building this package.

Create the GitHub Repository

Next create a GitHub Repository, but do not initialize anything withing the repository. See Figure 2 below.

How the create a new repository page will look before you create it.

Figure 2: How the create a new repository page will look before you create it.