Skip to contents

Retrieves a list of all departments from Stanford ExploreCourses, either from cache if available or from the API. The function handles caching automatically when a cache directory is specified.

Usage

fetch_departments(cache_dir = NULL)

Arguments

cache_dir

Character string. Optional path to cache directory. If provided, the function will:

  • Try to read existing department data from cache

  • Cache fresh API data when cache miss occurs If NULL, always fetches fresh data from the API.

Value

A tibble (data frame) containing department information with columns:

  • name: Department code (e.g., "CS")

  • longname: Full department name (e.g., "Computer Science")

  • school: School name (e.g., "School of Engineering")

Details

The function follows this process:

  1. If cache_dir provided:

    • Attempts to read cached department data

    • On cache miss, proceeds to API fetch

  2. API fetch (when needed):

    • Requests department list from ExploreCourses API

    • Caches response if cache_dir provided

  3. Data processing:

    • Parses XML response into structured data frame

    • Organizes departments by school

Error Handling

  • If API request fails or returns invalid data. Includes specific error message and any relevant details.

  • If cache operations fail when cache_dir is provided. Includes cache path and specific error message.

See also

Examples

if (FALSE) { # \dontrun{
# Fetch fresh data from API (no caching)
departments <- fetch_departments()

# Fetch with caching enabled
departments <- fetch_departments(cache_dir = "course_cache")

# Example data processing
# Find all engineering departments
eng_deps <- subset(departments, school == "School of Engineering")

# Get department codes
dept_codes <- departments$name
} # }