scludam.rutils module
Module for R helper functions.
- scludam.rutils.clean_r_session(rsession, mode: str = 'all')[source]
Remove saved elements from R session.
- Parameters:
rsession (rpy2.robjects.R) – Active R session object.
mode (str, optional) – Elements to remove, by default “all”. Possible values are “all” to remove variables, functions and imported packages, “vars” to remove only variables.
- Returns:
Active R session object, the same provided.
- Return type:
rpy2.robjects.R
- scludam.rutils.load_r_packages(rsession, packages: List[str] | str)[source]
Load R packages into R session.
If the package is not installed, it will try to install it from the first CRAN mirror.
- Parameters:
rsession (rpy2.robjects.R) – Active R session.
packages (Union[List[str], str]) – A package name or list of package names
- Returns:
R session object with loaded packages, the same R session provided.
- Return type:
rpy2.robjects.R
- scludam.rutils.assign_r_args(rsession, **kwargs)[source]
Assign R arguments to R session.
Loads function kwargs as R variables into the provided R session. The kwargs supported can have any name, but must be of type str, int, float, bool or np.ndarray.
- Parameters:
rsession (rpy2.robjects.R) – Active R session.
- Returns:
rpy2.robjects.R – Provided R session with assigned arguments.
str – A string containing all the assigned arguments, useful for calling R commands, assuming vars are named as they are used in the R command. For example:
_, my_r_args = assing_r_args(rsession, a=1, b="foo")
returnsmy_r_args="a=a, b=b"
. Then the a command can be run as:r(f"myRfunction({my_r_args})")
.
- Raises:
TypeError – If kwarg is not of type str, int, float, bool or np.ndarray.