
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  runBySourcingFolders.r                                                                 #
  #           Last Updated Funclist  :  08 Feb 2015,  5:12 AM (Sunday)                                                         #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   runBySourcingFolders ( foldersList, parentFolder=NULL, restartAttempts=1                                                 #
  #                          , continue.ToNextFolderIfCannotSourceCurrentFolder=FALSE )                                        #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

runBySourcingFolders <- function(foldersList, parentFolder=NULL, restartAttempts=1
	, continue.ToNextFolderIfCannotSourceCurrentFolder=FALSE) {

	allFolders <- as.path(parentFolder, foldersList)

	## Confirm everything exists
	if(any(!file.exists(allFolders))) {
		stop("\n\nThe following folders not found: \n   ", paste(allFolders[!file.exists(allFolders)], collapse="\n   "))
	}

	for (folder in allFolders) {
	
		## Initialize
		restartsRemaining <- restartAttempts
		succeeded <- NULL
	
		while(restartsRemaining >= 1 && !isTRUE(succeeded)) {
			if (restartAttempts - restartsRemaining > 0)
				cat("****** RESTART ATTEMPT #", restartAttempts - restartsRemaining, " for folder '", folder, "' ******\n", sep="")
			for(fil in dir(folder, recursive=TRUE, include.dirs=FALSE, full.names=TRUE)) {
			cat("Sourcing files in  ", fil, "\n")
				caught <- try(source(fil))
#				caught <- try({i <- i+1; if(i == 12) stop("ERROR") else "HELLO"})
				if (inherits(caught, "try-error"))
					break
			}
			if (!inherits(caught, "try-error"))
				succeeded <- TRUE
			restartsRemaining <- restartsRemaining - 1
		} #// end while-loop

		if (!continue.ToNextFolderIfCannotSourceCurrentFolder && !isTRUE(succeeded))
			stop ("\n\n", paste("**** ", center(paste0("  \nSourcing folder\n'", folder, "'\n failed during file\n'", fil, "'\n  "))  ," ****", collapse="\n"), "\n")

	} # // end outter for-loop
}
