Skip to content

io

I/O helpers for brmspy.

This module contains helpers for: - loading example datasets from R packages - saving/loading R objects via saveRDS / readRDS

Notes

These functions are executed inside the worker process that hosts the embedded R session.

Classes

Functions

get_data(dataset_name, **kwargs)

Load an R dataset and return it as a pandas DataFrame.

This is a thin wrapper around R's data() that loads the object into the R global environment and converts it to a :class:pandas.DataFrame.

Parameters:

Name Type Description Default
dataset_name str

Name of the dataset as used in R (e.g. "BTdata").

required
**kwargs

Additional keyword arguments forwarded to R's data() function, for example package="MCMCglmm" or other arguments supported by utils::data() in R.

{}

Returns:

Type Description
DataFrame

Dataset converted to a pandas DataFrame.

Raises:

Type Description
KeyError

If the dataset is not found in the R global environment after calling data().

RuntimeError

If conversion from the R object to a pandas DataFrame fails.

See Also

get_brms_data Convenience wrapper for datasets from the brms package.

get_brms_data(dataset_name, **kwargs)

Load an example dataset from the R brms package.

Parameters:

Name Type Description Default
dataset_name str

Dataset name (for example "epilepsy" or "kidney").

required
**kwargs

Forwarded to R utils::data() via get_data().

{}

Returns:

Type Description
DataFrame

Dataset converted to a DataFrame.

Examples:

from brmspy import brms

epilepsy = brms.get_brms_data("epilepsy")
assert epilepsy.shape[0] > 0

save_rds(object, file, **kwargs)

Save an R object to an .rds file via R saveRDS().

Parameters:

Name Type Description Default
object RListVectorExtension or ProxyListSexpVector

Object to save. If you pass a FitResult, the underlying brmsfit is saved.

required
file str

Output path.

required
**kwargs

Forwarded to R saveRDS() (for example compress="xz").

{}

Returns:

Type Description
None

Examples:

from brmspy import brms

model = brms.brm("y ~ x", data=df, chains=4)
brms.save_rds(model, "model.rds")

read_rds_raw(file, **kwargs)

Load an R object from an .rds file via R readRDS().

This returns the raw R object handle.

Parameters:

Name Type Description Default
file str

Input path.

required
**kwargs

Forwarded to R readRDS().

{}

Returns:

Type Description
ListSexpVector

Raw R object.

Examples:

from brmspy import brms

obj = brms.read_rds_raw("model.rds")

read_rds_fit(file, **kwargs)

Load a saved brms model from an .rds file.

Parameters:

Name Type Description Default
file str

Input path containing a saved brmsfit.

required
**kwargs

Forwarded to R readRDS().

{}

Returns:

Type Description
FitResult

FitResult containing ArviZ InferenceData and an underlying R handle.

Examples:

from brmspy import brms

fit = brms.read_rds_fit("model.rds")
fit.idata.posterior