Three Ways to Program in Python With RStudio

2021-12-06

Photo by Timothy Dykes on Unsplash

Note

Check out our Python Solutions page for the latest on integrating Posit tools and RStudio with Python.

RStudio is known for making excellent tools and packages for R programming. But did you know that you can use RStudio for Python programming, as well? Whether you want to use R and Python together or work solely in Python, there are a variety of ways for you to develop your code. You can:

Let’s explore these features using the Palmer Penguins dataset.

1. Run Python Scripts in the RStudio IDE

The RStudio IDE is a free and open-source IDE for Python, as well as R. You can write scripts, import modules, and interactively use Python within the RStudio IDE.

To get started writing Python in the RStudio IDE, go to File, New File, then Python Script. Code just as you would in an R script.

The RStudio IDE provides several useful tools for your Python development:

Running a Python script in the RStudio IDE

Need help remembering your Python function? RStudio also provides code completion for Python scripts:

Getting Python function help in the RStudio IDE

Learn more about RStudio IDE Tools for reticulate.

2. Use R and Python in a Single Project With the reticulate Package

Once installed, you can call Python in R scripts. In this example, we turn the Palmer Penguins dataset into a Pandas data frame. Then, we run the pandas.crosstab function.

Turning an R data frame to a Python pandas data frame

We can turn the Pandas data frame back into an R object:

Turning a Pandas data frame back to an R data frame

Interoperability works in R Markdown as well. Create and execute Python chunks in your .Rmd file:

```{python}
from palmerpenguins import load_penguins
penguins = load_penguins()
penguins.describe()
```

With reticulate, you can use Python in R packages, Shiny apps, and more.

Find out more about the R Markdown Python engine and how to call Python from R.

3. Use Your Python Editor of Choice Within RStudio Tools

RStudio tools, such as RStudio Workbench and RStudio Cloud, integrate with interfaces beyond the RStudio IDE.

RStudio Workbench

As your data science team grows, your tools need to scale as well. With RStudio Workbench, data scientists collaboratively work from a centralized server using their editor of choice: RStudio, JupyterLab, Jupyter Notebook, or VSCode.

Running a Python script in the RStudio IDE

Within the editor, data scientists can write Python code with:

In the RStudio IDE, data scientists can also collaborate in real time. When multiple users are active in the project at once, you can see each others’ activity and work together.

Learn more about RStudio Workbench.

RStudio Cloud

RStudio Cloud is a cloud-based solution that allows you to run, share, teach and learn Python. Jupyter Notebook projects are now available to Premium, Instructor, or Organization account holders. Once in RStudio Cloud, it is easy to install modules, share Jupyter notebooks, and run Python code:

Running a Python script in the RStudio IDE

Want to try out this notebook? Check it out in your browser (no paid subscription needed but login required).

Learn more about RStudio Cloud.

Conclusion

We want you to do your best work in your preferred environment and language. RStudio provides exciting options for your Python projects: Python scripts in the RStudio IDE, mixed language development with reticulate, and editor options in RStudio Cloud and RStudio Workbench.

Data science goes beyond coding. With RStudio, you can also:

See RStudio Connect and Jupyter notebooks in action during Tom Mock’s live webinar on Thursday, December 9th at 11-12 ET - Cut down on the grunt work and deliver insights more effectively with RStudio Connect, R Markdown, and Jupyter.

Read more about how RStudio can support your Python development: