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.
Install RPy2 if necessary. See the previous recipe.
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()
motorcars dataset is described at https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/mtcars.html. Let's start by loading ...