-
Notifications
You must be signed in to change notification settings - Fork 22
Description
This is written by Claude Code:
Refactoring Suggestion: Unify Progress Tracking with progressr Package
Summary
The current implementation uses pbapply for serial and parallel execution paths, while the future execution path uses progressr. I suggest refactoring to use progressr consistently across all execution paths for better maintainability and consistency.
Current State
The codebase currently has three different progress tracking approaches:
- Serial path (R/analysis.R:38-73): Uses
pbapply::pblapply() - Parallel cluster path (R/analysis.R:84-97): Uses
pbapply::pblapply(cl=cl) - Future path (R/analysis.R:14-35): Uses
progressr::progressor()✓
Rationale for Unification
Benefits of using progressr everywhere:
- Consistency: Single progress tracking mechanism across all execution modes
- Already imported:
progressris already a dependency (DESCRIPTION:35) - Proven to work: Future path demonstrates it works well in practice
- Better flexibility: Users can customize progress handlers via
progressr::handlers() - File-friendly by design:
progressrhandles non-interactive output naturally - Reduced dependencies: Could potentially remove
pbapplydependency entirely - Maintainability: One codebase to maintain instead of two different systems
Proposed Changes
Replace pbapply::pblapply() calls with progressr-based implementation similar to the future path:
Serial path (currently lines 38-73):
# Instead of:
results <- if(progress){
try(pbapply::pblapply(1L:replications, used_mainsim, ...))
} else {
try(lapply(1L:replications, used_mainsim, ...))
}
# Use:
iters <- 1L:replications
if(progress) {
p <- progressr::progressor(along = iters)
} else {
p <- NULL
}
results <- try(lapply_with_progress(iters, used_mainsim, p = p, ...))
# Where lapply_with_progress is:
lapply_with_progress <- function(X, FUN, p = NULL, ...) {
results <- vector("list", length(X))
for(i in seq_along(X)) {
results[[i]] <- FUN(X[[i]], ...)
if(!is.null(p)) p()
}
results
}Parallel path: Similar adaptation using parallel::parLapply() with progressor updates on the main process.
Backward Compatibility
- No user-facing API changes required
progressparameter continues to work as before- Users who already customize
progressr::handlers()see no change - Interactive users still get progress bars (via progressr's default handlers)
Migration Path
- Phase 1: Add
progressrimplementation alongside existingpbapplycode - Phase 2: Test thoroughly across all execution modes
- Phase 3: Deprecate
pbapplyusage (keep as fallback) - Phase 4: Remove
pbapplydependency in future major version
Alternative: Keep Current Hybrid Approach
If maintaining pbapply is preferred:
Pros:
- No refactoring needed
pbapplyis mature and stable- Current fix (recent commit) already solves the non-interactive issue
Cons:
- Two different progress systems to maintain
- Inconsistent user experience between execution modes
- Future path already proves
progressris sufficient
Implementation Effort
- Low-Medium: Core logic already exists in future path
- Most work is adapting serial/parallel paths to use similar pattern
- Extensive testing needed across execution modes
Note: This is a long-term architectural suggestion and not urgent. The recent fix already addresses the immediate need for SLURM cluster progress tracking.