reticulate_vignette.Rmd 3.27 KB
Newer Older
Eva Rifà's avatar
Eva Rifà committed
---
author: "Eva Rifà"
Eva Rifà's avatar
Eva Rifà committed
date: "01/02/2024"
Eva Rifà's avatar
Eva Rifà committed
output: rmarkdown::html_vignette
vignette: >
  %\VignetteEngine{knitr::knitr}
  %\VignetteIndexEntry{Data Storage and Retrieval}
  %\usepackage[utf8]{inputenc}
---

Eva Rifà's avatar
Eva Rifà committed
Reticulate
Eva Rifà's avatar
Eva Rifà committed
-----------------------------------------

Eva Rifà's avatar
Eva Rifà committed
The reticulate package provides a comprehensive set of tools for interoperability between Python and R.
Eva Rifà's avatar
Eva Rifà committed

Eva Rifà's avatar
Eva Rifà committed
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.
Eva Rifà's avatar
Eva Rifà committed

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

### 1. Importing Python modules
Eva Rifà's avatar
Eva Rifà committed
The import() function can import any Python module and call it’s functions directly from R.  
Eva Rifà's avatar
Eva Rifà committed

Eva Rifà's avatar
Eva Rifà committed
#### Example I
Eva Rifà's avatar
Eva Rifà committed
```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")
```
Eva Rifà's avatar
Eva Rifà committed
#### Example II
Eva Rifà's avatar
Eva Rifà committed

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
Eva Rifà's avatar
Eva Rifà committed
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:
Eva Rifà's avatar
Eva Rifà committed

```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")
```
Eva Rifà's avatar
Eva Rifà committed
Then, we need to save the script. Let's call it ('test_reticulate_source.py').  
Eva Rifà's avatar
Eva Rifà committed

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

Eva Rifà's avatar
Eva Rifà committed
source_python("test_reticulate_source.py")
Eva Rifà's avatar
Eva Rifà committed
```

### 3. Python REPL
Eva Rifà's avatar
Eva Rifà committed
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).  
Eva Rifà's avatar
Eva Rifà committed

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")
```