README

Anna Guo 2024-03-25

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 5: Add vignettes

devtools::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: Install the package

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