Skip navigation

How to Run Python in RStudio Using the Reticulate Library in R

This video explains how Reticulate makes it easier to use the strengths of both Python and R to produce stronger data analysis.

In this video, Pierre DeBois explains how R's Reticulate library can allow you to produce better data analysis by taking advantage of both R and Python.

By watching this video, you will learn how Reticulate, combined with Quarto, can allow you to quickly run Python scripts alongside R code within RStudio, granting you access to both R's and Python's powerful libraries and modules, making you a better data analyst.

The transcript has been lightly edited for clarity and length.


Pierre DeBois: Programmers often face a choice in what programming language to use — but when it comes to data, the choices are becoming blurrier and, to be honest, even more convenient, allowing you to use the strengths of multiple languages to produce even stronger analysis. At least that's what's happening with R and Python: R's Reticulate library allows data scientists to access Python.

Incorporating Reticulate Into an R Script

In particular, Reticulate can be used in three ways, the easiest and most basic of which is incorporating it within an R script.

For example, I have an object I'm creating using the dataset GT Cars from the library GT Cars. When I run it, you have this nice section of information on a number of exotic automobiles.

But suppose I want to create a Python object that I can use within Python? Well, if I run Reticulate and then select one of the functions, in this case, r_to_py, I now have a data frame — in this case it's called gtcars_python — that I can use within the context of a Python module or Python script.

With Reticulate, you can also run a Python script while in the R programming environment using the Reticulate function py_run_file. In this case, I'm running a very simple addition program created in Python. The functions starts the Python program, which asks me to enter values for the variables a and b, and then it will go ahead and run the script to return the sum of those two values.

RStudio and Quarto

You can also use RStudio, which is the integrated development environment (IDE) typically used with R programming, and you can run Reticulate from there. Within RStudio there is a global option, which allows you to look for different versions of Python and run Reticulate automatically.

The advantage is that if you have a version of Python that you want to use on your computer, you can select it. You can also select virtual environments. With RStudio you can actually identify which virtual environment you want to use knowing which modules are associated with it, and be able to have the right environment to match your needs.

Finally, you can also run R and Python together within a markdown document or, in this case, Quarto. Quarto is just an updated version of R Markdown, but Quarto is designed to have chunks where in one chunk, you run R programming and in another chunk you run Python. It does this in the background, relying on Reticulate to create the function and run.

For example, for R, again I have my data set from GT Cars, but then just below I can have a Python chunk, which can take that object and set it up as a data frame within Python, just like we did before.

The Benefits of Reticulate

You may be thinking, why would I want to do this? Well, there's a lot of times where you're cleaning data, trying to get it into the right table and format that you want, and you don't want to redo that for two different languages. Sometimes it's easier to do it for one and then see if you can carry it over to the other.

As I said earlier, there are a lot of libraries and modules for R and Python, respectively, some of which don't always overlap, but you might want the option of being able to port data from one area to the other to be able to run it and create the analysis that you need.

Use Reticulate as a basis for learning how to use R and Python and be an even better data analyst or manager.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.