Skip to content

Instantly share code, notes, and snippets.

View andrie's full-sized avatar

Andrie de Vries andrie

View GitHub Profile
library(foreach)
library(doParallel)
# Estimate value of pi using simulation
mcPi <- function(n=1e6) 4 * sum( runif(n)^2 + runif(n)^2 < 1) / n
# Set up cluster
cl <- makeCluster(4)
@andrie
andrie / rrt-checkpoint.R
Created August 28, 2014 13:49
Introducing RRT
# Create RRT repo checkpoint
library(RRT)
checkpoint("2014-08-28")
rrt_browse()
@andrie
andrie / checkpoint-use.R
Created October 8, 2014 09:05
Using checkpoint
library(checkpoint)
checkpoint("2014-09-17")
@andrie
andrie / checkpoint-install.R
Created October 8, 2014 09:08
Installing checkpoint
install.packages("checkpoint")
library("checkpoint")
@andrie
andrie / checkpoint-example.R
Created October 8, 2014 09:09
Worked example of checkpoint
# Create temporary project and set working directory
example_project <- paste0("~/checkpoint_example_project_", Sys.Date())
dir.create(example_project, recursive = TRUE)
oldwd <- setwd(example_project)
# Write dummy code file to project
@andrie
andrie / setSnapshot.R
Created November 18, 2014 08:56
Easily set a snapshot date on MRAN
setSnapshot <- function(date){
if(missing(date)) return(getOption("repos"))
repoDate <- paste0("http://mran.revolutionanalytics.com/snapshot/", date)
response <- tryCatch(
suppressWarnings(readLines(repoDate)),
error = function(e)e
)
if(inherits(response, "error")) stop(paste0("Invalid snapshot date."))
options(repos = c(CRAN = repoDate))
}
@andrie
andrie / permutations of samples.R
Last active August 29, 2015 14:11
Computing the probability of drawing two identical numbers out of a hat
# Computing the probability of drawing two identical
# numbers out of a hat
# Incorrect - binomial distribution ---------------------------------------
# Use pbinom() to compute a probability curve for binomial
# distribution
@andrie
andrie / cran-package-communities.R
Last active August 29, 2015 14:11
Determine CRAN package clusters (communities)
## Determine CRAN package clusters (communities)
library(miniCRAN)
library(igraph)
library(magrittr)
# Download matrix of available packages at specific date ------------------
# Wrapper around available.packages ---------------------------------------
index <- function(url, type="source", filters=NULL){
contribUrl <- contrib.url(url, type=type)
available.packages(contribUrl, type=type, filters=filters)
}
# CRAN --------------------------------------------------------------------
@andrie
andrie / foreach-progressbar.R
Last active August 29, 2015 14:15
Create a tclktk progressbar to provide feedback when running a parallel job
library(foreach)
library(iterators)
library(doParallel)
library(tcltk)
# Choose number of iterations
n <- 250
# In sequence, without progress bar ---------------------------------------