Basic Usage Examples for rpy-bridge
This page demonstrates common usage patterns for rpy-bridge:
Loading R scripts and calling functions
Accessing multiple namespaces
Calling functions from R packages
Inspecting available functions in scripts and packages
Python Example
1from pathlib import Path
2
3from rpy_bridge import RFunctionCaller
4
5# -----------------------------
6# Example 1: Single R script
7# -----------------------------
8rfc = RFunctionCaller(scripts=Path("my_script.R"))
9
10# Call a function from the script
11result = rfc.call("add_numbers", 3, 5)
12print("add_numbers(3,5) =", result)
13
14# -----------------------------
15# Example 2: Multiple scripts / namespaces
16# -----------------------------
17rfc_multi = RFunctionCaller(scripts=["script1.R", "script2.R"])
18
19# List loaded namespaces
20print("Namespaces:", rfc_multi.list_namespaces())
21
22# List functions in script1
23print("script1 functions:", rfc_multi.script1.list_functions())
24
25# Call a function from script2 via namespace
26result2 = rfc_multi.script2.multiply_numbers(4, 6)
27print("multiply_numbers(4,6) =", result2)
28
29# -----------------------------
30# Example 3: Calling R package functions
31# -----------------------------
32# Call 'mean' from base R package
33mean_val = rfc.call("base::mean", [1, 2, 3, 4])
34print("mean([1,2,3,4]) =", mean_val)
35
36# -----------------------------
37# Example 4: Inspect available functions
38# -----------------------------
39rfc_multi.print_function_tree(include_packages=False)
Note
All paths to R scripts are relative to your Python working directory. Make sure R and rpy2 are installed and accessible in your environment.