• Stars
    star
    1,656
  • Rank 28,122 (Top 0.6 %)
  • Language
    R
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

R Interface to Python

R Interface to Python

R-CMD-check

The reticulate package provides a comprehensive set of tools for interoperability between Python and R. The package includes facilities for:

reticulated python

  • Calling Python from R in a variety of ways including R Markdown, sourcing Python scripts, importing Python modules, and using Python interactively within an R session.

  • Translation between R and Python objects (for example, between R and Pandas data frames, or between R matrices and NumPy arrays).

  • Flexible binding to different versions of Python including virtual environments and Conda environments.

Reticulate embeds a Python session within your R session, enabling seamless, high-performance interoperability. If you are an R developer that uses Python for some of your work or a member of data science team that uses both languages, reticulate can dramatically streamline your workflow!

Getting started

Installation

Install the reticulate package from CRAN as follows:

install.packages("reticulate")

Python version

By default, reticulate uses an isolated python virtual environment named "r-reticulate".

The use_python() function enables you to specify an alternate python, for example:

library(reticulate)
use_python("/usr/local/bin/python")

The use_virtualenv() and use_condaenv() functions enable you to specify versions of Python in virtual or Conda environments, for example:

library(reticulate)
use_virtualenv("myenv")

See the article on Python Version Configuration for additional details.

Python packages

You can install any required Python packages using standard shell tools like pip and conda. Alternately, reticulate includes a set of functions for managing and installing packages within virtualenvs and Conda environments. See the article on Installing Python Packages for additional details.

Calling Python

There are a variety of ways to integrate Python code into your R projects:

  1. Python in R Markdown โ€” A new Python language engine for R Markdown that supports bi-directional communication between R and Python (R chunks can access Python objects and vice-versa).

  2. Importing Python modules โ€” The import() function enables you to import any Python module and call itโ€™s functions directly from R.

  3. Sourcing Python scripts โ€” The source_python() function enables you to source a Python script the same way you would source() an R script (Python functions and objects defined within the script become directly available to the R session).

  4. Python REPL โ€” The repl_python() function creates an interactive Python console within R. Objects you create within Python are available to your R session (and vice-versa).

Each of these techniques is explained in more detail below.

Python in R Markdown

The reticulate package includes a Python engine for R Markdown with the following features:

  1. Run Python chunks in a single Python session embedded within your R session (shared variables/state between Python chunks)

  2. Printing of Python output, including graphical output from matplotlib.

  3. Access to objects created within Python chunks from R using the py object (e.g. py$x would access an x variable created within Python from R).

  4. Access to objects created within R chunks from Python using the r object (e.g. r.x would access to x variable created within R from Python)

Built in conversion for many Python object types is provided, including NumPy arrays and Pandas data frames. For example, you can use Pandas to read and manipulate data then easily plot the Pandas data frame using ggplot2:

Note that the reticulate Python engine is enabled by default within R Markdown whenever reticulate is installed.

See the R Markdown Python Engine documentation for additional details.

Importing Python modules

You can use the import() function to import any Python module and call it from R. For example, this code imports the Python os module and calls the listdir() function:

library(reticulate)
os <- import("os")
os$listdir(".")
 [1] ".git"             ".gitignore"       ".Rbuildignore"    ".RData"
 [5] ".Rhistory"        ".Rproj.user"      ".travis.yml"      "appveyor.yml"
 [9] "DESCRIPTION"      "docs"             "external"         "index.html"
[13] "index.Rmd"        "inst"             "issues"           "LICENSE"
[17] "man"              "NAMESPACE"        "NEWS.md"          "pkgdown"
[21] "R"                "README.md"        "reticulate.Rproj" "src"
[25] "tests"            "vignettes"

Functions and other data within Python modules and classes can be accessed via the $ operator (analogous to the way you would interact with an R list, environment, or reference class).

Imported Python modules support code completion and inline help:

