| Title: | Call Python (Pyodide) from R in webR, Like 'reticulate' |
|---|---|
| Description: | A 'reticulate'-style interface for calling Python from R when R runs in the web browser through 'webR' (R compiled to WebAssembly). Python runs in 'Pyodide' (CPython compiled to WebAssembly) in its own web worker, and R talks to it synchronously through 'webR' worker messaging. Supports running code, importing modules, calling functions, converting vectors, lists, matrices and data frames to and from 'NumPy' and 'pandas', installing packages with 'micropip', and several independent Python sessions. |
| Authors: | John Muschelli [aut, cre] (ORCID: <https://orcid.org/0000-0001-6469-1750>) |
| Maintainer: | John Muschelli <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-10-29 21:58:30 UTC |
| Source: | https://github.com/muschellij2/rpyodide |
Runs rpyodide's Python bridge in a regular Python process instead of Pyodide, talking to it over stdin and stdout. Everything else works the same way, so code written for rpyodide (and rpyodide itself) can be tested outside webR, for example on CRAN or in continuous integration. This needs the processx package and Python 3.8 or later.
cpython_start(name = "cpython", python = find_python(), activate = TRUE) find_python()cpython_start(name = "cpython", python = find_python(), activate = TRUE) find_python()
name |
Session name. |
python |
Path to the Python executable. By default, the
|
activate |
Make this the active session? |
Differences from Pyodide sessions: packages come from the Python
installation (use pip; py_install() and py_require() can't install
them), files aren't copied because Python shares R's file system, and the
Python version is the local one.
The session, invisibly.
find_python(): The Python executable cpython_start() uses by
default, or "" if there is none.
pyodide_start() for Python in webR.
if (nzchar(find_python()) && requireNamespace("processx", quietly = TRUE)) { cpython_start("example") py_eval("1 + 1", session = "example") pyodide_stop("example") }if (nzchar(find_python()) && requireNamespace("processx", quietly = TRUE)) { cpython_start("example") py_eval("1 + 1", session = "example") pyodide_stop("example") }
Pyodide is loaded from this URL unless pyodide_url is given to
pyodide_start(). Set options(rpyodide.pyodide_url = ...) to change it.
The URL must end in / and contain pyodide.mjs.
default_pyodide_url()default_pyodide_url()
A character string.
Imports a module, loading it from the Pyodide distribution first if
necessary. Use $ to get its attributes.
import(module, convert = TRUE, session = NULL) import_main(convert = TRUE, session = NULL) import_builtins(convert = TRUE, session = NULL)import(module, convert = TRUE, session = NULL) import_main(convert = TRUE, session = NULL) import_builtins(convert = TRUE, session = NULL)
module |
Module name, e.g. |
convert |
Convert values returned from this module to R? |
session |
Pyodide session; defaults to the active one. |
A Python module object.
## Not run: np <- import("numpy") np$linspace(0, 1, 5L) ## End(Not run)## Not run: np <- import("numpy") np$linspace(0, 1, 5L) ## End(Not run)
Is an object a Python object?
is_py_object(x)is_py_object(x)
x |
An R object. |
TRUE if x refers to an object in a Pyodide session.
Is R running in webR?
is_webr()is_webr()
TRUE when R is webR (R compiled to WebAssembly).
__main__ modulepy$name gets a variable from Python's __main__ module in the active
session, and py$name <- value sets one.
pypy
An object of class pyodide_main.
Is Python available?
py_available(initialize = FALSE)py_available(initialize = FALSE)
initialize |
Start the default session if none is running? |
TRUE if a Pyodide session is running (or was started).
Python configuration
py_config(session = NULL)py_config(session = NULL)
session |
Pyodide session; defaults to the active one. |
A list with the Python and Pyodide versions and loaded packages.
Installs packages into a Pyodide session with micropip. Packages built
for Pyodide (such as numpy, pandas and scikit-learn) come from the Pyodide
distribution; others come from PyPI and must be pure Python or have
WebAssembly wheels.
py_install(packages, deps = TRUE, session = NULL)py_install(packages, deps = TRUE, session = NULL)
packages |
Package names, optionally with version specifiers. |
deps |
Also install the packages' dependencies? Use |
session |
Pyodide session; defaults to the active one. |
NULL, invisibly.
The last Python error
py_last_error()py_last_error()
A list with the Python exception type, message and traceback,
or NULL if there hasn't been an error.
List installed Python packages
py_list_packages(session = NULL)py_list_packages(session = NULL)
session |
Pyodide session; defaults to the active one. |
A data frame of package names, versions and sources.
Is a Python module available?
py_module_available(module, session = NULL)py_module_available(module, session = NULL)
module |
Module name. |
session |
Pyodide session; defaults to the active one. |
TRUE if the module can be imported, either because it is
installed or because it is in the Pyodide distribution.
Low-level accessors for Python objects. Usually you use $, [[ and [
instead: x$attr, x[["attr"]] and x[key] (Python x[key], 0-based).
py_get_attr(x, name) py_set_attr(x, name, value) py_has_attr(x, name) py_list_attributes(x) py_get_item(x, key) py_set_item(x, key, value) py_call(x, ...) py_len(x) py_str(x) py_repr(x)py_get_attr(x, name) py_set_attr(x, name, value) py_has_attr(x, name) py_list_attributes(x) py_get_item(x, key) py_set_item(x, key, value) py_call(x, ...) py_len(x) py_str(x) py_repr(x)
x |
A Python object. |
name |
Attribute name. |
value |
An R value or Python object. |
key |
An item key or index. |
... |
Arguments for the call; named arguments become keyword arguments. |
Like reticulate::py_require(): declare the Python packages (and Python
version) that your code needs, before or after Python has started.
Requirements are kept per session and accumulate across calls.
py_require( packages = NULL, python_version = NULL, deps = TRUE, action = c("add", "remove", "set"), session = NULL )py_require( packages = NULL, python_version = NULL, deps = TRUE, action = c("add", "remove", "set"), session = NULL )
packages |
Package requirements, such as |
python_version |
Python version constraints, such as |
deps |
Install the packages' dependencies too? Packages required with
|
action |
|
session |
Session name. Defaults to the active session, or
|
Before the session starts, requirements are only recorded. When
pyodide_start() runs (or Python is first used), they are all installed
with a single micropip call and the Python version is checked.
After the session has started, new packages are installed right away.
Packages that are already loaded can't be removed, so action = "remove"
and action = "set" must not drop packages from a running session; stop
it with pyodide_stop() first.
Requirements survive pyodide_stop(), so restarting a session installs them
again.
Each Pyodide release ships exactly one Python version, so python_version
can't choose an interpreter. It is a check: if the session's Python doesn't
satisfy it, py_require() (or pyodide_start()) fails. To get a different
Python, start the session with a different pyodide_url.
With no packages or python_version, the session's requirements.
Otherwise they are returned invisibly.
py_require(c("numpy", "pandas>=2"), session = "example") py_require(python_version = ">=3.12", session = "example") py_require(session = "example")py_require(c("numpy", "pandas>=2"), session = "example") py_require(python_version = ">=3.12", session = "example") py_require(session = "example")
py_run_string() runs code in the __main__ module, like
reticulate::py_run_string(). py_eval() evaluates a single expression and
returns its value. Pyodide packages that the code imports (for example
numpy) are loaded automatically. Anything printed by Python appears in R.
py_run_string(code, convert = TRUE, session = NULL) py_run_file(file, convert = TRUE, session = NULL) py_eval(code, convert = TRUE, session = NULL)py_run_string(code, convert = TRUE, session = NULL) py_run_file(file, convert = TRUE, session = NULL) py_eval(code, convert = TRUE, session = NULL)
code |
Python code. |
convert |
Convert results to R values where possible? If |
session |
Pyodide session; defaults to the active one. |
file |
A file in webR's virtual file system. |
py_run_string() and py_run_file() return the __main__ module,
invisibly. py_eval() returns the value of the expression.
webR and Pyodide have separate virtual file systems. Files whose absolute
paths are passed to Python as arguments are copied automatically (see
the rpyodide.mirror_files option); use py_upload() to copy other files,
such as Python modules to import.
py_upload(from, to = normalizePath(from, mustWork = TRUE), session = NULL)py_upload(from, to = normalizePath(from, mustWork = TRUE), session = NULL)
from |
Path of a file in webR's file system. |
to |
Path in Pyodide's file system. Directories are created as needed. |
session |
Pyodide session; defaults to the active one. |
to, invisibly.
List Pyodide sessions
pyodide_sessions()pyodide_sessions()
A data frame with one row per session.
Starts a Python interpreter (Pyodide) in its own web worker. Each session is
an independent Python environment with its own packages and globals, so
several sessions can act like separate virtual environments. Python
functions use the active session unless told otherwise. A "default"
session is started automatically the first time Python is used.
pyodide_start( name = "default", packages = character(), pyodide_url = default_pyodide_url(), lockfile_url = NULL, activate = TRUE, timeout = 300 )pyodide_start( name = "default", packages = character(), pyodide_url = default_pyodide_url(), lockfile_url = NULL, activate = TRUE, timeout = 300 )
name |
Session name. |
packages |
Packages to require for this session; they are added to
its |
pyodide_url |
URL of the Pyodide distribution. |
lockfile_url |
Optional URL of a |
activate |
Make this the active session? |
timeout |
Seconds to wait for Pyodide to start. |
The session, invisibly.
Terminates the session's worker (or, for cpython_start() sessions, its
Python process). Python objects from that session can no longer be used.
pyodide_stop(name = .rpyodide$active)pyodide_stop(name = .rpyodide$active)
name |
Session name. |
NULL, invisibly.
r_to_py() converts an R value to a Python object; py_to_r() converts a
Python object to an R value. Vectors of length one become Python scalars,
longer vectors become lists, named vectors and lists become dicts, matrices
and arrays become NumPy arrays and data frames become pandas DataFrames.
Use I() to send a length-one vector as a list.
r_to_py(x, convert = FALSE, session = NULL) py_to_r(x)r_to_py(x, convert = FALSE, session = NULL) py_to_r(x)
x |
An R value or Python object. |
convert |
For |
session |
Pyodide session; defaults to the active one. |
r_to_py() returns a Python object; py_to_r() returns an R value.
Python tuples and dicts
tuple(..., session = NULL) dict(..., session = NULL)tuple(..., session = NULL) dict(..., session = NULL)
... |
Values for the tuple, or |
session |
Pyodide session; defaults to the active one. |
A Python object.
Choose the active Pyodide session
use_pyodide(name, ...)use_pyodide(name, ...)
name |
Name of a session started with |
... |
Passed to |
The previously active session name, invisibly.