A simple commandline wrapper package for call a target R# function from commandline
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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
File details
Details for the file r_lambda-1.1.2025.820.tar.gz.
File metadata
- Download URL: r_lambda-1.1.2025.820.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
294419a099ca5e66be45b929e21c17077c29176d44f95b8fa7917736e7db9c50
|
|
| MD5 |
39c9fd0d6f54dcdf4cbbe35b1f01f9d0
|
|
| BLAKE2b-256 |
e41cbf867fffd8ee4d8040e37c7421a2ea7b6adecd500d8a856e139b9ec1ef36
|
File details
Details for the file r_lambda-1.1.2025.820-py3-none-any.whl.
File metadata
- Download URL: r_lambda-1.1.2025.820-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72fb7ecd92ed883fc1f61e71f33e0d0ac1819854b8d68d15db0789a697974241
|
|
| MD5 |
f4dbc170fa4b321e448cc00fe758a88e
|
|
| BLAKE2b-256 |
44a85f162ad6fc775847ef7f6067bbfbe7d1a992c4856b55f59a0f9ccee822ee
|