See Calling Python from R for additional details on interacting with Python objects from within R.

Sourcing Python scripts

You can source any Python script just as you would source an R script using the source_python() function. For example, if you had the following Python script flights.py:

import pandas
def read_flights(file):
  flights = pandas.read_csv(file)
  flights = flights[flights['dest'] == "ORD"]
  flights = flights[['carrier', 'dep_delay', 'arr_delay']]
  flights = flights.dropna()
  return flights

Then you can source the script and call the read_flights() function as follows:

source_python("flights.py")
flights <- read_flights("flights.csv")

library(ggplot2)
ggplot(flights, aes(carrier, arr_delay)) + geom_point() + geom_jitter()

See the source_python() documentation for additional details on sourcing Python code.

Python REPL

If you want to work with Python interactively you can call the repl_python() function, which provides a Python REPL embedded within your R session. Objects created within the Python REPL can be accessed from R using the py object exported from reticulate. For example:

Enter exit within the Python REPL to return to the R prompt.

Note that Python code can also access objects from within the R session using the r object (e.g. r.flights). See the repl_python() documentation for additional details on using the embedded Python REPL.

Type conversions

When calling into Python, R data types are automatically converted to their equivalent Python types. When values are returned from Python to R they are converted back to R types. Types are converted as follows:

R Python Examples
Single-element atomic vector Scalar 1, 1L, TRUE, "foo"
Unnamed list or multi-element atomic vector List c(1.0, 2.0, 3.0), c(1L, 2L, 3L)
Named list Dict list(a = 1L, b = 2.0), dict(x = x_data)
Matrix/Array NumPy ndarray matrix(c(1,2,3,4), nrow = 2, ncol = 2)
Data Frame Pandas DataFrame data.frame(x = c(1,2,3), y = c("a", "b", "c"))
Function Python function function(x) x + 1
NULL, TRUE, FALSE None, True, False NULL, TRUE, FALSE

If a Python object of a custom class is returned then an R reference to that object is returned. You can call methods and access properties of the object just as if it was an instance of an R reference class.

Learning more

The following articles cover the various aspects of using reticulate:

  • Calling Python from R โ€” Describes the various ways to access Python objects from R as well as functions available for more advanced interactions and conversion behavior.

  • R Markdown Python Engine โ€” Provides details on using Python chunks within R Markdown documents, including how call Python code from R chunks and vice-versa.

  • Python Version Configuration โ€” Describes facilities for determining which version of Python is used by reticulate within an R session.

  • Installing Python Packages โ€” Documentation on installing Python packages from PyPI or Conda, and managing package installations using virtualenvs and Conda environments.

  • Using reticulate in an R Package โ€” Guidelines and best practices for using reticulate in an R package.

  • Arrays in R and Python โ€” Advanced discussion of the differences between arrays in R and Python and the implications for conversion and interoperability.

  • Python Primer โ€” Introduction to Python for R users.

Why reticulate?

From the Wikipedia article on the reticulated python:

The reticulated python is a species of python found in Southeast Asia. They are the worldโ€™s longest snakes and longest reptilesโ€ฆThe specific name, reticulatus, is Latin meaning โ€œnet-likeโ€, or reticulated, and is a reference to the complex colour pattern.

From the Merriam-Webster definition of reticulate:

1: resembling a net or network; especially : having veins, fibers, or lines crossing a reticulate leaf. 2: being or involving evolutionary change dependent on genetic recombination involving diverse interbreeding populations.

The package enables you to reticulate Python code into R, creating a new breed of project that weaves together the two languages.

More Repositories

1

cheatsheets

Posit Cheat Sheets - Can also be found at https://posit.co/resources/cheatsheets/.
TeX
5,758
star
2

shiny

Easy interactive web applications with R
R
5,341
star
3

rstudio

RStudio is an integrated development environment (IDE) for R
Java
4,432
star
4

bookdown

Authoring Books and Technical Documents with R Markdown
JavaScript
3,743
star
5

