mirror of
https://github.com/rstudio/shiny.git
synced 2026-04-07 03:00:20 -04:00
First implementation of plotCache
This commit is contained in:
@@ -164,6 +164,7 @@ export(pageWithSidebar)
|
||||
export(paneViewer)
|
||||
export(parseQueryString)
|
||||
export(passwordInput)
|
||||
export(plotCache)
|
||||
export(plotOutput)
|
||||
export(plotPNG)
|
||||
export(pre)
|
||||
|
||||
@@ -975,3 +975,77 @@ find_panel_ranges <- function(g, pixelratio, res) {
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
#' Disk-based plot cache
|
||||
#'
|
||||
#' Creates a read-through cache for plots. The plotting logic is provided as
|
||||
#' plotFunc, a function that can have any number/combination of arguments; the
|
||||
#' return value of plotCache is a function that should be used in the place of
|
||||
#' plotFunc. Each unique combination of inputs will be cached to disk in the
|
||||
#' location specified by cachePath.
|
||||
#'
|
||||
#' The invalidationExpr expression will be monitored and whenever it is
|
||||
#' invalidated, so too is the cache invalidated (the contents are erased).
|
||||
#'
|
||||
#' @param cacheId An identifier for this cache; by default, will be incorporated
|
||||
#' into the cache directory path.
|
||||
#' @param invalidationExpr Any expression or block of code that accesses any
|
||||
#' reactives whose invalidation should cause cache invalidation. This
|
||||
#' typically would be an expression that indicates that the source data has
|
||||
#' changed. Use \code{NULL} if you don't want to cause cache invalidation.
|
||||
#' @param width,height The dimensions of the plot. (Use double the user
|
||||
#' width/height for retina/hi-dpi compatibility.)
|
||||
#' @param res The resolution of the PNG. Use 72 for normal screens, 144 for
|
||||
#' retina/hi-dpi.
|
||||
#' @param plotFunc Plotting logic, provided as a function that takes zero or
|
||||
#' more arguments. Don't worry about setting up a graphics device or creating
|
||||
#' a PNG; just write to the graphics device (you must call \code{print()} on
|
||||
#' ggplot2 objects).
|
||||
#' @param cachePath The location on disk where the cache will be stored. By
|
||||
#' default, uses a temp directory, which is generally cleaned up during a
|
||||
#' normal shutdown of the R process.
|
||||
#' @param invalidation.env The environment where the \code{invalidationExpr} is
|
||||
#' evaluated.
|
||||
#' @param invalidation.quoted Is \code{invalidationExpr} expression quoted? By
|
||||
#' default, this is FALSE. This is useful when you want to use an expression
|
||||
#' that is stored in a variable; to do so, it must be quoted with
|
||||
#' \code{quote()}.
|
||||
#'
|
||||
#' @export
|
||||
plotCache <- function(cacheId, invalidationExpr, width, height, res = 72,
|
||||
plotFunc,
|
||||
cachePath = file.path(tempdir(), cacheId),
|
||||
invalidation.env = parent.frame(),
|
||||
invalidation.quoted = FALSE) {
|
||||
|
||||
dir.create(cachePath, recursive = TRUE, mode = "0700")
|
||||
|
||||
if (!invalidation.quoted) {
|
||||
invalidationExpr <- substitute(invalidationExpr)
|
||||
}
|
||||
|
||||
observeEvent(invalidationExpr, event.env = invalidation.env, event.quoted = TRUE, {
|
||||
# TODO: robustify
|
||||
if (dir.exists(cachePath)) {
|
||||
file.rename(cachePath, paste0(cachePath, ".gone"))
|
||||
}
|
||||
dir.create(cachePath, recursive = TRUE, mode = "0700")
|
||||
unlink(paste0(cachePath, ".gone"), recursive = TRUE)
|
||||
})
|
||||
|
||||
function(...) {
|
||||
args <- list(...)
|
||||
key <- paste0(digest::digest(args), ".png")
|
||||
filePath <- file.path(cachePath, key)
|
||||
if (!file.exists(filePath)) {
|
||||
message("Cache miss")
|
||||
plotPNG(function() {
|
||||
do.call("plotFunc", args)
|
||||
}, filename = filePath, width = width, height = height, res = res)
|
||||
} else {
|
||||
message("Cache hit")
|
||||
}
|
||||
filePath
|
||||
}
|
||||
}
|
||||
|
||||
53
man/plotCache.Rd
Normal file
53
man/plotCache.Rd
Normal file
@@ -0,0 +1,53 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/render-plot.R
|
||||
\name{plotCache}
|
||||
\alias{plotCache}
|
||||
\title{Disk-based plot cache}
|
||||
\usage{
|
||||
plotCache(cacheId, invalidationExpr, width, height, res = 72, plotFunc,
|
||||
cachePath = file.path(tempdir(), cacheId),
|
||||
invalidation.env = parent.frame(), invalidation.quoted = FALSE)
|
||||
}
|
||||
\arguments{
|
||||
\item{cacheId}{An identifier for this cache; by default, will be incorporated
|
||||
into the cache directory path.}
|
||||
|
||||
\item{invalidationExpr}{Any expression or block of code that accesses any
|
||||
reactives whose invalidation should cause cache invalidation. This
|
||||
typically would be an expression that indicates that the source data has
|
||||
changed. Use \code{NULL} if you don't want to cause cache invalidation.}
|
||||
|
||||
\item{width, height}{The dimensions of the plot. (Use double the user
|
||||
width/height for retina/hi-dpi compatibility.)}
|
||||
|
||||
\item{res}{The resolution of the PNG. Use 72 for normal screens, 144 for
|
||||
retina/hi-dpi.}
|
||||
|
||||
\item{plotFunc}{Plotting logic, provided as a function that takes zero or
|
||||
more arguments. Don't worry about setting up a graphics device or creating
|
||||
a PNG; just write to the graphics device (you must call \code{print()} on
|
||||
ggplot2 objects).}
|
||||
|
||||
\item{cachePath}{The location on disk where the cache will be stored. By
|
||||
default, uses a temp directory, which is generally cleaned up during a
|
||||
normal shutdown of the R process.}
|
||||
|
||||
\item{invalidation.env}{The environment where the \code{invalidationExpr} is
|
||||
evaluated.}
|
||||
|
||||
\item{invalidation.quoted}{Is \code{invalidationExpr} expression quoted? By
|
||||
default, this is FALSE. This is useful when you want to use an expression
|
||||
that is stored in a variable; to do so, it must be quoted with
|
||||
\code{quote()}.}
|
||||
}
|
||||
\description{
|
||||
Creates a read-through cache for plots. The plotting logic is provided as
|
||||
plotFunc, a function that can have any number/combination of arguments; the
|
||||
return value of plotCache is a function that should be used in the place of
|
||||
plotFunc. Each unique combination of inputs will be cached to disk in the
|
||||
location specified by cachePath.
|
||||
}
|
||||
\details{
|
||||
The invalidationExpr expression will be monitored and whenever it is
|
||||
invalidated, so too is the cache invalidated (the contents are erased).
|
||||
}
|
||||
Reference in New Issue
Block a user