Thanks for your detailed reply. It seems you are on the right track.
The missing bit: By going from single core to multicore you need to be aware that one R process is started on each core. Those processes are independent of each other and do not share the same memory and hence do not have access to the same functions let alone variables.
A generic way now would be to use parallel::clusterExport() and export the missing variables, like this:
library(doParallel)
cores <- as.integer(Sys.getenv("SLURM_CPUS_PER_TASK"))
mycluster = makeCluster(cores)
registerDoParallel(mycluster)
...
# var1 is a variable that cannot be found
var1<-1
clusterExport(mycluster,"var1")
...
flatten_chr(mlply(input, .fun = which_closest2, table=WWIS, .parallel = TRUE, .progress = "text"))
plyr however also allows for the .paropts parameter which then can contain any option you would use in foreach for parallel processing. Exporting a variable (or package) now becomes integrated in the plyr command via
flatten_chr(mlply(input, .fun = which_closest2, table=WWIS, .parallel = TRUE, .progress = "text",.paropts = list(.exports = c("var1", "var2", ...), .packages = c("package1", "package2", ...)))
So, to summarize: clusterExport is probably more explicit, specifying .paropts is more integrated.
References: