I am trying to run a loop in R for string detect for on multiple cores using foreach but I see it runs only on one core and I was not able to bring it to run on several cores.
library("parallel")
library("foreach")
library("doParallel")
#How to detect how many cores are on the server
detectCores()
cl <- makeCluster(3)
registerDoParallel(cl)
x<-c("Hello My Name is", "Happy Birthday", "Hi How are you today? My Name is walley", "Nice to meet you", "Best friends")
y<-c("Hello", "Birthday", "Hi", "Nice", "Best friends")
foreach(i = 1:length(y)) %dopar%{
print(i)
if (sum(str_detect(x,paste("\\b",as.character(y),"\\b", sep=""))) > 0){
str_replace(x[i], as.character(y[which(str_detect(x[i],paste("\\b",as.character(y),"\\b", sep="")) == TRUE)]), "")
}
}
write.csv(mycatalog, "Matching.csv")
getDoParWorkers()
stopCluster(cl)