O'Reilly logo

NumPy Cookbook - Second Edition by Ivan Idris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Interfacing with R

RPy2 can only be used to call R from Python, and not the other way around. We will import some sample R datasets and plot the data of one of them.

Getting ready

Install RPy2 if necessary. See the previous recipe.

How to do it...

The complete code for this recipe is in the rdatasets.py file in this book's code bundle:

from rpy2.robjects.packages import importr
import numpy as np
import matplotlib.pyplot as plt

datasets = importr('datasets')
mtcars = datasets.__rdata__.fetch('mtcars')['mtcars']

plt.title('R mtcars dataset')
plt.xlabel('wt')
plt.ylabel('mpg')
plt.plot(mtcars)
plt.grid(True)
plt.show()

The motorcars dataset is described at https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/mtcars.html. Let's start by loading ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required