Skip to content

Instantly share code, notes, and snippets.

@riinuots
Created June 22, 2022 08:38
Show Gist options
  • Save riinuots/caef467dc60db0d9bdbce44234fac7e9 to your computer and use it in GitHub Desktop.
Save riinuots/caef467dc60db0d9bdbce44234fac7e9 to your computer and use it in GitHub Desktop.
---
title: "Reticulate example"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
message = FALSE,
warning = FALSE
)
```
The reticulate R package allows for interoperability between R and python within R Markdown documents or scripts.
https://rstudio.github.io/reticulate/
## Initialise R Markdown document with reticulate and load the desired environment
```{r}
library(reticulate)
conda_list()
use_condaenv("sklearn-env")
```
## Some python code
```{python}
import pandas as pd
import matplotlib.pyplot as plt
iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
iris['species'].value_counts().plot(kind='barh')
plt.show()
```
## Some R code modifying the python object
```{r}
library(tidyverse)
py$iris = py$iris %>%
filter(species == "setosa")
```
## Back to python checking the change worked
```{python}
iris['species'].value_counts().plot(kind='barh')
plt.show()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment