Skip to content

Instantly share code, notes, and snippets.

@pinusm
Created December 22, 2021 11:54
Show Gist options
  • Save pinusm/1a0c6667fa3c969e6112993857adb333 to your computer and use it in GitHub Desktop.
Save pinusm/1a0c6667fa3c969e6112993857adb333 to your computer and use it in GitHub Desktop.
Using RStudio as a python IDE
---
title: "python in RStudio"
output: html_notebook
---
Based on instruction [here] (https://support.rstudio.com/hc/en-us/articles/360023654474-Installing-and-Configuring-Python-with-RStudio), but adapted for Windows.
1. Install system-wide python, using conda, or whatever.
2. Make sure python/pip is included in PATH
3. Install virtualenv with `pip install virtualenv` from a terminal
4. Navigate to project folder (or create it), and create a new virtualenv in it with `virtualenv my_env` from a terminal.
5. Activate the virtualenv, with `source /path/to/venv/bin/activate` in bash (unix,macOS), `path\to\venv\Scripts\activate.bat` in cmd, or `path\to\venv\Scripts\Activate.ps1` in powershell.
6. Make sure the correct python interpreter is being used, with `which python` from a terminal. The output should be
'/path/to/venv/Scripts/python'.
7. Install python packages using pip from a terminal, such as in:
`pip install numpy pandas matplotlib`.
8. Install reticulate with `install.packages("reticulate")` from within R.
9. Create an `.Rprofile` file, with the path to the virtualenv in it, to point reticulate to the correct python version. This is what should be in it:
`Sys.setenv(RETICULATE_PYTHON = "my_env/Scripts")`
9. Verify the correct python version is set, with `reticulate::py_config()`, from within R.
10. OPTIONAL: Modify the keyboard shortcut to create a new R chunk (default: Ctrl+Alt+I), to create a new python chunk, or simply add a keyboard shortcut for this action (I'm using Ctrl+Alt+Shift+I).
```{r}
library(reticulate)
py_config()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment