R: How to quiet tidyverse when attaching it to multiple cores. (Rmarkdown)

Viewed 157

I have an issue when knitting my Rmarkdown as initializing the tidyverse package to each core in my cluster it just prints all the packages it attaches. I have tried everything like suppressPackageStartupMessages() and adding quietly = TRUE and/or verbose = FALSE. Nothing works!

Below is a reproducible example (the more cores the messier it looks in the html).

knitr::opts_chunk$set(echo = TRUE)
library(parallel)
#Assign cores to a cluster.
cl <- makeCluster(detectCores())
#The cluster needs tidyverse
clusterEvalQ(cl,suppressPackageStartupMessages(library(tidyverse, quietly = TRUE, verbose = FALSE)))

Edit 1: I have also tried to add {r,warning=FALSE,message=FALSE} to my R chunk header.

Edit 2: Output for people that do not have tidyverse installed (with 2 cores initialized):

## [[1]]
##  [1] "forcats"   "stringr"   "dplyr"     "purrr"     "readr"     "tidyr"    
##  [7] "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics"  "grDevices"
## [13] "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "forcats"   "stringr"   "dplyr"     "purrr"     "readr"     "tidyr"    
##  [7] "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics"  "grDevices"
## [13] "utils"     "datasets"  "methods"   "base"  
2 Answers

The problem is actually clusterEvalQ() that needs to be silenced. Try invisible(). Demonstration with the MASS package:

library(parallel)
cl <- makeCluster(detectCores())
invisible(clusterEvalQ(cl, library(MASS)))
r <- parSapply(cl=cl, X=seq(cl), FUN=function(i) names(sessionInfo()[[7]]))
stopCluster(cl)

Result

> library(parallel)
> cl <- makeCluster(detectCores())
> invisible(clusterEvalQ(cl, library(MASS)))
> r <- parSapply(cl=cl, X=seq(cl), FUN=function(i) names(sessionInfo()[[7]]))
> stopCluster(cl)
> 
> r
[1] "MASS" "MASS" "MASS" "MASS" "MASS" "MASS" "MASS" "MASS"

MASS appears to be loaded on all clusters without annoying console output.

As a workaround you could use capture.output:

void <- capture.output(clusterEvalQ(cl,{library(tidyverse)}))
Related