rmarkdown

Dynamic Documents for R
R
2,737
star
6

gt

Easily generate information-rich, publication-quality tables from R
R
1,985
star
7

shiny-examples

JavaScript
1,959
star
8

blogdown

Create Blogs and Websites with R Markdown
R
1,724
star
9

webinars

Code and slides for RStudio webinars
HTML
1,510
star
10

rticles

LaTeX Journal Article Templates for R Markdown
TeX
1,402
star
11

plumber

Turn your R code into a web API.
R
1,390
star
12

tensorflow

TensorFlow for R
R
1,325
star
13

renv

renv: Project environments for R.
R
995
star
14

pagedown

Paginate the HTML Output of R Markdown with CSS for Print
R
883
star
15

shinydashboard

Shiny Dashboarding framework
CSS
852
star
16

pointblank

Data quality assessment and metadata reporting for data frames and database tables
R
845
star
17

keras3

R Interface to Keras
R
834
star
18

flexdashboard

Easy interactive dashboards for R
JavaScript
811
star
19

leaflet

R Interface to Leaflet Maps
JavaScript
799
star
20

rmarkdown-book

R Markdown: The Definitive Guide (published by Chapman & Hall/CRC in July 2018)
RMarkdown
738
star
21

rstudio-conf

Materials for rstudio::conf
HTML
721
star
22

shiny-server

Host Shiny applications over the web.
JavaScript
712
star
23

ggvis

Interactive grammar of graphics for R
R
709
star
24

learnr

Interactive Tutorials with R Markdown
R
709
star
25

RStartHere

A guide to some of the most useful R Packages that we know about
R
662
star
26

py-shiny

Shiny for Python
Python
627
star
27

DT

R Interface to the jQuery Plug-in DataTables
JavaScript
599
star
28

rmarkdown-cookbook

R Markdown Cookbook. A range of tips and tricks to make better use of R Markdown.
RMarkdown
577
star
29

blastula

Easily send great-looking HTML email messages from R
R
541
star
30

r2d3

R Interface to D3 Visualizations
R
516
star
31

bookdown-demo

A minimal book example using bookdown
CSS
476
star
32

hex-stickers

RStudio hex stickers
R
463
star
33

bslib

Tools for theming Shiny and R Markdown via Bootstrap 3, 4, or 5.
SCSS
461
star
34

distill

Distill for R Markdown
HTML
423
star
35

packrat

Packrat is a dependency management system for R
R
394
star
36

tufte

Tufte Styles for R Markdown Documents
R
385
star
37

dygraphs

R interface to dygraphs
JavaScript
365
star
38

revealjs

R Markdown Format for reveal.js Presentations
JavaScript
316
star
39

pins-r

Pin, discover, and share resources
R
313
star
40

fontawesome

Easily insert FontAwesome icons into R Markdown docs and Shiny apps
R
294
star
41

crosstalk

Inter-htmlwidget communication for R (with and without Shiny)
JavaScript
287
star
42

tinytex-releases

Windows/macOS/Linux binaries and installation methods of TinyTeX
PowerShell
251
star
43

config

config package for R
R
247
star
44

pool

Object Pooling in R
R
244
star
45

thematic

Theme ggplot2, lattice, and base graphics based on a few simple settings.
R
242
star
46

Intro

Course materials for "Introduction to Data Science with R", a video course by RStudio and O'Reilly Media
R
234
star
47

shinytest

Automated testing for shiny apps
JavaScript
225
star
48

shinymeta

Record and expose Shiny app logic using metaprogramming
R
222
star
49

nomnoml

Sassy 'UML' Diagrams for R
JavaScript
221
star
50

httpuv

HTTP and WebSocket server package for R
C
217
star
51

shinyuieditor

A GUI for laying out a Shiny application that generates clean and human-readable UI code
JavaScript
212
star
52

htmltools

Tools for HTML generation and output
R
201
star
53

