Learn R Programming

LCPA (version 1.0.0)

install_python_dependencies: Install Required Python Dependencies for Neural Latent Variable Models

Description

Checks whether five essential Python packages required to run neural latent variable models (e.g., LCAnet, LPAnet) are installed in the current Python environment. If any are missing, the user is interactively prompted to install them via reticulate::py_install(). The targeted packages are:

  • numpy — Fundamental package for numerical computing in Python.

  • torch — PyTorch deep learning framework (supports CPU/GPU computation).

  • matplotlib — 2D plotting and visualization library.

  • scikit-learn — Machine learning utilities (used here primarily for KMeans initialization).

  • scipy — Scientific computing and advanced linear algebra routines.

For torch, users can choose between CPU-only or GPU-enabled versions (with CUDA support). Available CUDA versions are filtered by OS compatibility.

This function is especially useful when deploying models that bridge R and Python via reticulate, ensuring all backend dependencies are met before model execution.

Usage

install_python_dependencies()

Arguments

Value

A named list of logical values indicating whether each package was already installed before running this function:

numpy_installed

Logical. Was numpy already available?

torch_installed

Logical. Was torch already available?

matplotlib_installed

Logical. Was matplotlib already available?

sklearn_installed

Logical. Was scikit-learn already available?

scipy_installed

Logical. Was scipy already available?

Details

The function performs the following steps for each dependency:

  1. Uses reticulate::py_module_available() to test if the module is importable.

  2. If not available, prints a message describing the package’s purpose.

  3. Prompts the user interactively (via readline) whether to proceed with installation.

  4. For torch, offers CPU/GPU choice and CUDA version selection if GPU is chosen.

  5. Installs the package using reticulate::py_install() with appropriate index URL if needed.

  6. Returns a logical list indicating initial installation status of each package.

Note: This function requires reticulate to be loaded and a valid Python environment configured. It does NOT automatically install reticulate or configure Python — that must be done separately.

Examples

Run this code
library(reticulate)

# Ensure reticulate is loaded and Python is configured
# need python
if (FALSE) {
# Run dependency installer
deps <- install_python_dependencies()

# Check which were missing
print(deps)
}

Run the code above in your browser using DataLab