load_data <- function(){
readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2024/2024-08-06/olympics.csv')
}Project Checkpoint 8
This week, your goal is to create a minimal working package, that we will build on in the remainder of the course until you have a fully fleshed out data analysis package!
Note: Each team member should have their own copy of the package directory on GitHub. This can be a separate copy, or if you prefer, you can fork a teammate’s directory.
Name your package
The first thing you’ll need is a name for your package. Get creative with this! There are a few common package styles:
“old school” R tradition is to use a capital R in the package, e.g.
RMySQLIn the early tidyverse days, packages were all lowercase and ended in an r, such as
dplyrortidyr.More recently, folks often go for one-word puns that describe the package purpose, like
lubridateorforcats.
You can do whatever you’d like with this - as long as the package has no special characters, does not start with a number, and relates in some way to the actual functionality or data context of the package.
Prepare the empty package structure
Once you have a name,
Run
usethis::create_package("path/to/package/packagename")to make your new package repository. (Of course, replace the path with your own computer location, and the packagename with your chosen name!)Open the
DESCRIPTIONfile, and update theTitle,Authors, andDescriptionsections only.Run
library(usethis)in your console. Then runuse_r("load_data").In the
load_data.Rfile that opens, write a function calledload_datato load your original dataset into the package from the Tidy Tuesday link or using thetidytuesdayRpackage. It will probably look something like:
- Use
roxygen2documentation to document theload_datafunction. Don’t forget to document dependencies on other packages, including for example:
Use
@importFrom readr read_csvto specify the package and function(s) your function is dependent onDirectly reference the package that the function you are using lives in:
readr::read_csv()This will add an
importFrom(readr,read_csv)line in theNAMESPACEfile!
- Run
use_packages("readr")to include the package as an imported dependency. This will add a line (Imports: readr) in theDESCRIPTION!
You do not have to write any testthat tests at this stage!
Make a plan
When designing a package, it is helpful to write down “aspirational” code - lines of code that you wish would work, that you can build towards as you develop your functions.
Think about the code in your Projects 1 and 2. Which parts do you wish you had a shortcut function for?
Run use_this::use_vignette("main-functions"). Then, open the new folder named vignette in your package folder. You should see a file called main-functions.Rmd. Change the name to main-functions.qmd. Open this quarto file.
In main-functions.qmd, write three code chunks with functions that you plan to make, and a brief description of what they do. For example,
plot_olympic_golds(olympic_data, "United States", min_year = 2000, by_gender = TRUE)This should make a line plot showing how many gold medals were won by the United States every year since 2000. There should be one line for the Men’s category and one for the Women’s category.
At least one of these should be a plot and at least one should be a summary calculation.
Build It!
Even though your package has nothing in it yet, make sure to build (Ctrl/Cmd-shift-B) it before you push to GitHub.