A Python library and CLI tool for analyzing R code quality metrics. This tool can analyze both local R projects and remote Git repositories (GitHub, GitLab, Bitbucket, etc.). Supports both functional and object-oriented R code (S3, S4, R6, and Reference Classes).
Install directly from GitHub using pip:
pip install git+https://github.com/yourusername/r-quality-analyzer.gitReplace yourusername with your actual GitHub username.
Clone and install locally:
git clone https://github.com/yourusername/r-quality-analyzer.git
cd r-quality-analyzer
pip install .For development, install in editable mode:
pip install -e .The analyzer calculates the following metrics:
- file: Path to the analyzed file
- loc: Lines of Code (excluding comments and blank lines)
- nom: Number of Methods/Functions
- cc_avg: Average Cyclomatic Complexity
- mpc: Methods Per Class (for OOP: average methods per class; for functional: ratio of method calls to functions)
- cbo: Coupling Between Objects (number of package dependencies)
- lcom: Lack of Cohesion of Methods
- paradigm: Code paradigm detected:
"functional","oop", or"mixed" - classes (OOP only): Dictionary of class names and their method counts
- num_classes (OOP only): Total number of classes detected
The analyzer automatically detects and handles:
- Functional R code: Standard function definitions
- S3 methods: Functions with dot notation (e.g.,
print.myclass) - S4 classes: Classes defined with
setClass()and methods withsetMethod() - R6 classes: Modern OOP classes defined with
R6Class() - Reference Classes: Classes defined with
setRefClass()
r-quality-analyzer /path/to/r/project# GitHub (shorthand format)
r-quality-analyzer user/repo
# GitHub (full URL)
r-quality-analyzer https://github.com/user/repo
# GitLab
r-quality-analyzer https://gitlab.com/user/repo
# Bitbucket
r-quality-analyzer https://bitbucket.org/user/repo
# SSH format
r-quality-analyzer git@github.com:user/repo.gitr-quality-analyzer -f /path/to/file.Rr-quality-analyzer user/repo -o results.jsonr-quality-analyzer user/repo --keep-clonefrom r_quality_analyzer import analyze_file, analyze_repo
# Analyze a single file
metrics = analyze_file("path/to/file.R")
print(metrics)
# Analyze a repository
results = analyze_repo("path/to/r/project")
print(results)- Python 3.7+
- GitPython (for Git repository cloning)
{
"repo": "https://github.com/AMantes/Greekonomics",
"repo_name": "Greekonomics",
"local_repo": "local\\path\\r_quality_analyzer_lma0y3bn",
"total_files": 2,
"total_loc": 1889,
"total_nom": 30,
"avg_cc": 1.98,
"avg_mpc": 13.33,
"total_cbo": 4,
"avg_lcom": 62.0,
"paradigm": "functional",
"paradigm_distribution": {
"functional": 2
},
"total_classes": 0,
"files": [
{
"loc": 782,
"nom": 9,
"cc_avg": 1.78,
"complexities": [
{
"function": "theme_greekonomics",
"start_line": 32,
"cc": 1
},
...
],
"mpc": 8.56,
"cbo": 2,
"lcom": 7,
"paradigm": "functional",
"classes": {},
"num_classes": 0,
"file": "C:\\Users\\User\\AppData\\Local\\Temp\\r_quality_analyzer_lma0y3bn\\Greekonomics_51_public.R"
},
...
]
}MIT License