diff --git a/man/knit_cache_hook.Rd b/man/knit_cache_hook.Rd new file mode 100644 index 0000000000..ee250cf619 --- /dev/null +++ b/man/knit_cache_hook.Rd @@ -0,0 +1,57 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/cache.R +\name{knit_cache_hook} +\alias{knit_cache_hook} +\title{Hook cache behavior} +\usage{ +knit_cache_hook(x, nm, path, ...) +} +\arguments{ +\item{x}{a value of object to be cached.} + +\item{nm}{a name of the object to be cached. If a hook creates an external file based on \code{nm}, then apply \code{\link{URLencode}} to \code{nm} in order to avoid invalid file names.} + +\item{path}{a common path of the cache files of a chunk. If the hook creates extra +files which needs be cleaned up by knitr, then create a directory whose +name is `\code{path}` suffixed by "__extra", and save the files in it.} + +\item{...}{Reserved for future extensions} +} +\value{ +A value to be cached. If the value is the \code{knitr_cache_loader}-classed +function, then the function is called and the returned value is treated as +the loaded value. The loader should receive ellipsis as an argument for the +future extentions. +} +\description{ +By default, a named list of objects in a chunk is cached as is in a rds +file. If certain classes of objects need custom cache behaviors, register +S3 methods to \code{knit_cache_hook}. The return value of the method +is cached to the rds file. If custom loader is needed, the method should +return a function with \code{knit_cache_loader} class which will be called. +} +\examples{ +registerS3method( + "knit_cache_hook", + "character", + function(x, nm, path, ...) { + # Cache x as is if it extends character class + if (!identical(class(x), "character")) { + return(x) + } + + # Preprocess data (e.g., save data to an external file) + # Create external files under the directory of `paste0(path, "__extra")` + # if knitr should cleanup them on refreshing/cleaning cache. + d <- paste0(path, "__extra") + dir.create(d, showWarnings = FALSE, recursive = TRUE) + f <- file.path(d, paste0(URLencode(nm, reserved = TRUE), '.txt')) + writeLines(x, f) + + # Return loader function + # which receives ellipsis for future extentions and has knit_cache_loader class + structure(function(...) readLines(f), class = 'knit_cache_loader') + }, + envir = asNamespace("knitr") +) +}