Skip to content

generic

Functions

sanitised_name(function)

Sanitize a function name for safe R execution.

Converts Python-style function names to valid R identifiers by: - Replacing invalid characters with underscores - Ensuring the name doesn't start with a number - Preserving namespace separators (::)

Parameters:

Name Type Description Default
function str

Function name to sanitize

required

Returns:

Type Description
str

Sanitized function name safe for R execution

Examples:

from brmspy.brms_functions.generic import sanitised_name

# Basic sanitization
print(sanitised_name("my-function"))  # "my_function"
print(sanitised_name("123func"))       # "_123func"

# Preserves namespace
print(sanitised_name("brms::loo"))     # "brms::loo"

call(function, *args, **kwargs)

Call any brms or R function by name with automatic type conversion.

Generic wrapper for calling brms functions that don't have dedicated Python wrappers. Automatically converts Python arguments to R objects and R results back to Python. Tries brms::function_name first, then falls back to base R.

This function is useful for: - Accessing newer brms functions not yet wrapped in brmspy - Calling brms utility functions without writing custom wrappers - Quick exploration of brms functionality from Python

Parameters:

Name Type Description Default
function str

Name of the R function to call. Will be prefixed with 'brms::' if possible. Can also include namespace (e.g., "stats::predict").

required
*args

Positional arguments passed to the R function. Automatically converted from Python to R types (FitResult → brmsfit, DataFrame → data.frame, etc.).

()
**kwargs

Keyword arguments passed to the R function. Python parameter names are automatically converted to R conventions.

{}

Returns:

Type Description
Any

Result from R function, automatically converted to appropriate Python type (R data.frame → pandas DataFrame, R vector → numpy array, etc.).

See Also

py_to_r : Python to R type conversion r_to_py : R to Python type conversion

Examples:

Call brms functions not yet wrapped:

from brmspy import brms
from brmspy.brms_functions.generic import call

model = brms.fit("y ~ x", data=data, chains=4)

# Call brms::neff_ratio (not yet wrapped)
neff = call("neff_ratio", model)
print(neff)

# Call brms::rhat (not yet wrapped)
rhat = call("rhat", model)
print(rhat)

Call with keyword arguments:

# Call brms::hypothesis for testing hypotheses
hypothesis_result = call(
    "hypothesis",
    model,
    hypothesis="b_x1 > 0",
    alpha=0.05
)
print(hypothesis_result)

Access functions from other R packages:

# Call functions with namespace
result = call("stats::AIC", model)
print(result)