from rpy2.robjects import packages as rpackages


def is_r_package_installed(package_name: str) -> bool:
    """
    Checks if a given R package is installed.

    Parameters
    ----------
    package_name : str
        The name of the R package to check.

    Returns
    -------
    bool
        True if the package is installed, False otherwise.
    """
    return rpackages.isinstalled(package_name)


def install_r_package(package_name: str):
    """
    Installs a given R package using CRAN mirror.

    This function selects the first CRAN mirror and installs the specified R package.

    Parameters
    ----------
    package_name : str
        The name of the R package to install.
    """
    utils = rpackages.importr("utils")
    utils.chooseCRANmirror(ind=1)
    utils.install_packages(package_name)


if not is_r_package_installed("BAS"):
    install_r_package("BAS")

BAS = rpackages.importr("BAS")
