Skip to content

Commit

Permalink
changes from feedback on knitr #1518 with updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tmastny committed Apr 18, 2018
1 parent bd68b2c commit 3805536
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
9 changes: 5 additions & 4 deletions R/knitr-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -314,18 +314,19 @@ save_python_session <- function(cache_path) {
#' 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 cache_path
#' The path to save the chunk cache, as provided by `knitr` during chunk execution.
#' @param options
#' List of chunk options provided by `knitr` during chunk execution.
#' Contains the caching path.
#'
#' @export
cache_eng_python <- function(cache_path) {
cache_eng_python <- function(options) {
module <- tryCatch(import("dill"), error = identity)
if (inherits(module, "error")) {
if (module$message == "ImportError: No module named dill") return()
stop(module$message)
}

module$load_session(filename = paste0(cache_path, ".pkl"))
module$load_session(filename = paste0(options$hash, ".pkl"))
}


5 changes: 3 additions & 2 deletions man/cache_eng_python.Rd

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

2 changes: 0 additions & 2 deletions tests/testthat/resources/eng-reticulate-cache-test.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ title: "Using reticulate's Python Engine with knitr"
```{r setup, include = FALSE}
library(reticulate)
knitr::opts_chunk$set(cache=TRUE)
knitr::knit_engines$set(python = eng_python)
knitr::cache_engines$set(python = cache_eng_python)
```

Cache can handle changes to second chunk:
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-python-cache-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ test_that("An R Markdown document builds if a cache is modified", {
old_var <- "1"
new_var <- "0"
mutate_chunk <- function(x) {
print_line <- 19
print_line <- 17
file_text <- readLines("resources/eng-reticulate-cache-test.Rmd")
file_text[print_line] <- paste("print(x + ", x, ")", sep = "")
file_text[print_line] <- paste0("print(x + ", x, ")")
writeLines(file_text, "resources/eng-reticulate-cache-test.Rmd")
}
mutate_chunk(old_var)
Expand Down

0 comments on commit 3805536

Please sign in to comment.