Skip to content

Commit

Permalink
added cache_engines object and cache_engine function to make API for …
Browse files Browse the repository at this point in the history
…engine-specific caches
  • Loading branch information
tmastny committed Apr 18, 2018
1 parent 5e99ea1 commit 4718e21
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export(all_labels)
export(all_patterns)
export(all_rcpp_labels)
export(asis_output)
export(cache_engines)
export(clean_cache)
export(combine_words)
export(current_input)
Expand Down
1 change: 1 addition & 0 deletions R/block.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ call_block = function(block) {
params$engine != 'Rcpp') {
if (opts_knit$get('verbose')) message(' loading cache from ', hash)
cache$load(hash, lazy = params$cache.lazy)
cache_engine(params$engine, hash)
if (!params$include) return('')
if (params$cache == 3) return(cache$output(hash))
}
Expand Down
37 changes: 37 additions & 0 deletions R/engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@
#' names(knit_engines$get())
knit_engines = new_defaults()


#' Cache engines of other languages
#'
#' This object controls how to load cached environments from languages
#' other than R (when the chunk option \code{engine} is not \code{'R'}).
#' Each component in this object is a function that takes the current path
#' to the chunk cache and loads it into the language environment.
#'
#' The cache engine function has one argument \code{cache_path}. The
#' argument is the path to the current chunk cache with the chunk's
#' hash, but without any file extension.
#'
#' The cache engine function should load the cache environment and should
#' know the extension appropriate for the language.
#' @export
#' @examples cache_engines$set(python = reticulate::load_python_session)
cache_engines = new_defaults()

#' An output wrapper for language engine output
#'
#' If you have designed a language engine, you may call this function in the end
Expand Down Expand Up @@ -178,6 +196,19 @@ eng_python = function(options) {
}
}

eng_python_cache = function(options) {
if (isFALSE(options$python.reticulate)) {
eng_interpreted(options)
} else {
if (!loadable('reticulate')) warning2(
"The 'python' engine in knitr requires the reticulate package. ",
"If you do not want to use the reticulate package, set the chunk option ",
"python.reticulate = FALSE."
)
reticulate::load_python_session(options)
}
}

## Java
# e.g. see http://cran.rstudio.com/package=jvmr

Expand Down Expand Up @@ -649,6 +680,12 @@ get_engine = function(name) {
}
}

cache_engine = function(name, cache_path) {
cache_fun = cache_engines$get(name)
if (!is.function(cache_fun)) return()
cache_fun(cache_path)
}

# possible values for engines (for auto-completion in RStudio)
opts_chunk_attr$engine = as.list(sort(c('R', names(knit_engines$get()))))
opts_chunk_attr[c('engine.path', 'engine.opts')] = list('character', 'character')
28 changes: 28 additions & 0 deletions man/cache_engines.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions man/read_chunk.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4718e21

Please sign in to comment.