setwd("/home/ion606/Desktop/Homework/Data Analytics/Assignments/Assignment II/")

# yes I am lazy
dir.create("/home/ion606/Desktop/Homework/Data Analytics/Assignments/Assignment II/output/stats", recursive = TRUE, showWarnings = FALSE)
dir.create("/home/ion606/Desktop/Homework/Data Analytics/Assignments/Assignment II/output/figures", recursive = TRUE, showWarnings = FALSE)


# These functions were chatgpt generated because I was having some issues with sourcing
options(
	warn = 1,
	keep.source = TRUE,
	show.error.locations = TRUE
)

safe_source <- function(file) {
	message("Sourcing: ", file)
	tryCatch(
		{
			# echo=TRUE prints each line before running, aiding pinpointing failures
			source(file, echo = TRUE, chdir = TRUE, keep.source = TRUE, local = new.env(parent = globalenv()))
		},
		error = function(e) {
			cat("\nError while sourcing: ", file, "\n", sep = "")
			cat(conditionMessage(e), "\n", sep = "")
			cat("Calls:\n"); print(sys.calls())
			stop(e)  # rethrow to halt execution with context
		}
	)
}


safe_source("R/01_args_and_load.R")
safe_source("R/02_plots.R")
safe_source("R/03_ols_full.R")
safe_source("R/04_ols_region.R")
safe_source("R/05_knn.R")
safe_source("R/06_report.R")
