-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path08-Python-in-R.Rmd
More file actions
51 lines (38 loc) · 1.57 KB
/
08-Python-in-R.Rmd
File metadata and controls
51 lines (38 loc) · 1.57 KB
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
---
editor_options:
markdown:
wrap: 80
---
# Python Integration in RStudio
## Setting up a python virtual environment
If you need python modules (the equivalent of R packages) that are not included
in the python base modules, you will need to set up a virtual environment for
reproducibility.
In order to do this successfully, you'll need to edit the `pySetup.R` script so
that it suits your needs. An example of this script can be found at
`python_setup_helps/pySetup.R`.
Best practices note: you should not track your virtual environment folder in
GitHub. Add the folder extension to your `.gitignore` file and confirm that it
is not tracking the folder.
### Python setup code snippet
This snippet checks to see if the 'env' folder exists - this is where the
virtual environment is set up, and if it is not, it runs the `pySetup.R` file.
If the 'env' folder exists, we use the {reticulate} function `use_condaenv()`
to read the virtual environment you already created in the `pySetup.R` script.
```{r, eval=FALSE}
library(reticulate)
if (!dir.exists("env")) {
source("pySetup.R")
} else {
use_condaenv(file.path(getwd(), "env"))
}
```
### Editing the `pySetup.R` file
The things you may have to edit are the lines for the python modules you want
to install in your python virtual environment (on line 6), and the version of
python you want to use.
The script defaults to python version 3.8 as it seems to be the most stable for
the `earthengine-api` module, but any version can be specified.
```{r, echo = F}
knitr::wrap_rmd('08-Python-in-R.Rmd', width = 80, backup = NULL)
```