% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/loop_guardian.R
\name{loop_guardian}
\alias{loop_guardian}
\title{Bulk Loop Guardian}
\usage{
loop_guardian(
  items,
  target_func,
  limit_mb = 4000,
  save_path = "emergency_checkpoint.rds"
)
}
\arguments{
\item{items}{A vector or list of items to process.}

\item{target_func}{The function to apply to each item.}

\item{limit_mb}{Numeric. The memory limit in megabytes before triggering the alarm. Defaults to `4000`.}

\item{save_path}{Character. Where to dump the emergency checkpoint data. Defaults to `"emergency_checkpoint.rds"`.}
}
\value{
A list of successfully processed results.
}
\description{
A memory-safe wrapper for heavy iterations that monitors RAM usage and 
triggers an interactive failsafe if the environment approaches critical capacity.
}
\details{
The function provides a safeguard against memory-related crashes during 
large-scale batch processing:
\enumerate{
  \item Iterates through the `items` vector, applying `target_func` to each element.
  \item Every 50 iterations, it checks the current memory usage using `gc()`.
  \item If the memory usage exceeds `limit_mb`, it first attempts a deep garbage 
      collection to recover RAM.
  \item If memory remains above the threshold after GC, it triggers an interactive 
      alarm, allowing the user to:
      \itemize{
        \item Save current progress to `save_path` and abort.
        \item Ignore the limit and attempt to continue.
        \item Abort immediately without saving.
      }
}
}
\section{Warning}{

This function modifies files on disk or the global environment. Please ensure you have a backup or are using version control (e.g., Git) before execution.
}

\examples{
if (interactive()) {
  loop_guardian(items = 1:5, target_func = print)
}
}
