From 3805536f09eea381fb0f83ff0c18ae9e616263b9 Mon Sep 17 00:00:00 2001 From: Timothy Mastny Date: Wed, 18 Apr 2018 17:19:02 -0500 Subject: [PATCH] changes from feedback on knitr #1518 with updated tests --- R/knitr-engine.R | 9 +++++---- man/cache_eng_python.Rd | 5 +++-- tests/testthat/resources/eng-reticulate-cache-test.Rmd | 2 -- tests/testthat/test-python-cache-engine.R | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/R/knitr-engine.R b/R/knitr-engine.R index 9de988820..55a3746ed 100644 --- a/R/knitr-engine.R +++ b/R/knitr-engine.R @@ -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")) } diff --git a/man/cache_eng_python.Rd b/man/cache_eng_python.Rd index d8fd64ef0..ff8067b7b 100644 --- a/man/cache_eng_python.Rd +++ b/man/cache_eng_python.Rd @@ -4,10 +4,11 @@ \alias{cache_eng_python} \title{A reticulate cache engine for Knitr} \usage{ -cache_eng_python(cache_path) +cache_eng_python(options) } \arguments{ -\item{cache_path}{The path to save the chunk cache, as provided by \code{knitr} during chunk execution.} +\item{options}{List of chunk options provided by \code{knitr} during chunk execution. +Contains the caching path.} } \description{ This provides a \code{reticulate} cache engine for \code{knitr}. The cache engine diff --git a/tests/testthat/resources/eng-reticulate-cache-test.Rmd b/tests/testthat/resources/eng-reticulate-cache-test.Rmd index a878f8b98..2017d584a 100644 --- a/tests/testthat/resources/eng-reticulate-cache-test.Rmd +++ b/tests/testthat/resources/eng-reticulate-cache-test.Rmd @@ -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: diff --git a/tests/testthat/test-python-cache-engine.R b/tests/testthat/test-python-cache-engine.R index 940d7016d..6d8f17422 100644 --- a/tests/testthat/test-python-cache-engine.R +++ b/tests/testthat/test-python-cache-engine.R @@ -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)