Lightweight bridge for calling R functions from Python
Project description
rtopy
Lightweight Python bridge for calling R functions with seamless type conversion.
Features
- Simple API: Call R functions with native Python types
- Auto type conversion: Supports int, float, str, bool, list, dict, numpy, pandas
- Minimal dependencies: Only requires R + jsonlite package
- Library support: Use any R package in your functions
- Fast: Direct subprocess execution, no rpy2 overhead
Installation
# Install package
pip install rtopy
# Install with optional numpy/pandas support
pip install rtopy[full]
# Requires R to be installed and in PATH
# Install R from: https://cran.r-project.org/
# Install jsonlite in R: install.packages("jsonlite")
Quick Start
from rtopy import RBridge, call_r
# One-off function call
result = call_r(
r_code="add <- function(x, y) x + y",
r_func="add",
x=5,
y=3
)
print(result) # 8.0
# Reusable bridge
rb = RBridge()
# Statistical analysis
code = '''
library(stats)
analyze <- function(data) {
list(
mean = mean(data),
median = median(data),
sd = sd(data)
)
}
'''
stats = rb.call(code, "analyze", return_type="dict", data=[1,2,3,4,5])
print(stats) # {'mean': 3.0, 'median': 3.0, 'sd': 1.58...}
# Return as pandas DataFrame
code = '''
make_data <- function(n) {
data.frame(
x = 1:n,
y = rnorm(n),
group = sample(c("A", "B"), n, replace=TRUE)
)
}
'''
df = rb.call(code, "make_data", return_type="pandas", n=100)
Return Types
"auto": Automatically infer best type (default)"int","float","str","bool": Python scalars"list","dict": Python collections"numpy": NumPy array (requires numpy)"pandas": pandas DataFrame/Series (requires pandas)"raw": Raw JSON-parsed output
Advanced Usage
# Custom timeout and verbose mode
rb = RBridge(timeout=60, verbose=True)
# Use any R package
code = '''
library(dplyr)
library(ggplot2)
process_data <- function(values) {
df <- data.frame(x = values)
df %>%
mutate(squared = x^2) %>%
summarise(mean_x = mean(x), mean_squared = mean(squared))
}
'''
result = rb.call(code, "process_data", data=[1, 2, 3, 4, 5])
# Pass NumPy arrays and pandas DataFrames
import numpy as np
import pandas as pd
arr = np.array([1, 2, 3, 4, 5])
result = rb.call(code, "my_func", data=arr)
df = pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})
result = rb.call(code, "another_func", df=df)
Requirements
- Python >= 3.7
- R >= 3.6
- R package: jsonlite
Optional:
- numpy >= 1.19 (for numpy return type)
- pandas >= 1.1 (for pandas return type)
License
BSD License Clause Clear
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
rtopy-0.2.0.tar.gz
(42.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
rtopy-0.2.0-py2.py3-none-any.whl
(12.5 kB
view details)
File details
Details for the file rtopy-0.2.0.tar.gz.
File metadata
- Download URL: rtopy-0.2.0.tar.gz
- Upload date:
- Size: 42.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ddc9bc40f723015de0c9570da09f2c1a7ad37d738b26878b37bd417b78b97f9
|
|
| MD5 |
d489e519dc91064179de4dbb2351c047
|
|
| BLAKE2b-256 |
946503f54eee39d40945854e90c34cf266e6876ca51e880ad1fa7bff561d3b74
|
File details
Details for the file rtopy-0.2.0-py2.py3-none-any.whl.
File metadata
- Download URL: rtopy-0.2.0-py2.py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0703e94a07941e672ba09f33d09b407cd2e3b8c86e36b8d0de1991eab30f482c
|
|
| MD5 |
e5ff38af22d35e8ad607f8b6075715e5
|
|
| BLAKE2b-256 |
d120590fbac27b6d5eca16c9a0405e8f2d933af9e190212af9d55105081a6ada
|