Created
April 10, 2019 20:27
-
-
Save dwhdai/25b67fd0645559fbb2b8e720bb74a47b to your computer and use it in GitHub Desktop.
Export log file for parallel processing in R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a simple example of how you can create an external log file for monitoring parallel jobs in R, using doSNOW and foreach | |
library(doSNOW) | |
library(foreach) | |
n <- 10 | |
cl <- makePSOCKcluster(5) | |
registerDoSNOW(cl) | |
writeLines(c(""), "log.txt") | |
results <- foreach(i = 1:n, .combine = rbind) %dopar% { | |
x <- sample(1:10, 1) | |
y <- sample(seq(0.01, 0.05, by = 0.001), 1) | |
res <- c(x = x,y = y) | |
# Create temporary artificial delays between iterations | |
Sys.sleep(0.5) | |
sink("log.txt", append=TRUE) | |
# Change text | |
cat("Iteration", i, "\n") | |
} | |
stopCluster(cl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment