Skip to content

Instantly share code, notes, and snippets.

View andrie's full-sized avatar

Andrie de Vries andrie

View GitHub Profile
@andrie
andrie / cleanGoogleTable.R
Last active November 20, 2016 12:43
Functions to read Google Docs spreadsheet into R
library(XML)
cleanGoogleTable <- function(dat, table=1, skip=0, ncols=NA, nrows=-1, header=TRUE, dropFirstCol=NA){
if(!is.data.frame(dat)){
dat <- dat[[table]]
}
if(is.na(dropFirstCol)) {
firstCol <- na.omit(dat[[1]])
if(all(firstCol == ".") || all(firstCol== as.character(seq_along(firstCol)))) {
dat <- dat[, -1]
}
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 / 1-miniCRAN.R
Last active April 2, 2018 11:44
Introduction to miniCRAN
# Install package from CRAN
getOption("repos")
options(repos=c(CRAN="http://cran.at.r-project.org/"))
install.packages("miniCRAN", type="source")
library("miniCRAN")
sessionInfo()
@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 / run-urbanek-benchmark-25.R
Created October 16, 2014 11:34
Source Simon Urbanek's benchmark v2.5 and capture total execution time
# Run Simon Urbanek's benchmark v2.5
cat("R version\n")
cat("=========\n")
print(R.version)
if(exists("Revo.version")) {
cat("Revo version")
cat("============")
print(Revo.version)
@andrie
andrie / rro-mkl-benchmark.R
Last active December 15, 2023 10:20
Test performance of Intel MKL on matrix operations
# Set MKL threads if Revolution R Open or Revolution R Enterprise is available
if(require("RevoUtilsMath")){
setMKLthreads(4)
}
# Initialization
set.seed (1)
@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))
}