Just seeing if there is a sure fire way to determine if you are already within a foreach loop using %dopar%? As it is currently, I have a workaround function that parses sys.status() for any foreach calls that also use %dopar%-
isAlreadyInParallel = function() {
status = sys.status()
return(any(grepl(
'%dopar%',
status$sys.calls[grepl('foreach(', status$sys.calls, fixed = TRUE)],
fixed = TRUE)))}
However, this feels pretty limited and doesn't cover the case in which someone uses a variable to hold %dopar% like below-
doFunction = if (foreach::getDoParRegistered()) `%dopar%` else `%do%`
doFunction(foreach(1:5), {...})