File: test-custom-root-dir.Rmd

package info (click to toggle)
r-cran-reticulate 1.41.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,088 kB
  • sloc: cpp: 5,154; python: 620; sh: 13; makefile: 2
file content (57 lines) | stat: -rw-r--r-- 1,575 bytes parent folder | download
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")
```