Lab9

Creating an R Package Overview

Demo Overview:

  1. Create an R package
  2. Add R function to the R package
  3. Load and check
  4. Code Description
  5. R Oxygen documentation
  6. Check and Install

1. Create a new package using devtools::create_package()

Use a path inside the create_package() function or set the working directly to where you’d like the function files to live.

library(devtools)
create_package('STAT408')

Note this will actually create a bundle of files known as an R project and likely open that project in a new R studio window. That R project will contain the following files and directories (as well as a few others you can ignore):

2. Add R function to the R package.

We are going to add your candy function from Tuesday into the R package. This will require working inside your R Insert your candy function from Tuesday. This is a two step process:

get_candy <- function(num_candy){
  # A function to randomly select candy from a bag consisting of Reeses, Snickers, M&Ms, and Kit Kats
  # Args: num_candy - the number of candy pieces to select
  # Returns: vector of candy bar names with length equal to num_candy
  if(!is.numeric(num_candy)) {
    stop('Please enter a numeric value for number of candy')
  }
  if(num_candy > 5){
    warning("Sugar Rush! Are you sure you want to do that?")
  }
  return(sample(c("Reese's", "Snickers", "m&ms", "Kit Kats"), num_candy, replace = T))
}

#| eval: FALSE

use_r("get_candy")

This will create an R file with the specified name.

3. Test function with load_all() and check()

4. Code Description

5. Documentation

6. check() & install()


Question 1 (3 points)

Show that you can load the R package and run the get_candy function.

library(STAT408)
get_candy(5)

Question 2 (2 points)

What happens when you type ?get_candy into the console?

Question 3 (5 points)

Let’s assume now that the number of pieces from each candy type are not equal. Use this image candy.png to identify the candy types and candy amounts in the candy basket.

Write a function to draw candy from this basket. Use at least one tidyverse funtion inside your function, such as tibble or sample_n

Question 4 (5 points)

Add this new function to your R package. Note to use other functions inside your function, you’ll need to do two things:

  1. run use_package('dplyr') or similar to identify the package the functions comes from

  2. refer to functions with the following convention dplyr::sample_n()

Re-install your package and run your function here. Make sure you are accessing the version of the function from the package using the same convention by calling STAT408::trickRtreat(4)

#| eval: FALSE
STAT408::trickRtreat(4)

Question 5 (5 points)

We’ve not talked formally about pie charts in this class, but they should be avoided. Pie charts make it very difficult to compare different categories (something much easier to do with bar charts).

Use your trickRtreat function and sample 100 candies. Then create a bar chart to show the outcome.