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.
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:
If
cache_dir
provided:Attempts to read cached department data
On cache miss, proceeds to API fetch
API fetch (when needed):
Requests department list from ExploreCourses API
Caches response if
cache_dir
provided
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
fetch_department_courses()
for fetching courses for a specific departmentprocess_departments_xml()
for details on XML processingread_xml_cache()
andwrite_xml_cache()
for cache operations
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
} # }