1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
---
title: "reticulate"
---
Change the knit root directory to the temporary R session.
See https://github.com/rstudio/reticulate/issues/1526 for context.
```{r setup}
knitr::opts_knit$set("root.dir" = tempdir())
```
```{r confirm-wd}
getwd()
```
```{r configure-python}
library("reticulate")
matplotlib <- import("matplotlib")
```
```{r}
reticulate::py_config()
```
The R plot is saved relative to the location of the Rmd file:
```{r r-plot}
plot(1:10)
```
The Python plot is also saved relative to the location of the Rmd file (not root.dir):
```{python py-plot}
import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, x)
```
```{r}
knitr::opts_knit$get("root.dir")
knitr::opts_knit$get("base.dir")
knitr::opts_knit$get("output.dir")
if (length(Sys.glob(paste0(knitr::opts_knit$get("root.dir"), "/figure*/*"))))
stop("Figures saved in the wrong dir")
if (!setequal(
basename(Sys.glob(paste0(knitr::opts_knit$get("output.dir"), "/figure*/*"))),
c("r-plot-1.png", "py-plot-1.png")))
stop("Figures not found in expected output.dir")
if( file.exists(file.path(knitr::opts_knit$get("root.dir"), "figure", "r-plot-1.png"))) stop("figure saved in wrong place1")
if( file.exists(file.path(knitr::opts_knit$get("root.dir"), "figure", "py-plot-1.png"))) stop("figure saved in wrong place2")
if(!file.exists(file.path(knitr::opts_knit$get("output.dir"), "figure", "r-plot-1.png"))) stop("figure saved in wrong place3")
if(!file.exists(file.path(knitr::opts_knit$get("output.dir"), "figure", "py-plot-1.png"))) stop("figure saved in wrong place4")
```
|