[R] help with parellel processing and RSelenium

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sun Dec 12 18:12:17 CET 2021


Hello,

Inline.

Às 16:43 de 12/12/21, akshay kulkarni escreveu:
> dear members,
>                           I am a stock trader based in INDIA using R for my research. I have two questions:
> 
> 
>    1.  I want to send the same function with different arguments to different cores. This link in SO https://stackoverflow.com/questions/25045998/send-function-calls-with-different-arguments-to-different-processors-in-r-using
>    2.  gives the following solution:
> 
>     library(parallel)
> 
> cl <- makeCluster(4)
> clusterExport(cl, "foo")
> cores <- seq_along(cl)
> r <- clusterApply(cl[cores], cores, function(core) {
>    if (core == 1) {
>      foo(5, 4, 1/2, 3, "a")
>    } else if (core == 2) {
>      foo(5, 3, 1/3, 1, "b")
>    } else if (core == 3) {
>      foo(5, 4, 1/4, 1, "c")
>    } else if (core == 4) {
>      foo(5, 2, 1/5, 0, "d")
>    }})
> 
> My question is: what is the structure of the output "r" in the above code? I think it is a list with r[[1]] = output of foo(5,4,1/2,3,"a"),r[[2]] = output of foo(5,1/3,1,"b")
> 
> and so on. AM I right?

Yes, you are right. Why don't you try and print r[[1]]?

Anyway, I would put the parameters in a list and pass them to the 
function following the below lines.


library(parallel)

foo <- function(pars){
   x <- pars$x
   y <- pars$y
   z <- pars$z
   w <- pars$w
   alpha <- pars$alpha
   res <- (x + y)*z^w
   list(result = res, message = alpha)
}
params <- list(
   list(x=5, y=4, z=1/2, w=3, alpha="a"),
   list(x=5, y=3, z=1/3, w=1, alpha="b"),
   list(x=5, y=4, z=1/4, w=1, alpha="c"),
   list(x=5, y=2, z=1/5, w=0, alpha="d")
)

cl <- makeCluster(4)
clusterExport(cl, "foo")
clusterExport(cl, "params")
cores <- seq_along(cl)
r <- clusterApply(cl[cores], cores, function(core) {
   foo(params[[core]])
})
stopCluster(cl)

do.call(rbind.data.frame, r)


Hope this helps,

Rui Barradas


> 
>    1.
> 
> I am using RSelenium to scrape a website. Javascript has a document.ready function which ensures that any JS code is run only after the whole document is
> 
>    *
> 
> loaded. Is there a similar function in RSelenium? Or will the execution of the next expression takes place only after the whole page is loaded (with the
> 
>    *
> 
> "navigate" method of RSelenium)?
> 
> Thanking you,
> 
> Yours sincerely,
> 
> AKSHAY M KULKARNI
> 
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list