Skip to content

Instantly share code, notes, and snippets.

View lgatto's full-sized avatar

Laurent Gatto lgatto

View GitHub Profile
@lgatto
lgatto / listORenv.R
Created November 25, 2010 17:15
Comparing speed when populating an environment and a preallocated list
library(MSnbase)
n <- 10000
ll <- vector("list",length=n)
e <- new.env()
tl <- system.time(for (i in 1:n)
ll[[i]] <- new("Spectrum2"))
te <- system.time(for (i in 1:n)
assign(paste("X",i,sep=""),new("Spectrum2"),e))
## Results for n=10000
@lgatto
lgatto / speed.R
Created March 5, 2011 13:44
Comparing for, apply and vectorised functions
m <- matrix(runif(1e6),nrow=10000)
## append to result vector -- slow
res1 <- NULL
t1 <- system.time(for (i in 1:1000) res1[i] <- sum(m[i,]))
## initialise to full length -- fast
res2 <- numeric(1000)
t2 <- system.time(for (i in 1:1000) res2[i] <- sum(m[i,]))
@lgatto
lgatto / watch_variable.R
Created May 13, 2012 20:40
Monitor a variable in R
## Credit Hadley Wickham
## http://www.mail-archive.com/r-help@r-project.org/msg125980.html
watch <- function(varname) {
old <- get(varname)
changed <- function(...) {
new <- get(varname)
if (!identical(old, new)) {
message(varname, " is now ", new)
old <<- new
}
@lgatto
lgatto / getVariableName.R
Created May 15, 2012 19:06
Grab a variable names passed as function parameter
##' Return the name of variable \code{varname} in call \code{match_call}.
##'
##' @title Return a variable name
##' @param match_call An object of class \code{call}, as returned by \code{match.call}.
##' @param varname An \code{character} of length 1 which is looked up in \code{match_call}.
##' @return A \code{character} with the name of the variable passed as parameter
##' \code{varname} in parent close of \code{match_call}.
@lgatto
lgatto / scatterhist.R
Created June 4, 2012 12:31
scatterhist
scatterhist <- function(x, y, xlab="", ylab="", ...){
zones <- matrix(c(2,0,1,3), ncol=2, byrow=TRUE)
layout(zones, widths=c(4/5,1/5), heights=c(1/5,4/5))
xhist <- hist(x, plot=FALSE)
yhist <- hist(y, plot=FALSE)
top <- max(c(xhist$counts, yhist$counts))
par(mar=c(3,3,1,1))
plot(x,y, ...)
par(mar=c(0,3,1,1))
barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0)
@lgatto
lgatto / ksvm-models.R
Created June 8, 2012 21:29
Different ksvm models
> library(kernlab)
> data(iris)
> irismodel1 <- ksvm(Species ~ ., data = iris)
Using automatic sigma estimation (sigest) for RBF or laplace kernel
> irismodel2 <- ksvm(Species ~ ., data = iris)
Using automatic sigma estimation (sigest) for RBF or laplace kernel
> table(predict(irismodel1, iris[,-5]), predict(irismodel2, iris[,-5]))
setosa versicolor virginica
setosa 50 0 0
@lgatto
lgatto / deseq-vs-edger.R
Created September 19, 2012 09:38 — forked from stephenturner/deseq-vs-edger.R
DESeq vs edgeR comparison
## http://gettinggeneticsdone.blogspot.co.uk/2012/09/deseq-vs-edger-comparison.html
library(DESeq)
library(edgeR)
library(VennDiagram)
# Read in data ------------------------------------------------------------
## Use pasilla data
datafile = system.file( "extdata/pasilla_gene_counts.tsv", package="pasilla" )
@lgatto
lgatto / getDependencies.R
Last active December 10, 2015 21:18
Get/install an R package's dependencies.
getDependencies <- function(p,
rep = c("BioCsoft", "BioCann", "BioCexp", "BioCextra", "CRAN"),
biocVersion = "2.12",
depLevels = c("Depends", "Imports", "Suggests"),
filter = TRUE) {
rep <- match.arg(rep)
if (rep == "CRAN") {
rep <- getOption("repos")["CRAN"]
} else {
biocMirror <- getOption("BioC_mirror", "http://bioconductor.org")
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
;; grep -h X-Key */*/cur/* | sed -e 's/X-Keywords: //; s/, /\n/g' | sort | uniq > xkeys.txt
(setq mu4e-xkeys "~/Maildir/xkeys.txt")
(defun read-lines (f)
"Return a list of lines of a file at f."
(with-temp-buffer
(insert-file-contents f)
(split-string (buffer-string) "\n" t)))