'Fastest way to drive a vector of library packages given the vector of packages' names
I am currently trying to see if there is a fast way to apply library() into a vector of string elements so that the final vector would result in a list of library of each package. For example, if I have a vector c("base", "dplyr", "parallel", "RPostgresSQL", "DBI", "rlang"), and I want to obtain a vector c(library("base"), library("dplyr"), library("parallel"), library("RPostgresSQL"), library("DBI"), library("rlang")). My goal is to put this vector of library into clusterEvalQ() so that each cluster node can have all the packages to execute the function clusterMap().
My attempt.
lib.list <- unlist(lapply(c("base", "dplyr", "parallel", "RPostgresSQL", "DBI", "rlang")), function(x) paste0('library(',as.character(x), )')) --- only gives me the vector of each string-type library (e.g. "library(base)".)
noquote(lib.list) --- Get rid of the annoying double-quote characters around "library(base)".
I have two questions though:
- It seems to me that each time we execute the
clusterMap(), we need to execute themakeCluster()to refresh each cluster node, if we update the variablelib.listinclusterEvalQ(cl, lib.list)? I tried executing only theclusterMap()but always ended up getting the same result, which does not make sense. - Is there a faster way, in terms of execution time, to achieve the original goal?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