promises

A promise library for R
R
201
star
54

vetiver-r

Version, share, deploy, and monitor models
R
181
star
55

rstudioapi

Safely access RStudio's API (when available)
R
161
star
56

concept-maps

Concept maps for all things data science
HTML
161
star
57

gradethis

Tools for teachers to use with learnr
R
161
star
58

chromote

Chrome Remote Interface for R
R
155
star
59

master-the-tidyverse

Course contents for Master the Tidyverse
155
star
60

shinythemes

Themes for Shiny
R
152
star
61

ShinyDeveloperConference

Materials collected from the First Shiny Developer Conference Palo Alto, CA January 30-31 2016
HTML
152
star
62

shiny-gallery

Code and other documentation for apps in the Shiny Gallery โœจ
HTML
147
star
63

sortable

R htmlwidget for Sortable.js
R
124
star
64

reactlog

Shiny Reactivity Visualizer
JavaScript
121
star
65

r-docker

Docker images for R
Dockerfile
121
star
66

rsconnect

Publish Shiny Applications, RMarkdown Documents, Jupyter Notebooks, Plumber APIs, and more
R
120
star
67

redx

dynamic nginx configuration
Lua
118
star
68

bigdataclass

Two-day workshop that covers how to use R to interact databases and Spark
R
114
star
69

r-system-requirements

System requirements for R packages
Shell
111
star
70

shinyloadtest

Tools for load testing Shiny applications
HTML
110
star
71

shinyapps

Deploy Shiny applications to ShinyApps
110
star
72

webshot2

Take screenshots of web pages from R
R
109
star
73

shinyvalidate

Input validation package for the Shiny web framework
JavaScript
108
star
74

shinytest2

R
103
star
75

miniUI

R
102
star
76

sass

Sass compiler package for R
C++
102
star
77

keras-customer-churn

Customer Churn Shiny Application
R
99
star
78

r-builds

an opinionated environment for compiling R
Shell
95
star
79

r-manuals

A re-styled version of the R manuals
R
88
star
80

addinexamples

An R package showcasing how RStudio addins can be registered and used.
R
86
star
81

shinyapps-package-dependencies

Collection of bash scripts that install R package system dependencies
R
74
star
82

markdown

The first generation of Markdown rendering for R (born in 2012). Originally based on the C library sundown. Now based on commonmark. Note that this package is markdown, not *rmarkdown*.
R
72
star
83

webdriver

WebDriver client in R
R
69
star
84

R-Websockets

HTML 5 Websockets implementation for R
R
68
star
85

beyond-dashboard-fatigue

Materials for the RStudio webinar 'Beyond Dashboard Fatigue'
R
66
star
86

cloudml

R interface to Google Cloud Machine Learning Engine
R
65
star
87

rstudio-docker-products

Docker images for RStudio Professional Products
Shell
64
star
88

shinylive

Run Shiny on Python (compiled to wasm) in the browser
TypeScript
61
star
89

rstudio-conf-2022-program

rstudio::conf(2022, "program")
R
60
star
90

bookdown.org

Source documents to generate the bookdown.org website
R
59
star
91

vetiver-python

Version, share, deploy, and monitor models.
Python
59
star
92

education.rstudio.com

CSS
58
star
93

tfestimators

R interface to TensorFlow Estimators
R
57
star
94

connections

https://rstudio.github.io/connections/
R
56
star
95

tfprobability

R interface to TensorFlow Probability
R
54
star
96

sparkDemos

HTML
53
star
97

swagger

Swagger is a collection of HTML, Javascript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
HTML
53
star
98

shiny-incubator

Examples and ideas that don't belong in the core Shiny package and aren't officially supported.
JavaScript
53
star
99

pins-python

Python
50
star
100

leaflet.mapboxgl

Extends the R Leaflet package with a Mapbox GL JS plugin to allow easy drawing of vector tile layers.
R
50
star