title: R doParallel: couldn't find function
body: <p>I have set up the following function:</p>
  
  <pre><code>cv_model &lt;- function(dat, targets, predictors_name){
  
    library(randomForest)
    library(caret)
    library(MLmetrics)
    library(Metrics)
  
    # set up error measures
    sumfct &lt;- function(data, lev = NULL, model = NULL){
      mape &lt;- MAPE(y_pred = data$pred, y_true = data$obs)
      RMSE &lt;- sqrt(mean((data$pred - data$obs)^2, na.omit = TRUE))
      MAE &lt;- mean(abs(data$obs - data$pred))
      BIAS &lt;- mean(data$obs - data$pred)
      Rsquared &lt;- R2(pred = data$pred, obs = data$obs, formula = "corr",    na.rm = FALSE)
      c(MAPE = mape, RMSE = RMSE, MAE = MAE, BIAS = BIAS, Rsquared = Rsquared)
    }
  
    for (k in 1:length(dat)) {
  
      a &lt;- dat[[k]][dat[[k]]$vari == "a", -c(which(names(dat[[k]]) == "vari"))]
      b &lt;- dat[[k]][dat[[k]]$vari == "b", -c(which(names(dat[[k]]) == "vari"))]
      ab &lt;- list(a, b)
  
      for (i in 1:length(targets)) {
        for (j in 1:length(ab)) {
  
  
          # specifiy trainControl
          control &lt;- trainControl(method="repeatedcv", number=10,   repeats=10, search="grid", savePred =T,
                                  summaryFunction = sumfct)
  
          tunegrid &lt;- expand.grid(mtry=c(1:length(predictors_name)))
  
  
  
  
          set.seed(42)
          model &lt;- train(formula(paste0(targets[i], 
                                        " ~ ", 
                                        paste(predictors_name, sep = '',    collapse = ' + '))),
                         data = ab[[j]],
                         method="rf",
                         ntree = 25, 
                         metric= "RMSE", 
                         tuneGrid=tunegrid, 
                         trControl=control)
  
  
        }
      }
    }
  
  }
  </code></pre>
  
  <p>According to this tutorial (<a href="https://topepo.github.io/caret/parallel-processing.html" rel="nofollow noreferrer">https://topepo.github.io/caret/parallel-processing.html</a>) I can parallelize my code just by calling <code>library(doParallel); cl &lt;- makePSOCKcluster(2); registerDoParallel(cl)</code>. 
  When I then use the function with <code>doParallel</code> </p>
  
  <pre><code>predictors_name &lt;- c("Time", "Chick")
  targets &lt;- "weight"
  
  dat &lt;- as.data.frame(ChickWeight)
  dat$vari &lt;- rep(NA, nrow(dat))
  dat$vari[c(1:10,320:350)] &lt;- "a"
  dat$vari[-c(1:10,320:350)] &lt;- "b"
  
  d &lt;- list(dat[1:300,], dat[301:500,])
  
  ## use 2 of the cores
  library(doParallel)
  cl &lt;- makePSOCKcluster(2)
  registerDoParallel(cl)
  
  cv_model(dat = d, targets = targets, predictors_name = predictors_name)
  
  # end parallel computing
  stopCluster(cl)
  </code></pre>
  
  <p>the error message <code>couldn't find function "MAPE"</code> occurs. </p>
  
  <p>How can I fix this without using the foreach syntax?</p>