Using Python in Rstudio

With the reticulate package, it has become much easier to call Python in RStudio.

Below is (probably) the easiest way I have found to set Python in RStudio.

library(reticulate)
use_condaenv("anaconda3", required = TRUE)
## An example 
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(19680801)
N = 150
r = 2 * np.random.rand(N)
theta = 2 * np.pi * np.random.rand(N)
area = 200 * r**2
colors = theta
fig = plt.figure()
ax = fig.add_subplot(111, projection = 'polar')
c = ax.scatter(theta, r, c = colors, s = area, cmap = 'hsv', alpha = 0.75)
plt.savefig('test.png', dpi = 300)

Pythom example

Avatar
Yonghui Dong

My research interests include mass spectrometry imaging, metabolomics, chemometrics and chemical ecology.

comments powered by Disqus

Related