R

Install

  1. R

  2. RStudio

  3. Installing R Packages in Your Own Directories You need to pre-create a directory, e.g., mkdir ~/R-packages, then

    1. run export R_LIBS=~/R-packages

    2. or in the installation R code, add the lib=“~/R-packages” option to the install.packages function.

Automatically install and load packages
list.of.packages <- c(
  "foreach",
  "doSNOW"
)
new.packages <- list.of.packages[!(list.of.packages %in%
                                     installed.packages()[,"Package"])]
if(length(new.packages) > 0){
  install.packages(new.packages, dep=TRUE)
}

for(package.i in list.of.packages){
  suppressPackageStartupMessages(
    library(
      package.i,
      character.only = TRUE
    )
  )
}

High Performance Computing

Use foreach with doSNOW
library(doSNOW)
n.cores <- min(60, parallel::detectCores() - 1)
cl <- makeSOCKcluster(n.cores)
registerDoSNOW(cl)

num_iteration <- 10

# show progress bar
pb <- txtProgressBar(max = num_iteration, style = 3)
progress <- function(n) setTxtProgressBar(pb, n)
opts <- list(progress=progress)

# speed up your for loop
results <- foreach(
  i_iteration = 1:num_iteration,
  .combine = 'cbind',
  # .packages=c('lme4'), # you need to state the packages you have used in the loop
  .options.snow=opts
) %dopar% {
  set.seed(i_iteration)
  result <- rnorm(1)
}
results