Skip to Content
Course content

68: Package Structure and DESCRIPTION File

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've seen this play out a dozen times: a researcher or a data analyst spends months building a brilliant set of tools for a project—let's say, a system for analyzing urban air quality sensors—and then they "share" it by sending a ZIP file containing five different .R scripts and a README that says, "Source these files in this specific order."

The Fragility of the "Source-All" Approach

When you start out, the naive way to organize your code is just to group related functions into scripts. You might have load_sensors.R, calculate_aqi.R, and plot_trends.R. To use them, you just call source("load_sensors.R") at the top of your analysis script. It feels simple enough at first, but it's a ticking time bomb.

The problem is that source() just dumps everything into your global environment. If load_sensors.R defines a variable called temp_data and calculate_aqi.R also uses a variable called temp_data for something entirely different, you've just introduced a silent bug that's nearly impossible to track down. Plus, you're relying on the user to manually call library(dplyr) or library(ggplot2) in the right order before sourcing your scripts. I can't tell you how many hours I've wasted debugging someone's "toolset" only to find out they forgot to tell me they needed a specific version of a helper package installed.

Moving to a Formal Package Structure

The better way—the professional way—is to wrap your logic into a formal R package. This isn't just for things you intend to publish on CRAN; it's for any project that needs to be reproducible. In a package, you don't source() anything. Instead, all your functions live in the R/ directory. When the package is loaded, R handles the namespaces for you, ensuring that your functions are organized and isolated from the user's global environment.

A standard structure looks like this:

aqi_analyzer/
├── DESCRIPTION
├── NAMESPACE
├── R/
│   ├── load_sensors.R
│   ├── calculate_aqi.R
│   └── plot_trends.R
└── man/
    └── (documentation files)

By moving your code into the R/ folder, you're telling R: "These are my tools." You no longer worry about the order in which files are read. R parses all the files in that directory, figures out the dependencies between your functions, and makes them available as a cohesive unit.

The DESCRIPTION File as a Contract

The real magic, however, happens in the DESCRIPTION file. If the R/ folder is where the work happens, the DESCRIPTION file is the contract. In the naive approach, you might put library(tidyverse) at the top of every script. But that's reactive; the code fails after the user tries to run it.

In a package, you list your dependencies under Imports in the DESCRIPTION file. For our air quality tool, it would look something like this:

Package: aqi_analyzer
Type: Package
Title: Urban Air Quality Sensor Analysis
Version: 0.1.0
Imports: 
    dplyr (>= 1.0.0),
    ggplot2,
    readr

This changes everything. Now, when someone installs your package, R doesn't just hope the dependencies are there—it checks the DESCRIPTION file and installs the required versions automatically. It transforms your code from a "collection of scripts" into a "software product." I've found that spending an extra ten minutes setting up this metadata saves me days of support emails asking why a function is returning Error: could not find function "filter".




📋 Practical Task

Converting the "Sensor-Tool" Scripts to a Package Metadata File

You have been handed a folder of scripts for a project called wind_speed_metrics. The current developer has been using source() and manual library() calls. Your task is to create the DESCRIPTION file that would turn this into a proper package.

The project requires the following:

  • The package name is wind_speed_metrics.
  • It is currently at version 0.0.1.
  • The code relies on dplyr (any version) and lubridate (must be version 1.7 or higher).
  • The title of the package should be "Wind Speed Analysis Utilities".

Write the full content of the DESCRIPTION file following the standard R package format. Ensure the indentation for the Imports section is correct, as R is picky about the leading spaces for multi-line dependencies.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.