Skip to content

Commit

Permalink
Expose the cache$available() method to knitr
Browse files Browse the repository at this point in the history
  • Loading branch information
leogama committed Sep 14, 2022
1 parent 5d6f7a7 commit a33ed39
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
16 changes: 9 additions & 7 deletions R/knitr-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#'
#' @export
cache_eng_python <- (function() {
check_cache_available <- function() {
check_cache_available <- function(options) {
eng_python_initialize(options)

# does the python version is supported by 'dill'?
if (py_version() < "3.7") {
warning("Python cache requires Python version >= 3.7")
Expand Down Expand Up @@ -48,10 +50,10 @@ cache_eng_python <- (function() {
TRUE
}

cache_available <- function() {
cache_available <- function(options) {
available <- knitr::opts_knit$get("reticulate.cache")
if (is.null(available)) {
available <- check_cache_available()
available <- check_cache_available(options)
knitr::opts_knit$set(reticulate.cache = available)
}
available
Expand All @@ -66,8 +68,7 @@ cache_eng_python <- (function() {
}

cache_load <- function(options) {
eng_python_initialize(options, envir = environment())
if (!cache_available()) return()
if (!cache_available(options)) return()
dill <- import("dill")
dill$load_module(filename = cache_path(options$hash), module = "__main__")
}
Expand All @@ -81,7 +82,7 @@ cache_eng_python <- (function() {
}

cache_save <- function(options) {
if (!cache_available()) return()
if (!cache_available(options)) return()
dill <- import("dill")
tryCatch({
dill$dump_module(cache_path(options$hash), refimported = TRUE, exclude = r_obj_filter())
Expand All @@ -95,5 +96,6 @@ cache_eng_python <- (function() {
unlink(cache_path(glob_path))
}

list(exists = cache_exists, load = cache_load, save = cache_save, purge = cache_purge)
list(available = cache_available, exists = cache_exists, load = cache_load, save = cache_save,
purge = cache_purge)
})()
3 changes: 2 additions & 1 deletion R/knitr-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ eng_python <- function(options) {
# a list of pending plots / outputs
.engine_context$pending_plots <- stack()

eng_python_initialize(options = options, envir = environment())
eng_python_initialize(options)

# helper function for extracting range of code, dropping blank lines
extract <- function(code, range) {
Expand Down Expand Up @@ -294,6 +294,7 @@ eng_python <- function(options) {
}

eng_python_initialize <- function(options, envir) {
if (missing(envir)) envir <- environment()

if (is.character(options$engine.path))
use_python(options$engine.path[[1]])
Expand Down

0 comments on commit a33ed39

Please sign in to comment.