R Environment Setup Guide
MassFlow's default preprocessing functions are implemented in Python by MassFlow itself. MassFlow also provides an interface to Cardinal.
You only need to install and configure an R environment when you explicitly select backend="cardinal", need compatibility with an existing Cardinal workflow, or want to compare methods.
This page explains how to configure the R environment, including how to set the R installation path.
Authors: Leqiang Dong, Jingwei Dong
Feedback: If you find any problems while using MassFlow or reading the documentation, you can submit feedback through GitHub Issues, or contact us by email at 707244007@qq.com.
1. R Environment Detection
When the R environment is initialized, MassFlow searches for the R installation path (R_HOME) in the following order of priority:
- Function parameter: a path passed directly when the initialization function is called.
R_HOMEenvironment variable: theR_HOMEvalue configured in the operating-system environment variables.- Default configuration: the path set through
set_default_r_home. - Automatic detection: MassFlow attempts to obtain the R installation path with a system command (
R RHOME).
If no valid R path can be found by any of these methods, the program raises an error.
2. How to Configure the R Path
Method 1: Use an Environment Variable (Recommended)
Before running your Python script, set the system environment variable R_HOME to the R installation directory.
Windows PowerShell example:
$env:R_HOME = "C:\Program Files\R\R-4.3.1"
python main.pyLinux/macOS example:
export R_HOME=/usr/lib/R
python main.pyMethod 2: Set the Default Path in Code
Before importing other MassFlow modules, use the set_default_r_home function to set the default R path.
from massflow.r_preprocess import set_default_r_home
# Set R installation path
set_default_r_home("C:\\Program Files\\R\\R-4.3.1")
# Proceed with other operations
from massflow.r_preprocess import init_r_environment
env = init_r_environment()Method 3: Specify the Path During Initialization
Pass the r_home argument directly when initializing the R environment.
from massflow.r_preprocess import init_r_environment
# Initialize and specify path
env = init_r_environment(r_home="C:\\Program Files\\R\\R-4.3.1")3. Common Problems
R_HOME Not Found Error
If you encounter this error, MassFlow was unable to locate R automatically. Check whether R is installed, or manually specify the path using one of the methods described above under “How to Configure the R Path.”
Errors Related to UnicodeDecodeError
MassFlow automatically sets the R locale to English (LANGUAGE=en, LC_ALL=C) to avoid character-encoding problems. In most cases, no manual user intervention is required.