Three Ways to Program in Python With RStudio

This is a companion discussion topic for the original entry at https://www.rstudio.com/blog/three-ways-to-program-in-python-with-rstudio


Photo by Timothy Dykes on Unsplash

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:

  • The RStudio environment pane displays the contents of Python modules.
  • Explore Python objects either by calling the View() function or by using the associated right-most buttons in the Environment pane.
  • The RStudio IDE presents matplotlib and seaborn plots within the Viewer pane.

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:

  • Better collaboration: Data scientists use the same back-end infrastructure, which makes it easier for them to share files, data, libraries, and other resources.
  • Concurrent sessions: RStudio Workbench enables users to have multiple concurrent R or Python sessions on a single server or a load-balanced cluster of servers.
  • Security: Python programmers can work in securely-managed session sandboxes.

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:

  • Manage your Python packages: With RStudio Package Manager, you can mirror the Python Package Index (PyPI) to organize and centralize packages behind your firewall.

  • Share Python content via RStudio Connect: Need to publish and share your Python content? RStudio Connect allows data scientists to publish Jupyter Notebooks, Flask applications, Shiny applications that call Python scripts, and much more.

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:

This topic was automatically closed after 179 days. New replies are no longer allowed.


If you have a query related to it or one of the replies, start a new topic and refer back with a link.