Retrieves all courses for a specified department from Stanford ExploreCourses, either from cache if available or from the API. Supports fetching courses for specific academic years and handles caching automatically when a cache directory is specified.
Arguments
- name
Character string. Department code (e.g.,
"CS"
). Must be a single, non-empty string. If multiple codes provided, only the first is used.- year
Character string. Optional academic year in
YYYYYYYY
format (e.g.,"20232024"
). IfNULL
, fetches data for the current academic year.- cache_dir
Character string. Optional path to cache directory. If provided, the function will:
Try to read existing course 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 course information with columns:
objectID
: Unique course identifieryear
: Academic yearsubject
: Department codecode
: Course numbertitle
: Course titledescription
: Course descriptionunits_min
: Minimum unitsunits_max
: Maximum unitsdepartment
: Department code of the courseterm
: Academic term (e.g., "Autumn", "Winter")term_id
: Numeric term identifiersection_number
: Section identifiercomponent
: Course component type (e.g., "LEC", "DIS", "LAB")class_id
: Unique identifier for the sectioncurrent_class_size
: Current enrollmentmax_class_size
: Maximum enrollment capacitySchedule information:
days
: Days of the week (e.g., "Mon, Wed, Fri")start_time
: Start time of the classend_time
: End time of the classlocation
: Class location/room
Instructor information:
instructors
: Combined instructor information with rolesinstructor_names
: Names of instructorsinstructor_sunets
: SUNet IDs of instructorsinstructor_roles
: Roles of instructors
Returns NULL
if no courses found.
Details
The function follows this process:
Input validation:
Ensures single department code
Validates code format and content
Data retrieval:
Checks cache if
cache_dir
providedFetches from API on cache miss
Caches new data if applicable
Data processing:
Parses XML into structured format
Validates course data exists
Warning handlers
Warning message will be shown if:
Multiple department codes provided
No courses found for department
Error Handling
Invalid department code (empty or non-string)
API fetch failures
Cache operation failures
XML processing errors
See also
fetch_departments()
for getting list of valid department codesfetch_all_courses()
for fetching courses from multiple departmentsvalidate_academic_year()
for year format details
Examples
if (FALSE) { # \dontrun{
# Fetch current year's CS courses
cs_courses <- fetch_department_courses("CS")
# Fetch specific academic year
cs_courses_2023 <- fetch_department_courses(
name = "CS",
year = "20232024"
)
# Fetch with caching enabled
cs_courses <- fetch_department_courses(
name = "CS",
cache_dir = "course_cache"
)
# Handle empty results
courses <- fetch_department_courses("DEPT")
if (!is.null(courses)) {
# Process courses
}
# Find all courses taught by a specific instructor
cs_courses %>%
dplyr::filter(grepl("Smith", instructor_names))
# Get all lecture sections
lectures <- cs_courses %>%
dplyr::filter(component == "LEC")
} # }