This lab will extend functions to create an R package. First we will do a quick demo and create your first R package.
A comprehensive overview (free online book) about creating R packages is available here: R Packages
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):
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))
}
use_r()
#| eval: FALSE
use_r("get_candy")
This will create an R file with the specified name.
load_all()
and check()
Use load_all()
for a quick test it the function will work. Make sure that the function you are accessing is from the package and not the global enviroinment: exists("get_candy", where = globalenv(), inherits = FALSE)
Then run check()
to verify that the package has all the necessary parts to load.
Update the code description file
specify a license use_mit_license()
Open your R file, do Code > Insert roxygen skeleton. A very special comment should appear above your function, in which each line begins with #’.
After updating your text, run document()
which will pull in the documentation
Finally, check()
and install()
will activate your package.
You can now run libary(STAT408)
to use your package.
Show that you can load the R package and run the get_candy
function.
library(STAT408)
get_candy(5)
What happens when you type ?get_candy
into the console?
Let’s assume now that the number of pieces from each candy type are not equal. Use this image 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
Add this new function to your R package. Note to use other functions inside your function, you’ll need to do two things:
run use_package('dplyr')
or similar to identify the package the functions comes from
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)
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.