My question has two parts:
1) I want to run my function in parallel regardless of the code inside, is possible?e.g.
data("iris")
x.train <- iris[1:100,1:4]
y.train <- iris[1:100,5]
x.test <- iris[101:150,1:4]
y.test <- iris[101:150,5]
myfun<- function(x.train,y.train,x.test,y.test) {
library("e1071")
model1 <- svm(x.train,y.train,type="c-classification")
predc <<- predict(model,x.test)
model2 <- svm(x.train,y.train,type="nu-classification")
prednu <<- predict(model,x.test)
}
I want to parallelize this part:
myfun(x.train,y.train,x.test,y.test)
2) I also want to run the above function multiple times:
for i=1:10
myfun(x.train,y.train,x.test,y.test)
Can you tell me how can I do these two parts in parallel in R?
PS: My original data is immense genome reads and I run over 10 classifiers, I really need do it in parallel.
This question is more adequate for StackOverflow since it is about R programming. Also, the code you have there is no way near enough to provide you with an answer. For instance the for loop syntax is not even R, and the function returns nothing - as far as I can tell. An internet search returns plenty of tutorials that should help to get started with parallelizing R functions. 1, 2, 3, 4. Good luck.
Thanks for your information. the function pass argument to the workspace by <<- sign.
And you should post first few lines of your original data. No matter how large the data, you can always do a head on it and paste a few lines. What is the role of i other than running the same code 10 times over?
Cool, I did not know about "<<-". Learned something today.
Because it's bad practice and unsafe to use the 'global assignment' operator. Parallel (or even looped) calls to this function will overwrite each other's result. The function needs to be rewritten with a normal return value.
Yes, I read the linked entry from Advanced R, and it looks like something one should not use unless really needed. Well, the first time I saw it was in an Advanced R book, and that tells it all :)
haha, perhaps learned another way of R allowing one to shoot themselves in the foot.