This is more like a summary note of the two tutorials listed in the references section.

This is a test for making R packages.

Step 0: Packages required

library("devtools")
library("roxygen2")

Step 1: Create your package directory

Travel to the parent folder where you want to create your package. Then run the following code:

setwd("parent_directory")
create("test.package")

Step 2: Create a function

# A function for cat lovers
# Original code from https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/

cat_function <- function(love=TRUE){
  if(love==TRUE){
    print("I love cats!")
    print(combn(c('lover','love','cat'), 2, paste, collapse = " "))
  }
  else {
    print("I am not a cool person.")
    print(combn(c('not','cool'), 2, paste, collapse = " "))
  }
}

Step 3: Add package dependencies

Given that cat_function() uses the combn() function, we need to add the itertools package as a dependency. This can be achieved by add the following line to the DESCRIPTION file:

Imports:
    itertools

Step 4: Add documentation

To document the function, add the following commented lines on top of the cat_function().

#' A Cat Function
#'
#' This function allows you to express your love of cats.
#' @param love Do you love cats? Defaults to TRUE.
#' @keywords cats
#' @export
#' @examples
#' cat_function()
#' 
cat_function <- function(love=TRUE){
  if(love==TRUE){
    print("I love cats!")
    print(combn(c('lover','love','cat'), 2, paste, collapse = " "))
  }
  else {
    print("I am not a cool person.")
    print(combn(c('not','cool'), 2, paste, collapse = " "))
  }
}

Once the documentation is added, run devtools::document() to generate the documentation files cat_function.Rd.

Step 4 (optional): Include a binary data

Sometimes we may like to include binary data in the package such that the data is readily available to the users once they installed the package. The data can be used for testing out the functions in the package or work as an example for the format of the data that the functions accept. To do this, we can create a R script say data.R, within wich we include the scripts used to generate the binary data. For example,

testdata <- data.frame(x=rnorm(10,0,1), y=rbinom(10,1,0.5))

devtools::use_data(testdata)

We first generate the binary data called testdata. Then we use the use_data() function to include the data in the package. This will create a data folder in the package directory and store the binary data called testdata.rda in the data folder.

We may also want to make the script data.R accessible to the users. To do this, we can create a folder called data-raw in the package directory and store the data.R script in the data-raw folder. If we don’t want to include the data.R script when building the package, we can add the following line to the .Rbuildignore file:

^data-raw$

It’s a good practice to document the binary data generated above such that when users explore the package with the data, they know what each variable mean. To document the data, we can create an R script named document_data.R under the R folder. Within the script, we can include the following

#' This is a toy dataset
#'
#'
#' @format A data frame with 10 rows and 2 variables: x, y.
#' \describe{
#'  \item{x}{a variable that follows normal distribution}
#'  \item{y}{a binary variable}
#'  }

#' @name testdata
#' @docType data
#' @keywords data
"testdata"

Step 5: Add vignettes

usethis::use_vignette("introduction")

Step 5 (optional): Add a github readme file

Create a README.Rmd file in the package directory such that there will be a readme frontpage on github. An example yaml for the README.Rmd file is as follows:

---
title: "README"
author: "Anna Guo"
date: "`r Sys.Date()`"
output: 
  github_document:
    toc: true
    number_sections: false
---

The code below sets global options for the README.Rmd file.

library(knitr)
opts_chunk$set(warning = FALSE, message = FALSE, eval=F)

Step 6: Check and install the package

Before building the package, it’s a good practice to check if there is any error or warning associated with the package. To do this, we can use the check() function

devtools::check()

Upon addressing all the errors, we can install the package via

devtools::install()

OR make the package folder a git repository and install the package using the following code:

git init # initialize a local git repository
git add . # add all files to the repository
git commit -m "First commit" # commit the changes

# create an empty repository on GitHub and copy the ssh address
# run this line of code to connect local git repo the remote GitHub repo
git remote add origin git@github.com:<github_username>/<github_repo_name>.git # make change here!!!!!
git push origin main # push local main to origin

Once the above is done. The package can be download via

devtools::install_github("<github_username>/<github_repo_name>")

Step 7: Test the package

This is a short tutorial on how to use the test.package package.

library(test.package)
cat_function(love=T)
## [1] "I love cats!"
## [1] "lover love" "lover cat"  "love cat"

References