5

How to Use R and Python Together? Try These 2 Packages

 2 years ago
source link: https://www.r-bloggers.com/2022/03/how-to-use-r-and-python-together-try-these-2-packages/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to Use R and Python Together? Try These 2 Packages

Posted on March 22, 2022 by Dario Radečić in R bloggers | 0 Comments

[This article was first published on r – Appsilon | Enterprise R Shiny Dashboards, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
How to use R and Python together Article Thumbnail

Data science is vastly different than programming. We use only four languages – R, Python, Julia, and SQL. Now, SQL is non-negotiable, as every data scientist must be proficient in it. Julia is still the new kid on the block. Many argue which is better – Python or R? But today, we ask a different question – how can you use R and Python together?

It might seem crazy at first, but hear us out. Both Python and R are stable languages used by many data scientists. Even seasoned package developers, such as Hadley Wickham, borrow from

BeauftifulSoup
BeauftifulSoup (Python) to make
Rvest
Rvest (R) web scraping packages. Reinventing the wheel makes no sense.

Today we’ll explore a couple of options you have if you want to use R and Python together in the same project. Let’s start with options for Python users.

Table of contents:


How to Call R Scripts from Python

Using R and Python together at the same time is incredibly easy if you already have your R scripts prepared. Calling them from Python boils down to a single line of code. Let’s cover the R script before diving further.

It’s really a simple one, as it only prints some dummy text to the console:

print("Hello from R script!")

On the Python end, you’ll need to use the

subprocess
subprocess module to run a shell command. All R scripts can be run with the
Rscript <script-path>
Rscript <script-path> call:

import subprocess

res = subprocess.call("Rscript /Users/dradecic/Desktop/script.R", shell=True) res

Below you’ll see the output:

Image 1 - Running an R script from Python

Image 1 – Running an R script from Python

The line was successfully printed to the console, and a zero was returned. That’s the thing – this approach is excellent if your R script performs tasks one after the other. It falls short if you want to use the output from R code in Python.

It’s a shortcoming that the next option for using R and Python together addresses.

How to Run R Code from Python with rpy2

Now we’ll dive into the good stuff. You’ll have to install the rpy2 package in Python to follow along. It’s assumed you also have R installed and configured.

To start, we’ll use the

robjects
robjects submodule to access R objects, such as the number PI:

from rpy2 import robjects

pi = robjects.r['pi'] pi

Here’s what’s stored in the variable:

Image 2 - PI R object

Image 2 – PI R object

You can check its type. It’s an R-specific float vector:

Image 3 - Type of the R PI object

Image 3 – Type of the R PI object

There’s a lot more you can do than access individual R objects. For example, you can also declare and run R functions. The code snippet below shows you how to declare a function for adding numbers and call it two times. Just to be extra careful, make sure to surround the R code with triple quotation marks:

robjects.r(''' add_nums <- function(x, y) { return(x + y) } print(add_nums(x = 5, y = 10)) print(add_nums(x = 10, y = 20)) ''')

Here’s the output from the above code snippet:

Image 4 - Calling R function in Python

Image 4 – Calling R function in Python

Many times you won’t find the built-in R packages enough for your specific use case. You can install additional, external R packages through Python with the

rpackages
rpackages submodule:

import rpy2.robjects.packages as rpackages

utils = rpackages.importr('utils') utils.chooseCRANmirror(ind=1)

utils.install_packages('<package_name>')

Dataframes

There’s also an option to work with R dataframes in Python. The code snippet below shows you how to import the

datasets
datasets subpackage and access the well-known MTcars dataset:

from rpy2.robjects.packages import importr, data

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

Here’s what the dataset looks like when displayed in Python:

Image 5 - MTcars dataset as a data frame

Image 5 – MTcars dataset as a data frame

Visualization

And for the last bit, we’ll show you how to visualize the dataset with R’s

ggplot2
ggplot2 package. As of now, you can’t display the figures directly in the notebook, so you’ll need to save the figure to a file using the
grDevices
grDevices package. The code responsible for plotting should go between the call to
grdevices.png()
grdevices.png() and
grdevices.dev_off()
grdevices.dev_off(), so keep that in mind for future reference:

from rpy2.robjects.packages import importr, data import rpy2.robjects.lib.ggplot2 as ggplot2

grdevices = importr('grDevices') grdevices.png(file="/Users/dradecic/Desktop/mtcars.png", width=1024, height=512) datasets = importr('datasets') mtcars = data(datasets).fetch('mtcars')['mtcars']

pp = (ggplot2.ggplot(mtcars) + ggplot2.aes_string(x='wt', y='mpg', col='factor(cyl)') + ggplot2.geom_point()) pp.plot()

grdevices.dev_off()

Image 6 - Using ggplot2 in Python

Image 6 – Using ggplot2 in Python

And that’s how you can use R and Python together at the same time by running R code from Python. Let’s reverse the roles next and explore options for R users.

Looking to style your scatter plots? Read our comprehensive guide to stunning scatter plots with R and ggplot2.

How to Call Python Scripts from R

R users have an even easier time running scripts from the opposite programming language. You’ll have to install the

reticulate
reticulate package if you want to follow along, as it’s responsible for running Python scripts and configuring Python environments.

First things first, let’s write a Python script. It will be a simple one, as it prints a single line to the console:

print("Hello from Python script")

In R, you’ll have to import the

reticulate
reticulate package and call the
py_run_file()
py_run_file() function with a path to the Python script provided:

library(reticulate)

py_run_file("/Users/dradecic/Desktop/script.py")

Here’s the output displayed in the R console:

Image 7 - Running Python scripts from R

Image 7 – Running Python scripts from R

As you can see, everything works as advertised. You can go one step further and use a specific Python version, virtual environment, or Anaconda environment. Use any of the three function calls below as a reference:

use_python("<python-path>") use_virtualenv("<environment-name>") use_condaenv("<conda-environment-name>")

Next, we’ll explore more advanced ways R users can use R and Python at the same time.

Can R programmers make Machine Learning models? Yes! Learn how with fast.ai in R

How to Run Python Code from R

The

reticulate
reticulate package comes with a Python engine you can use in R Markdown. Reticulate allows you to run chunks of Python code, print Python output, access Python objects, and so on.

To start, create a new R Markdown (Rmd) file and do the usual setup – library imports and Python location configuration:

```{r setup, include=FALSE, echo=TRUE} library(reticulate) use_condaenv("base") ```

You can now create either an R or a Python block by writing three backticks and specifying the language inside of curly brackets. We’ll start with Python. The code snippet below imports the Numpy library, declares an array, and prints it:

```{python} import numpy as np py_arr = np.array([1, 2, 3, 4, 5]) for item in py_arr: print(item, end=', ') ```

Image 8 - Python list printed in R Markdown

Image 8 – Python list printed in R Markdown

But what if you want to convert Python’s Numpy array to an R vector? As it turns out, you can access Python objects in R by prefixing the variable name with py$. Here’s an example:

```{r} r_arr <- as.vector(py$py_arr) print(r_arr) ```

Image 9 - Numpy array converted to R vector

Image 9 – Numpy array converted to R vector

As you would imagine, the possibilities from here are endless. We’ll now show you how to import the Pandas library, load in a dataset from GitHub, and print its first five rows:

```{python} import pandas as pd mtcars = pd.read_csv('https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv') mtcars.head() ```

Image 10 - Head of the MTcars dataset

Image 10 – Head of the MTcars dataset

Easy, right? You can import any Python library and write any Python code you want, and then access the variables and functions declared with R.

Python’s de-facto standard data visualization library is Matplotlib, and it’s also easy to use in R Markdown. Just remember to call the

plt.show()
plt.show() method, as the figure won’t be displayed otherwise:

```{python} import matplotlib.pyplot as plt plt.figure(figsize=(12, 6)) plt.scatter(mtcars['wt'], mtcars['mpg'], color='#000', s=10) plt.title('Mtcars - WT vs. MPG') plt.show() ```

Image 11 - Matplotlib chart in R Markdown

Image 11 – Matplotlib chart in R Markdown

And that’s how you can run Python code in R and R Markdown. That’s all we wanted to cover in today’s article, so let’s make a brief summary next.


Summary of Using R and Python Together

Today you’ve learned how to use R and Python together from the perspectives of both R and Python users. Hopefully, you can now combine the two languages to get the best of both worlds. For example, some R packages, such as

autoarima
autoarima have no direct competitor in Python. Reinventing the wheel doesn’t make sense. So don’t. Just preprocess the data with Python and model it with R.

Why don’t you give it a try as a homework assignment? Download the Airline passengers dataset, load and preprocess it in Python, and R’s

autoarima
autoarima package to make the forecasts. Share your results with us on Twitter – @appsilon. We’d love to see what you come up with.

Want to crack your upcoming Python and Data Science coding interview? Here are the top 7 questions you must know how to answer.

Article How to Use R and Python Together? Try These 2 Packages comes from Appsilon | Enterprise R Shiny Dashboards.

To leave a comment for the author, please follow the link and comment on their blog: r – Appsilon | Enterprise R Shiny Dashboards.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.


Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK