-
Notifications
You must be signed in to change notification settings - Fork 346
Cache engine for reticulate using dill
#1210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 9 commits
228624b
b52ecaa
c345ce2
ff39889
8e07779
638a4e7
9753870
02c1771
dbebab3
bd29f84
f8497a0
fe4cd9f
5d6f7a7
a33ed39
266463c
445a5ca
c6a88ad
975c1b0
401b1ba
62c77d8
f487b52
cb9ee1f
7d4eeec
395627e
55d1e03
d43b593
f354f60
38ef3ce
79b9732
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -244,6 +244,10 @@ eng_python <- function(options) { | |
| outputs$push(output) | ||
| } | ||
|
|
||
| if(options$cache > 0) { | ||
| save_python_session(options$hash) | ||
| } | ||
|
|
||
| # if we had held outputs, add those in now (merging text output as appropriate) | ||
| text_output <- character() | ||
|
|
||
|
|
@@ -292,6 +296,16 @@ eng_python_initialize <- function(options, envir) { | |
| ensure_python_initialized() | ||
| eng_python_initialize_hooks(options, envir) | ||
|
|
||
| if (options$cache > 0) { | ||
| module <- tryCatch(import("dill"), error = identity) | ||
| if (inherits(module, "error")) { | ||
| if (module$message == "ImportError: No module named dill") { | ||
| warning("The Python module dill was not found. This module is needed for full cache functionality.") | ||
| } else { | ||
| stop(module$message) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| eng_python_knit_figure_path <- function(options, suffix = NULL) { | ||
|
|
@@ -628,3 +642,54 @@ eng_python_autoprint <- function(captured, options, autoshow) { | |
| } | ||
|
|
||
| } | ||
|
|
||
| save_python_session <- function(cache_path) { | ||
| module <- tryCatch(import("dill"), error = identity) | ||
| if (inherits(module, "error")) { | ||
| if (module$message == "ImportError: No module named dill") return() | ||
leogama marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| signalCondition(module$message) | ||
| } | ||
|
|
||
| r_obj_exists <- "'r' in globals()" | ||
| r_is_R <- "type(r).__module__ == '__main__' and type(r).__name__ == 'R'" | ||
| if (py_eval(r_obj_exists) && py_eval(r_is_R)) { | ||
leogama marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| py_run_string("del globals()['r']") | ||
|
||
| } | ||
|
|
||
| cache_path <- file.path(knitr::opts_knit$get("output.dir"), cache_path) | ||
| module$dump_session(filename = paste0(cache_path, ".pkl"), byref = TRUE) | ||
| } | ||
|
|
||
| #' A reticulate cache engine for Knitr | ||
| #' | ||
| #' This provides a `reticulate` cache engine for `knitr`. The cache engine | ||
| #' allows `knitr` to save and load Python sessions between cached chunks. The | ||
| #' cache engine depends on the `dill` Python module. Therefore, you must have | ||
| #' `dill` installed in your Python environment. | ||
| #' | ||
| #' The engine can be activated by setting (for example) | ||
| #' | ||
| #' ``` | ||
| #' knitr::cache_engines$set(python = reticulate::cache_eng_python) | ||
| #' ``` | ||
| #' | ||
| #' Typically, this will be set within a document's setup chunk, or by the | ||
| #' environment requesting that Python chunks be processed by this engine. | ||
| #' | ||
| #' @param options | ||
| #' List of chunk options provided by `knitr` during chunk execution. | ||
| #' Contains the caching path. | ||
| #' | ||
| #' @export | ||
| cache_eng_python <- function(options) { | ||
| module <- tryCatch(import("dill"), error = identity) | ||
| if (inherits(module, "error")) { | ||
| if (module$message == "ImportError: No module named dill") return() | ||
leogama marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| stop(module$message) | ||
| } | ||
|
|
||
| cache_path <- normalizePath(paste0(options$hash, ".pkl"), mustWork = TRUE) | ||
| knitr:::in_input_dir(module$load_session(filename = cache_path)) | ||
| } | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| --- | ||
| title: "Using reticulate's Python Engine with knitr" | ||
| --- | ||
|
|
||
| ```{r setup, include = FALSE} | ||
| library(reticulate) | ||
| knitr::opts_chunk$set(cache=TRUE) | ||
| ``` | ||
|
|
||
| Cache can handle changes to second chunk: | ||
|
|
||
| ```{python} | ||
| x = 1 | ||
| ``` | ||
|
|
||
| ```{python} | ||
| print(x + 1) | ||
| ``` | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| context("knitr-cache") | ||
|
|
||
| test_that("An R Markdown document can be rendered with cache using reticulate", { | ||
|
|
||
| skip_on_cran() | ||
| skip_if_not_installed("rmarkdown") | ||
| skip_if_not_installed("callr") | ||
|
|
||
| unlink("resources/eng-reticulate-cache-test_cache/", recursive = TRUE) | ||
|
|
||
| path <- callr::r( | ||
| function() { | ||
| rmarkdown::render("resources/eng-reticulate-cache-test.Rmd", quiet = TRUE, envir = new.env()) | ||
| }) | ||
| expect_true(file.exists(path)) | ||
| on.exit(unlink(path), add = TRUE) | ||
| }) | ||
|
|
||
| test_that("An R Markdown document builds if a cache is modified", { | ||
|
|
||
| skip_on_cran() | ||
| skip_if_not_installed("rmarkdown") | ||
| skip_if_not_installed("callr") | ||
|
|
||
| old_var <- "1" | ||
| new_var <- "0" | ||
| mutate_chunk <- function(x) { | ||
| print_line <- 17 | ||
| file_text <- readLines("resources/eng-reticulate-cache-test.Rmd") | ||
| file_text[print_line] <- paste0("print(x + ", x, ")") | ||
| writeLines(file_text, "resources/eng-reticulate-cache-test.Rmd") | ||
| } | ||
| mutate_chunk(old_var) | ||
| mutate_chunk(new_var) | ||
| path <- callr::r( | ||
| function() { | ||
| rmarkdown::render("resources/eng-reticulate-cache-test.Rmd", quiet = TRUE, envir = new.env()) | ||
| }) | ||
| mutate_chunk(old_var) | ||
| expect_true(file.exists(path)) | ||
| on.exit(unlink(path), add = TRUE) | ||
| on.exit(unlink("resources/eng-reticulate-cache-test_cache/", recursive = TRUE), add = TRUE) | ||
| }) | ||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| context("dill") | ||
|
|
||
| source("helper-utils.R") | ||
|
|
||
| test_that("Interpreter sessions can be saved and loaded with dill", { | ||
| skip_if_no_python() | ||
| skip_if_not_installed("callr") | ||
|
|
||
| session_one_vars <- callr::r( | ||
| function() { | ||
| module_load <- tryCatch( | ||
| dill <- reticulate::import("dill"), | ||
| error = function(c) { | ||
| py_error <- reticulate::py_last_error() | ||
| if(py_error$type == "ImportError" && py_error$value == "No module named dill") { | ||
| "No dill" | ||
| }}) | ||
| if (module_load == "No dill") return(module_load) | ||
| main <- reticulate::py_run_string("x = 1") | ||
| reticulate::py_run_string("y = x + 1") | ||
| dill$dump_session(filename = "x.dill", byref = TRUE) | ||
| c(main$x, main$y) | ||
| }) | ||
| if (session_one_vars[1] == "No dill") | ||
| skip("The dill Python module is not installed") | ||
|
|
||
| session_two_vars <- callr::r( | ||
| function() { | ||
| dill <- reticulate::import("dill") | ||
| dill$load_session(filename = "x.dill") | ||
| main <- reticulate::py_run_string("pass") | ||
| c(main$x, main$y) | ||
| }) | ||
| on.exit(unlink("x.dill"), add = TRUE) | ||
| expect_equal(session_one_vars, session_two_vars) | ||
| }) | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| context("globals") | ||
|
|
||
| source("utils.R") | ||
|
|
||
| test_that("Interpreter sessions can be saved and loaded with dill", { | ||
| skip_if_no_python() | ||
|
|
||
| py_run_string("x = 1") | ||
| py_run_string("y = 1") | ||
| py_run_string("[globals().pop(i) for i in ['x', 'y']]") | ||
|
|
||
| test_x <- tryCatch( | ||
| py_run_string("x = x + 1"), | ||
| error = function(e) { | ||
| py_last_error()$value | ||
| } | ||
| ) | ||
| test_y <- tryCatch( | ||
| py_run_string("y = y + 1"), | ||
| error = function(e) { | ||
| py_last_error()$value | ||
| } | ||
| ) | ||
| expect_equal(test_x, "name 'x' is not defined") | ||
| expect_equal(test_y, "name 'y' is not defined") | ||
| }) | ||
|
|
Uh oh!
There was an error while loading. Please reload this page.