1. Data and R Markdown template in an R package
This video will explore how to include
2. R package structure
data and the steps to construct an R Markdown template file in our R package.
3. Create R package skeleton
Let's start with the usethis R package. This package provides a set of utilities designed to automate common setup tasks for R projects. For R packages, this includes creating directories and making files.
We create a package in R using the create_package function from usethis. Our package will be called unitConverter.
create_package sets up a directory structure and generates essential files like DESCRIPTION and NAMESPACE. The output here is condensed to highlight the most important parts.
4. Check that package structure is created
The base R dir function and its default argument for the current working directory (denoted by a period in quotation marks) allows us to examine our working directory structure after executing create_package.
We have created the DESCRIPTION and NAMESPACE files as well as the R directory.
5. Store a CSV as an R object
We'll spend the majority of the course working with our most powerful tools in R: functions. Let's first start smaller by adding data to the package. While data isn't a core component of an R package, adding data to an R package allows for a demonstration of the package's functionalities. It provides a basis for running examples, ensuring the package works as intended.
We'll use a CSV file here, although many different data sources, including Excel files, could be used. We have some temperature data stored as a CSV to make available to package users. This CSV file is stored in the data-raw directory. It is best practice to place all our external data, like CSV files, in this data-raw directory of the package. We store this data as temperature_data using read_csv from the readr package.
6. Peek at the data
Using the base R head function on the data, the first six rows of temperature_data are displayed here. The value column refers to the specific measurement, and the unit column refers to which type of temperature was recorded. On line four, we see 45 degrees Fahrenheit, for example.
Our goal will be to convert between different temperature scales.
7. Loading data into an R package
To add this data to our package after it has been stored as an R object, we run the use_data function with the R object, temperature_data, as its input.
This modifies the DESCRIPTION file to add R as a dependency, makes a data directory, and saves temperature_data as an R data object with the dot-rda extension, making the data available when the unitConverter package is loaded.
8. R Markdown template file in the package
Another R package tool is an R Markdown template file. R Markdown is a file format that combines R code and Markdown to create dynamic reports.
Let's start an R Markdown template using the use_rmarkdown_template function from usethis providing a name as its argument. This creates a file for us to customize, as shown.
It also creates the directory structure for the R package to know this is a template.
9. Edit R Markdown template file
Now that the template file has been created, we add some usage of our package by editing the R Markdown text.
We can update the report title to "Temp Examples" in the metadata at the top. Headings are given with two hash symbols. These headings show what is being done with the code. Then we load the package using library. We can next take a look at the data by printing temperature_data. This R Markdown template file provides the user a sandbox to play in for the unitConverter package, so we could provide as much as we'd like.
10. Let's practice!
Test out your data-adding skills in the exercises.