Skip to content

Instantly share code, notes, and snippets.

@dwhdai
Created April 10, 2019 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwhdai/25b67fd0645559fbb2b8e720bb74a47b to your computer and use it in GitHub Desktop.
Save dwhdai/25b67fd0645559fbb2b8e720bb74a47b to your computer and use it in GitHub Desktop.
Export log file for parallel processing in R
# 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