---
author: "Eva Rifà"
date: "01/02/2024"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteEngine{knitr::knitr}
  %\VignetteIndexEntry{Data Storage and Retrieval}
  %\usepackage[utf8]{inputenc}
---

Reticulate
-----------------------------------------

The reticulate package provides a comprehensive set of tools for interoperability between Python and R.

Reticulate embeds a Python session within your R session. It calls Python from R being able to translate objects within both packages. All the information can be found in the package documentation: https://rstudio.github.io/reticulate/. 

> NOTE: This script was used as an Ice-Breaker of the R User Meeting held at BSC on 4/5/2023.

**How to use reticulate?**  
There are a variety of ways to integrate Python code into R:  
1. Importing Python modules
2. Sourcing Python scripts
3. Python REPL
4. Python in R Markdown (not shown in this file)

### 1. Importing Python modules
The import() function can import any Python module and call it’s functions directly from R.  

#### Example I
```t
# Import modules in a terminal
module load matplotlib/3.2.2-foss-2015a-Python-3.7.3
module load Python/3.7.3-foss-2015a
module load R/4.1.2-foss-2015a-bare
```

```r
library(reticulate)
```
```r
matplotlib <- import("matplotlib")

# Generate some data
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 6, 8, 10)

# Create a scatter plot using matplotlib
plt <- matplotlib$pyplot
plt$scatter(x, y)
plt$xlabel("X")
plt$ylabel("Y")
plt$title("Scatter Plot")
plt$savefig(fname = "plot1.png")
```
#### Example II

In the next example, we load the necessary modules in a terminal:  
```t
# Import modules in a terminal
module load xarray/0.20.0-foss-2015a-Python-3.7.3
# Other dependencies of xarray
module load netcdf4-python/1.5.3-foss-2015a-Python-3.7.3
module load numpy/1.20.3-foss-2015a-Python-3.7.3
```
```r
library(reticulate)
# Example 2: Read Netcdf with xarray 
xr <- import("xarray")

obs_path <- "/esarchive/recon/ecmwf/erainterim/monthly_mean/tas_f6h/"
obs_file <- paste0(obs_path, "tas_201811.nc")

obs <- xr$open_dataset(obs_file)

obs
# <xarray.Dataset>
# Dimensions:  (time: 1, lat: 256, lon: 512)
```

### 2. Sourcing Python scripts
The source_python() function can source a Python script the same way as source() an R script. Here, we first need to save a python script:

```py
import matplotlib.pyplot as plt
import numpy as np

# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel = 'time (s)', ylabel = 'x (m)',
       title = 'Created with R')
ax.grid()
fig.savefig("plot2.png")
```
Then, we need to save the script. Let's call it ('test_reticulate_source.py').  

Then, we source it from an R session:
```r
library(reticulate)

source_python("test_reticulate_source.py")
```

### 3. Python REPL
The repl_python() function creates an interactive Python console within R. Objects you create within Python are available to your R session (and vice-versa).  

Inside an R session:  
```r
library(reticulate)
repl_python()

import matplotlib.pyplot as plt
import numpy as np

# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel = 'time (s)', ylabel = 'x (m)',
       title = 'Created with R')
ax.grid()
fig.savefig("plot2.png")
```