Skip to content

Instantly share code, notes, and snippets.

View lgatto's full-sized avatar

Laurent Gatto lgatto

View GitHub Profile
@idot
idot / tikz2pdf.py
Created July 26, 2011 10:05
converts .tikz to pdf files
#!/usr/bin/env python
#
# Copyright (c) 2007, Hans Meine <[email protected]>
# All rights reserved.
#
# This is licensed according to the new BSD license.
# Please send patches / comments, I would be happy about any feedback.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
@even4void
even4void / tufte_bxp.R
Created August 5, 2011 23:26
Tufte's boxplot
tufte.boxplot <- function(x, g) {
k <- nlevels(g)
crit.val <- tapply(x, g, median)
plot(1:k, crit.val, ylim=c(min(x)*1.1, max(x)*1.1), pch=19,
xlab=deparse(substitute(g)), ylab=deparse(substitute(x)))
for (i in 1:k) {
tmp <- boxplot.stats(x[as.numeric(g)==i])
segments(i, tmp$stats[1], i, tmp$stats[2])
segments(i, tmp$stats[4], i, tmp$stats[5])
points(rep(i, length(tmp$out)), tmp$out, cex=.8)
@stephenturner
stephenturner / deseq-vs-edger.R
Created September 18, 2012 19:23
DESeq vs edgeR comparison
# Note: using the devel versions of both packages!
library(DESeq) # version 1.9.11
library(edgeR) # version 2.99.8
library(VennDiagram)
# Read in data ------------------------------------------------------------
## Use pasilla data
datafile = system.file( "extdata/pasilla_gene_counts.tsv", package="pasilla" )
datafile
@malcook
malcook / pvec.R
Created September 20, 2012 19:32
a retake on parallel:pvec
pvec2<-function(v, FUN, ..., mc.set.seed = TRUE, mc.silent = FALSE,
mc.cores = getOption("mc.cores", 2L), mc.cleanup = TRUE
,combineFUN = `c`
) {
### AUTHOR: [email protected]
### PURPOSE: an improvement(?) on parallel:pvec which
### (1) does not require v to be a vector, rather, v must
### implement `[`, `length`. Thus, i.e. BioConductor List (including
### GRangesList) is supported.
### (2) uses `parallel:splitIndices` to compute indices into v
@dsparks
dsparks / color_picker.R
Created November 5, 2012 23:31
Color picker in R
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2", "proxy")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
nIncrements <- 36
eachIncrement <- seq(0, 1, len = nIncrements)
colorSweep <- expand.grid(eachIncrement, eachIncrement, eachIncrement)
moduloRemainder <- with(colorSweep, Var1*(nIncrements-1)) %%
floor(sqrt(nIncrements))
@dsparks
dsparks / stat_2d.R
Created November 13, 2012 02:06
Encoding two-dimensional density in graphical variables
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Generate some randomly-distributed data
nObs <- 5000
myData <- data.frame(X = rnorm(nObs), Y = rnorm(nObs))
nClusters <- 7 # Cluster it
kMeans <- kmeans(myData, centers = nClusters)
@hadley
hadley / s3.r
Created May 7, 2013 13:16
Implementation of request signing for Amazon's S3 in R.
library(httr)
library(digest)
library(XML)
s3_request <- function(verb, bucket, path = "/", query = NULL,
content = NULL, date = NULL) {
list(
verb = verb,
bucket = bucket,
path = path,
@hadley
hadley / postmarkapp.r
Created June 4, 2013 17:16
Send an email from R using postmarkapp
library(base64enc)
library(RJSONIO)
library(httr)
default_key <- function () {
key <- Sys.getenv("POSTMARKAPP_API_KEY")
if (key == "") {
stop("Either provide key or set envvar POSTMARKAPP_API_KEY", call. = FALSE)
}
key
@hadley
hadley / s3.md
Created August 14, 2013 14:26
Some S3 brainteasers
  • UseMethod() has a special way of calling methods which has some weird consequences. Predict what the following code will return, then run it and read the help for UseMethod() to figure out what's going on. Write down the rules in the simplest form possible.

    y <- 1
    g <- function(x) { 
      y <- 2
      UseMethod("g")
    }
    g.numeric <- function(x) y

g(10)

library(microbenchmark)
microbenchmark(
mtcars["Volvo 142E", "carb"],
mtcars[32, 11],
mtcars[[c(11, 32)]],
mtcars[["carb"]][32],
mtcars[[11]][32],
mtcars$carb[32],
.subset2(mtcars, "carb")[32],