lesson_storage: Internal cache for storing pre-computed lesson objects

this_lessonR Documentation

Internal cache for storing pre-computed lesson objects


A storage cache for pegboard::Lesson objects and other pre-computed items for use by other internal functions while sandpaper is working.









a path to the current lesson

Lesson Object Storage

this_lesson() will return a pegboard::Lesson object if it has previously been stored. There are three values that are cached:

  • .this_lesson a pegboard::Lesson object

  • .this_diff a charcter vector from gert::git_diff_patch()

  • .this_status a data frame from gert::git_status()

  • .this_commit the hash of the most recent commit

The function this_lesson() first checks if .this_diff is different than the output of gert::git_diff_patch(), then checks if there are any changes to gert::git_status(), and then finally checks if the commits are identical. If there are differences or the values are not previously cached, the lesson is loaded into memory, otherwise, it is fetched from the previously stored lesson.

The storage cache is in a global package object called .store, which is initialised when sandpaper is loaded via .lesson_store()

If there have been no changes git is aware of, the lesson remains the same.

Pre-Computed Object Storage

A side-effect of this_lesson() is that it will also initialise pre-computed objects that pertain to the lesson itself. These are initialised via set_globals(). These storage objects are:

  • .resources: a list of markdown resources for the lesson derived from get_resource_list() via set_resource_list()

  • this_metadata: metadata with template for including in the pages. initialised in initialise_metadata() via set_globals()

  • learner_globals: variables for the learner version of the pages initialised in set_globals()

  • instructor_globals: variables for the instructor version of the pages initialised in set_globals()


tmp <- tempfile()
create_lesson(tmp, open = FALSE)
# Read the lesson into cache
system.time(sandpaper:::this_lesson(tmp)) # less time to read in once cached
l <- sandpaper:::this_lesson(tmp)
# clear the cache
system.time(sandpaper:::this_lesson(tmp)) # have to re-read the lesson

zkamvar/sandpaper documentation built on Jan. 26, 2023, 1:35 a.m.