Run R# lambda function in python workflow
Overview
This package enables seamless execution of R# lambda functions within Python workflows, supporting both local and Docker containerized environments. It handles configuration serialization, execution environment setup, and cross-platform compatibility.
Key Features
-
Multi-Environment Execution
- Local Execution: Direct execution via .NET R# runtime - Docker Execution: Containerized runs with automatic volume management
-
Automatic Configuration
- Parameter serialization to
.r_env/run.json - Runtime options serialization to
.r_env/options.json
- Parameter serialization to
-
Unified Interface
- Consistent API for local and containerized execution
- Support for both dictionary and CLI-style arguments
Installation
pip install r-lambda
System Requirements:
- Python ≥3.8
- .NET Runtime (for local execution)
- Docker Engine (for containerized execution)
Core Concepts
Introduce the R# lambda
The R# lambda function is a kind of shell feature function in R# script host. Assuming that there is a function named hello_world in a R# package which is named demo, so that we could run this hello world function in R# script file:
# In package "demo"
# function inside package namespace: demo
const hello_world = function(str) {
print(`hello: ${str}!`);
}
# call function in another script file
demo::hello_world("world");
Via the R# lambda function feature, that we could run this function without write script file, and we could run this function from commandline directly, example as:
# call function via R# lambda
Rscript --lambda demo::hello_world
and the parameter value of hello world function will be setup via a runtime json file ./.r_env/run.json in the working directory:
{
"str": "world"
}
If the function invoke contains getOption function calls, then you could setup the runtime environment via the json file ./.r_env/options.json in the working directory:
{
"example-var": "1234"
}
So, you could get such environment variable value in R# runtime environment by getOption:
print(getOption("example-var"));
# [1] "1234"
Configuration Files
| File | Purpose | Generated From |
|---|---|---|
| .r_env/run.json | Function parameters | argv argument |
| .r_env/options.json | Runtime environment settings | options argument |
API Reference
call_lambda(func, **kwargs)
Parameters:
| Parameter | Type | Description | Default |
|---|---|---|---|
| func | str | R# function in package::function format |
Required |
| argv | dict/list | Function parameters | {} |
| options | dict | Runtime configuration for getOption() |
{} |
| workdir | str | Execution working directory | "./" |
| docker | dict | Docker configuration (from docker_image()) |
None |
| run_debug | bool | Dry-run mode (no execution) | False |
Call R# lambda from a python workflow
For call a R# lambda function from your python workflow, then you could use this python package function. Current package call the R# workflow function running in local machine or running in a docker container already been wrapped into function calls, so that you can call such hello world function in R# package in python, example like:
import r_lambda
from r_lambda.docker import docker_image
r_lambda.call_lambda("demo::hello_world",
argv = {"str": "world"},
options = None,
workdir = "./",
docker = docker_image(id = "example_image",
volumn = ["/home"], name = "test"),
run_debug = False
)
Usage Examples
Basic Local Execution
from r_lambda import call_lambda
call_lambda(
func="demo::hello_world",
argv={"str": "world"}, # Dictionary-style arguments
options={"verbose": True},
workdir="./analysis"
)
CLI-Style Arguments
call_lambda(
"stats::calculate",
argv=["--input", "data.csv", "--threshold", "0.05"], # CLI-style arguments
options={"parallel": True}
)
Docker Execution
from r_lambda import call_lambda, docker_image
docker_config = docker_image(
id="rsharp/runtime:3.2",
volumn=["/data/inputs", "/data/outputs"], # Absolute paths required
shm_size="2g",
name="analysis_container"
)
call_lambda(
func="preprocess::clean_data",
argv={"input_dir": "/data/inputs", "output_dir": "/data/outputs"},
docker=docker_config
)
Advanced Configuration
Volume Mounting
The package automatically handles:
- Docker socket mounting (
/var/run/docker.sock) /tmpdirectory sharing- Argument-specified volumes from
docker_image() Example Mount:
docker_image(
id="rsharp/runtime:3.2",
volumn=["/host/path"], # Mounts to /host/path in container
...
)
Runtime Options
Configure environment variables accessible via getOption() in R#:
options = {
"max_memory": "16G",
"temp_dir": "/tmp/rsharp",
"threads": 8
}
Debugging & Testing
Dry-run Mode
call_lambda(
func="demo::test",
run_debug=True # Prints command without execution
)
Sample Output:
[DEBUG] Generated command:
docker run -v /data:/data ... rsharp/runtime:3.2 dotnet Rscript.dll --lambda demo::test
Configuration Inspection
Check generated files in workdir/.r_env:
run.json: Serialized argv parametersoptions.json: Serialized runtime configuration
Best Practices
- Path Handling
- Use absolute paths for Docker volumes
- Avoid spaces in directory names
- Windows: Use raw strings for paths (
"C:\data")
2. Resource Allocation
- Set shm_size for memory-intensive tasks
- Configure thread limits in options
3. Container Management
- Reuse containers for sequential calls
- Clean up temporary containers with --rm flag
Error Handling
Common Exit Codes:
| Code | Description |
|---|---|
| 0 | Success |
| 127 | R# runtime not found |
| 139 | Memory allocation error |
| 255 | Invalid function signature |
License & Support
MIT License. Report issues at GitHub Repo.
Metadata
Release files for r-lambda 1.1.2026.501
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| r_lambda-1.1.2026.501.tar.gz | 11.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| r_lambda-1.1.2026.501-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.6 kB
Release files / r_lambda-1.1.2026.501.tar.gz
| Download URL | r_lambda-1.1.2026.501.tar.gz |
|---|---|
| Size | 11.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d8c9050ab585b4cd5adef747e92361886348810e1df58641c074b8981dfe44a9
|
|
BLAKE2b-256 checksum How to use checksums |
180e9b4eaa2b671865f0df76d2d19333ffbbbc46da1f12626daa745688256569
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.7
|
Release files / r_lambda-1.1.2026.501-py3-none-any.whl
| Download URL | r_lambda-1.1.2026.501-py3-none-any.whl |
|---|---|
| Size | 10.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
31d6e3a4cb654c4c02057b1f14d994b7f08b98790852357cfe6e12969a248814
|
|
BLAKE2b-256 checksum How to use checksums |
823b4633cb6b6516b1c53bc2709f3c5a707def9d59b5462ab59b34ec4987bf5d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.7
|